Overview
When using AI providers that stream JSON responses, the individual chunks often contain incomplete JSON that cannot be parsed directly. This plugin automatically detects and fixes partial JSON chunks by adding the necessary closing braces, brackets, and quotes to make them valid JSON.Features
- Automatic JSON Completion: Detects partial JSON and adds missing closing characters
- Streaming Only: Processes only streaming responses (non-streaming responses are ignored)
- Flexible Usage Modes: Supports two usage types for different deployment scenarios
- Safe Fallback: Returns original content if JSON cannot be fixed
- Memory Leak Prevention: Automatic cleanup of stale accumulated content with configurable intervals
- Zero Dependencies: Only depends on Go’s standard library
Usage
Usage Types
The plugin supports two usage types:- AllRequests: Processes all streaming responses automatically
- PerRequest: Processes only when explicitly enabled via request context
PerRequest Mode
Configuration
Default Values
- CleanupInterval: 5 minutes (how often to run cleanup)
- MaxAge: 30 minutes (how old entries can be before cleanup)
- Usage: Must be specified (AllRequests or PerRequest)
Context Key for PerRequest Mode
When usingPerRequest mode, the plugin checks for the context key jsonparser.EnableStreamingJSONParser with a boolean value:
true: Enable JSON parsing for this requestfalse: Disable JSON parsing for this request- Key not present: Disable JSON parsing for this request
How It Works
The plugin implements an optimizedparsePartialJSON function with the following steps:
- Usage Check: Determines if processing should occur based on usage type and context
- Validates Input: First tries to parse the string as valid JSON
- Character Analysis: If invalid, processes the string character-by-character to track:
- String boundaries (inside/outside quotes)
- Escape sequences
- Opening/closing braces and brackets
- Auto-Completion: Adds missing closing characters in the correct order
- Validation: Verifies the completed JSON is valid
- Fallback: Returns original content if completion fails
Memory Management
The plugin automatically manages memory by:- Accumulating Content: Stores partial JSON chunks with timestamps for each request
- Periodic Cleanup: Runs a background goroutine that removes stale entries based on
MaxAge - Request Completion: Automatically clears accumulated content when requests complete successfully
- Configurable Intervals: Allows customization of cleanup frequency and retention periods
Real-Life Streaming Example
Here’s a practical example showing how the JSON parser plugin fixes broken JSON chunks in streaming responses: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
| 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) | {} |
Testing
Run the test suite:- Plugin interface compliance
- Both usage types (AllRequests and PerRequest)
- Context-based enabling/disabling
- Streaming responses only (non-streaming responses are ignored)
- Various JSON completion scenarios
- Edge cases and error conditions
- Memory cleanup functionality with real and simulated requests
- Configuration options and default values

