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):
- Terminal latch. At most one
StreamedAssistantContent::Final, and no content item (text, reasoning, tool call or delta) follows it — only in-band errors andUnknownpassthrough may. - Text conservation. The aggregated text is exactly the concatenation of the yielded text deltas: accumulated delta content equals the payload the aggregate delivers.
- 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).
- Delta-before-completion. A completed call correlated with
fragments (same
internal_call_id) never precedes its own deltas. - 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.