The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(anthropic): tolerate message_delta chunks without usage in streams
kerry4657d43fb999
diff --git a/litellm/llms/anthropic/chat/handler.py b/litellm/llms/anthropic/chat/handler.pyindex 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, containerdiff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.pyindex 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.pyindex 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 = [