From 4657d43fb999972bc9c77f65a4ee5a40bb68b44c Mon Sep 17 00:00:00 2001 From: kerry Date: Wed, 16 Sep 2026 00:27:09 +0000 Subject: [PATCH] fix(anthropic): tolerate message_delta chunks without usage in streams Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/llms/anthropic/chat/handler.py | 4 +++- litellm/types/llms/anthropic.py | 2 +- .../anthropic/chat/test_anthropic_chat_handler.py | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/litellm/llms/anthropic/chat/handler.py b/litellm/llms/anthropic/chat/handler.py index 5d3ae444b42..4dd0deeb62b 100644 --- a/litellm/llms/anthropic/chat/handler.py +++ b/litellm/llms/anthropic/chat/handler.py @@ -1167,7 +1167,9 @@ class ModelResponseIterator: # (matches OpenAI behavior and non-streaming Anthropic implementation) if self.converted_response_format_tool: finish_reason = "stop" - usage: Final = self._handle_usage(anthropic_usage_chunk=message_delta["usage"]) + usage: Final = ( + self._handle_usage(anthropic_usage_chunk=message_delta["usage"]) if "usage" in message_delta else None + ) container: Final = message_delta["delta"].get("container") return finish_reason, usage, container diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.py index d56ada07ed5..ecb8dbb2502 100644 --- a/litellm/types/llms/anthropic.py +++ b/litellm/types/llms/anthropic.py @@ -586,7 +586,7 @@ class MessageBlockDelta(TypedDict): type: Literal["message_delta"] delta: MessageDelta - usage: UsageDelta + usage: NotRequired[UsageDelta] context_management: NotRequired[ContextManagementResponse] diff --git a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py index 83201aef143..ac05cdcf251 100644 --- a/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py +++ b/tests/test_litellm/llms/anthropic/chat/test_anthropic_chat_handler.py @@ -579,6 +579,21 @@ def test_text_only_streaming_has_index_zero(): ), f"Expected index=0, got {parsed.choices[0].index}" +def test_message_delta_without_usage_returns_chunk_with_no_usage(): + """A message_delta event may carry no usage field; it must not raise.""" + iterator = ModelResponseIterator(None, sync_stream=True) + + model_response = iterator.chunk_parser( + { + "type": "message_delta", + "delta": {"stop_reason": "end_turn", "stop_sequence": None}, + } + ) + + assert model_response.choices[0].finish_reason == "stop" + assert model_response.usage is None + + def test_streaming_thinking_deltas_count_reasoning_tokens_in_usage(): """Anthropic streaming usage should account for emitted thinking deltas.""" chunks = [