JSON Parser
Overview
Section titled “Overview”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.
What You Get
Section titled “What You Get”- 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.
Enabling it
Section titled “Enabling it”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 } }}| Setting | Default | Description |
|---|---|---|
usage | - | all_requests processes every streaming response; per_request only processes requests that opt in |
cleanup_interval_minutes | 5 | How often accumulated per-request state is cleaned up |
max_age_minutes | 30 | How 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.
Behavior
Section titled “Behavior”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 JSONChunk 2: `{"name"` ❌ Invalid JSONChunk 3: `{"name": "John"` ❌ Invalid JSONChunk 4: `{"name": "John Doe"` ❌ Invalid JSONWith JSON Parser (processed chunks):
Chunk 1: `{}` ✅ Valid JSONChunk 2: `{"name": ""}` ✅ Valid JSONChunk 3: `{"name": "John"}` ✅ Valid JSONChunk 4: `{"name": "John Doe"}` ✅ Valid JSONUse Cases
Section titled “Use Cases”- 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
Example Transformations
Section titled “Example Transformations”| Input | Output |
|---|---|
{"name": "John" | {"name": "John"} |
["apple", "banana" | ["apple", "banana"] |
{"user": {"name": "John" | {"user": {"name": "John"}} |
{"message": "Hello\nWorld" | {"message": "Hello\nWorld"} |
"" (empty string) | {} |
" " (whitespace only) | {} |