Skip to main content

assert_valid_event_stream

Function assert_valid_event_stream 

Source
pub fn assert_valid_event_stream(
    items: &[Result<StreamedAssistantContent, CompletionError>],
    choice: &[AssistantContent],
)
Available on crate feature test-utils and not (target_os=unknown and WebAssembly) only.
Expand description

Executable stream-lifecycle validator (#2258 C1).

The invariants every normalized stream must satisfy, stated once and run over every recorded cassette and corpus fixture that drains through fixtures::drain — the langchain assert_valid_event_stream move: prose contracts scattered across N adapters become one executable artifact. Panics with the violated law.

Laws (universal — they hold for truncated and errored streams too):

  1. Terminal latch. At most one StreamedAssistantContent::Final, and no content item (text, reasoning, tool call or delta) follows it — only in-band errors and Unknown passthrough may.
  2. Text conservation. The aggregated text is exactly the concatenation of the yielded text deltas: accumulated delta content equals the payload the aggregate delivers.
  3. Completed-call conservation. Every completed tool call yielded on the stream appears in the aggregated choice exactly once, and vice versa (counts match; aggregation neither drops nor duplicates).
  4. Delta-before-completion. A completed call correlated with fragments (same internal_call_id) never precedes its own deltas.
  5. Reasoning provenance. The aggregate contains a reasoning part only if the stream yielded reasoning items; and when only deltas were yielded (no full block), the aggregated reasoning text is exactly their concatenation.