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
refactor(anthropic-adapter): exhaustively match delta payload lookup
mateo-berribf3a1f47dd1e
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.pyindex 5228f656dba..f02333c34c8 100644--- a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py@@ -13,14 +13,18 @@ from typing import (List,Literal,Optional,+ get_args,)+from typing_extensions import assert_never+from litellm._logging import verbose_loggerfrom litellm._uuid import uuidfrom litellm.types.llms.anthropic import (AppliedEdit,CompactionBlock,ContextManagementResponse,+ StreamingContentBlockDeltaType,UsageDelta,UsageIteration,)@@ -30,6 +34,23 @@ if TYPE_CHECKING:from litellm.types.utils import ModelResponseStream+_STREAMING_DELTA_TYPES = frozenset(get_args(StreamingContentBlockDeltaType))+++def _delta_payload_field(delta_type: StreamingContentBlockDeltaType) -> str:+ match delta_type:+ case "text_delta":+ return "text"+ case "input_json_delta":+ return "partial_json"+ case "thinking_delta":+ return "thinking"+ case "signature_delta":+ return "signature"+ case _:+ assert_never(delta_type)++class _CombinedChunkSplitter:"""Splits a streaming chunk that carries BOTH response content and a@@ -837,6 +858,12 @@ class AnthropicStreamWrapper(AdapterCompletionStreamWrapper):so the trigger chunk's delta must be re-queued or the first token ofthe new block (the first non-empty text/thinking delta, or bundledtool arguments) is silently dropped.++ Delta types outside ``StreamingContentBlockDeltaType`` — the closed+ set the translate layer can produce — are treated as empty. The+ per-type payload lookup is exhaustively matched against that set in+ ``_delta_payload_field``, so extending the translate layer with a new+ delta type fails type-checking here until it is handled."""if processed_chunk.get("type") != "content_block_delta":return False@@ -844,15 +871,9 @@ class AnthropicStreamWrapper(AdapterCompletionStreamWrapper):if not isinstance(delta, dict):return Falsedelta_type = delta.get("type")- if delta_type == "text_delta":- return bool(delta.get("text"))- if delta_type == "input_json_delta":- return bool(delta.get("partial_json"))- if delta_type == "thinking_delta":- return bool(delta.get("thinking"))- if delta_type == "signature_delta":- return bool(delta.get("signature"))- return False+ if delta_type not in _STREAMING_DELTA_TYPES:+ return False+ return bool(delta.get(_delta_payload_field(delta_type)))def _should_start_new_content_block(self, chunk: "ModelResponseStream") -> bool:"""diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.pyindex 4c981dd36b3..cd75eed2e6e 100644--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py@@ -104,6 +104,7 @@ from litellm.types.llms.anthropic import (ContextManagementResponse,MessageBlockDelta,MessageDelta,+ StreamingContentBlockDeltaType,UsageDelta,UsageIteration,)@@ -1423,7 +1424,7 @@ class LiteLLMAnthropicMessagesAdapter:def _translate_streaming_openai_chunk_to_anthropic(self, choices: List[Union[OpenAIStreamingChoice, StreamingChoices]]) -> Tuple[- Literal["text_delta", "input_json_delta", "thinking_delta", "signature_delta"],+ StreamingContentBlockDeltaType,Union[ContentTextBlockDelta,ContentJsonBlockDelta,diff --git a/litellm/types/llms/anthropic.py b/litellm/types/llms/anthropic.pyindex aa9f4dccbd1..c24d072217a 100644--- a/litellm/types/llms/anthropic.py+++ b/litellm/types/llms/anthropic.py@@ -439,6 +439,9 @@ class ContentThinkingSignatureBlockDelta(TypedDict):signature: str+StreamingContentBlockDeltaType = Literal["text_delta", "input_json_delta", "thinking_delta", "signature_delta"]++class ContentBlockDelta(TypedDict):type: Literal["content_block_delta"]index: int