AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

`ChatAnthropic(Stream_usage=False)` Also Drops `Stop_reason`, `Stop_sequence` And `Model_name` From `Response_metadata`, Not Just Usage Metadata

When `stream_usage=False` is set on `ChatAnthropic`, streaming responses lose non-usage metadata fields (`stop_reason`, `stop_sequence`, `model_name`) because `stream_usage` incorrectly gates the event dispatch branches that populate those fields. This makes truncated or otherwise terminated streams indistinguishable from complete ones, silently breaking downstream logic that relies on `stop_reason`.

highConfidence 95%Langchain-AnthropicAffected V1.5.6

Origin Analysis

In `ChatAnthropic._make_message_chunk_from_anthropic_event`, `stream_usage` is used as part of the event-dispatch conditions (`if event.type == "message_start" and stream_usage:` and `elif event.type == "message_delta" and stream_usage:`). This means the entire branches that capture `model_name` (from `message_start`) and `stop_reason`, `stop_sequence`, and other metadata (from `message_delta`) are skipped when `stream_usage=False`, instead of only suppressing the usage payload.
1. Create a `ChatAnthropic` instance with `stream_usage=False` and `max_tokens=1`. 2. Stream a prompt that gets truncated by `max_tokens`. 3. Aggregate the streamed chunks and inspect `response_metadata`. 4. Observe that `stop_reason` and `model_name` are `None` or missing, while the same call with `stream_usage=True` or `invoke()` includes them.

Fixing Code Block

Edge Case Audit

Existing user code that previously tolerated missing `stop_reason` or `model_name` when `stream_usage=False` may now encounter non-None values and need adjustment. Multi-threaded or async streaming must ensure no shared mutable state is introduced if the chunk construction logic is modified. Rollback: if this change causes regressions, revert to `langchain-anthropic==1.5.6`; however, be aware that the bug persists in that version. Add unit and integration tests to verify that `stop_reason`, `stop_sequence`, and `model_name` appear in `response_metadata` with `stream_usage=False` while `usage_metadata` remains `None`.

Ecosystem Topology