Json Processing

JSON processing utilities for LLM responses.

Provides efficient JSON completion checking and fixing for streaming JSON responses from language models.

Functions

arshai.llms.utils.json_processing.fix_incomplete_json(json_str, in_string=None, brace_depth=None, bracket_depth=None)[source]

Optimized JSON completion with single validation.

Parameters:
  • json_str (str) – JSON string to fix

  • in_string (bool) – Whether currently inside a string (if known)

  • brace_depth (int) – Current brace nesting depth (if known)

  • bracket_depth (int) – Current bracket nesting depth (if known)

Return type:

Tuple[bool, str]

Returns:

Tuple of (is_valid, fixed_json_string)

arshai.llms.utils.json_processing.is_json_complete(json_str)[source]

Efficient JSON completion checker using state machine approach. Optimized for streaming JSON parsing with minimal overhead.

Parameters:

json_str (str) – JSON string to check for completeness

Return type:

Tuple[bool, str]

Returns:

Tuple of (is_complete, fixed_json_string)