Skip to content

JSON Parser

When AI providers stream JSON responses, the individual chunks often contain incomplete JSON that cannot be parsed directly. The JSON Parser plugin automatically detects partial JSON chunks and completes them - adding any missing closing braces, brackets, and quotes - so each streamed chunk is valid JSON you can parse immediately.

  • Automatic JSON completion: Detects partial JSON and adds missing closing characters so every streamed chunk is valid JSON.
  • Streaming only: Only streaming responses are processed; non-streaming responses pass through unchanged.
  • Safe fallback: If a chunk can’t be completed into valid JSON, the original content is returned unchanged, so it never breaks your stream.

The JSON Parser is a gateway plugin. Enable it in your configuration, and choose whether it applies to every streaming request or only to requests that opt in.

{
"plugins": {
"jsonparser": {
"enabled": true,
"usage": "all_requests",
"cleanup_interval_minutes": 5,
"max_age_minutes": 30
}
}
}
SettingDefaultDescription
usage-all_requests processes every streaming response; per_request only processes requests that opt in
cleanup_interval_minutes5How often accumulated per-request state is cleaned up
max_age_minutes30How old accumulated entries can be before cleanup

When usage is per_request, send the x-bf-json-parser: true header on the requests you want completed; requests without the header stream unmodified.

For each streaming chunk, the plugin returns a string that is always valid JSON. If a chunk can’t be completed into valid JSON, the original content is returned unchanged.

Without JSON Parser (raw streaming chunks):

Chunk 1: `{` ❌ Invalid JSON
Chunk 2: `{"name"` ❌ Invalid JSON
Chunk 3: `{"name": "John"` ❌ Invalid JSON
Chunk 4: `{"name": "John Doe"` ❌ Invalid JSON

With JSON Parser (processed chunks):

Chunk 1: `{}` ✅ Valid JSON
Chunk 2: `{"name": ""}` ✅ Valid JSON
Chunk 3: `{"name": "John"}` ✅ Valid JSON
Chunk 4: `{"name": "John Doe"}` ✅ Valid JSON
  • Function Calling: Stream tool call arguments as valid JSON throughout the response
  • Structured Data: Stream complex JSON objects (user profiles, product catalogs) progressively
  • Real-time Parsing: Enable client-side JSON parsing at each streaming step without waiting for completion
  • API Integration: Forward streaming JSON to downstream services that expect valid JSON
  • Live Updates: Update UI components with valid JSON data as it streams in
InputOutput
{"name": "John"{"name": "John"}
["apple", "banana"["apple", "banana"]
{"user": {"name": "John"{"user": {"name": "John"}}
{"message": "Hello\nWorld"{"message": "Hello\nWorld"}
"" (empty string){}
" " (whitespace only){}