Skip to main content

Module json_parse

Module json_parse 

Source
Expand description

Robust JSON parsing utilities

Handles malformed JSON from streaming LLM responses:

  • Escapes raw control characters inside strings
  • Doubles backslashes before invalid escape characters
  • Repairs incomplete JSON from streaming responses

Functions§

extract_error_message
Extract the error message from an assistant message that may have malformed JSON in its error field.
parse_json_with_repair
Parse JSON with automatic repair of common malformations.
parse_sse_data
Parse a streaming SSE data field as JSON, with robust error handling. Returns None for non-data lines or unparseable content.
parse_streaming_json
Attempts to parse potentially incomplete JSON from a streaming response.
repair_json
Repairs malformed JSON string literals by: