Skip to main content

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:
  1. AllRequests: Processes all streaming responses automatically
  2. 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 using PerRequest mode, the plugin checks for the context key jsonparser.EnableStreamingJSONParser with a boolean value:
  • true: Enable JSON parsing for this request
  • false: Disable JSON parsing for this request
  • Key not present: Disable JSON parsing for this request
Example:

How It Works

The plugin implements an optimized parsePartialJSON function with the following steps:
  1. Usage Check: Determines if processing should occur based on usage type and context
  2. Validates Input: First tries to parse the string as valid JSON
  3. Character Analysis: If invalid, processes the string character-by-character to track:
    • String boundaries (inside/outside quotes)
    • Escape sequences
    • Opening/closing braces and brackets
  4. Auto-Completion: Adds missing closing characters in the correct order
  5. Validation: Verifies the completed JSON is valid
  6. Fallback: Returns original content if completion fails

Memory Management

The plugin automatically manages memory by:
  1. Accumulating Content: Stores partial JSON chunks with timestamps for each request
  2. Periodic Cleanup: Runs a background goroutine that removes stale entries based on MaxAge
  3. Request Completion: Automatically clears accumulated content when requests complete successfully
  4. 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:
Without JSON Parser (raw streaming chunks):
With JSON Parser (processed chunks):

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

Testing

Run the test suite:
The tests cover:
  • 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