Skip to main content

parse_streaming_json

Function parse_streaming_json 

Source
pub fn parse_streaming_json(s: &str) -> Value
Expand description

Parse potentially incomplete JSON from streaming tool calls.

During streaming, tool call arguments arrive incrementally. This function attempts to parse the partial JSON, returning an empty object if parsing fails.

§Strategy

  1. Try parsing as complete JSON
  2. Try adding various closing brackets/braces
  3. Fall back to empty object

§Example

use alchemy_llm::utils::json_parse::parse_streaming_json;
use serde_json::json;

let partial = r#"{"name": "test", "value": 42"#;
let result = parse_streaming_json(partial);
assert_eq!(result, json!({"name": "test", "value": 42}));

// Complete JSON works too
let complete = r#"{"name": "test"}"#;
let result = parse_streaming_json(complete);
assert_eq!(result, json!({"name": "test"}));