fix(anthropic): stop retaining snapshot events from messages.stream() #21667
All Vouch review confidence checks passed
Residual risk 0.23 = Change Risk (0.40) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.40 · 293 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.340
Real PR Conversation Timeline
1 comments
"Review active on #21667"
general
weight: 0.35
mansur-akhmadi
Description
Fixes #21666
On the
messages.stream()/beta.messages.stream()path the SDK yields derived events (text,input_json,thinking,signature,citation, ...) next to the raw stream events. The derived ones carry the cumulative snapshot of the output so far.AnthropicStreamHandlerstored every event until the stream finished, so retained memory grew quadratically with the response length._construct_messageonly reads the raw event types and ignored the rest anyway.The handlers now keep only chunks whose type
_construct_messagehandles. The transformation table moves from inside_extract_from_chunk(it was rebuilt on every chunk) to a module-level_CHUNK_TRANSFORMATIONS, and the filter uses its keys, so the two stay in sync. Sync and async handlers share one_retain_chunkhelper.Same mocked stream as in the issue (3000 deltas of 10 chars, anthropic 2.7): before, 6005 chunks retained and 51 MB traced at the end of the stream; after, 3004 chunks and 4.7 MB.
Testing
New tests in
tests/contrib/anthropic/test_anthropic_llmobs.pyon the existing cassettes:input_json)Each iterates the events and checks that the derived types were yielded to the caller but not retained, that raw
content_block_deltaevents were retained, and that the LLM Observability output (text, tool call arguments, reasoning) is unchanged. All three fail on main.test_anthropic.pyandtest_anthropic_llmobs.pypass against the latest (95857972cb3e, anthropic 0.117) and oldest (65dc301ce378, anthropic 0.28) locks.scripts/lintstyle, typing, spelling, security and suitespec checks pass on the changed files.Risks
Low. Only events that
_construct_messageignored are dropped, so span output and token counts don't change. If a future handler is added to_CHUNK_TRANSFORMATIONS, its events are retained automatically.Additional Notes
The handler still keeps the raw events (one per token) until the end of the stream. Folding each event into the message as it arrives would remove that too, but it's a bigger change to the handler, so I kept this one minimal. When AI Guard stream analysis is enabled, its own buffer still holds all events. That is a separate code path and isn't changed here.