Skip to main content

Module partial_json

Module partial_json 

Source
Expand description

Streaming partial-JSON parser for speculative tool-call dispatch.

The Anthropic SSE tool-use stream emits a single JSON object across many input_json_delta events. Standard JSON parsers (including serde_json::StreamDeserializer) wait for the closing brace before yielding any value — providing no benefit for speculative dispatch.

PartialJsonParser is a ~120-line brace/string/escape state machine that accumulates delta strings and extracts top-level leaf keys whose values have been fully closed (primitives, fully closed nested objects/arrays). When all required fields of a tool’s input_schema are present, the engine can speculatively dispatch the tool call without waiting for ToolUseStop.

§Invariants

  • Escape state: the escape flag is set after a literal \ inside a string and cleared after the following character, regardless of what that character is. Multi-byte escape sequences (e.g. \uXXXX) are not individually validated; the parser only tracks structural JSON tokens.
  • Mid-array edge case: array values at depth > 1 are treated as opaque; their contents are never surfaced as known_leaves. Only top-level keys (depth == 1) whose values close cleanly are included.
  • No allocation on malformed input: Malformed is returned immediately when a structural invariant is violated; the buffer is not reallocated.

Structs§

PartialJsonParser
Streaming structural parser for partial Anthropic SSE tool-input JSON.

Enums§

PrefixState
Result of feeding accumulated JSON delta bytes to PartialJsonParser::push.