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-adapter): drop empty content_block_delta events
mateo-berri0f9d593d29ab
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.pyindex 44c367ee805..5228f656dba 100644--- a/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/streaming_iterator.py@@ -458,12 +458,15 @@ class AnthropicStreamWrapper(AdapterCompletionStreamWrapper):# 3. If the trigger chunk carries delta content, queue it# so the first delta of the new block is not silently dropped.- if self._trigger_delta_has_content(processed_chunk):+ if self._delta_has_content(processed_chunk):self.chunk_queue.append(processed_chunk)self.sent_content_block_finish = Falsereturn self.chunk_queue.popleft()+ if processed_chunk["type"] == "content_block_delta" and not self._delta_has_content(processed_chunk):+ continue+if processed_chunk["type"] == "message_delta" and self.sent_content_block_finish is False:# Queue both the content_block_stop and the message_deltaself.chunk_queue.append(@@ -670,13 +673,18 @@ class AnthropicStreamWrapper(AdapterCompletionStreamWrapper):# 3. If the trigger chunk carries delta content, queue it# so the first delta of the new block is not silently dropped.- if self._trigger_delta_has_content(processed_chunk):+ if self._delta_has_content(processed_chunk):self.chunk_queue.append(processed_chunk)# Reset state for new blockself.sent_content_block_finish = Falsereturn self.chunk_queue.popleft()+ if processed_chunk["type"] == "content_block_delta" and not self._delta_has_content(+ processed_chunk+ ):+ continue+if processed_chunk["type"] == "message_delta" and self.sent_content_block_finish is False:# Queue both the content_block_stop and the holding chunkself.chunk_queue.append(@@ -808,15 +816,22 @@ class AnthropicStreamWrapper(AdapterCompletionStreamWrapper):self.current_content_block_index += 1@staticmethod- def _trigger_delta_has_content(processed_chunk: Dict[str, Any]) -> bool:- """Return True if a translated trigger chunk carries a non-empty- ``content_block_delta`` payload that must be re-emitted after a- block transition.-- When an upstream chunk both *triggers* a new content block (its type- differs from the active block) and *carries* delta content, that- content belongs to the new block. The synthesized- ``content_block_start`` only ever carries an empty body — see+ def _delta_has_content(processed_chunk: Dict[str, Any]) -> bool:+ """Return True if a translated chunk carries a non-empty+ ``content_block_delta`` payload.++ Gates every ``content_block_delta`` emission. An empty delta carries+ no information, and the translate fallback types empty deltas as+ ``text_delta`` regardless of the active block's type — emitting one+ into an open ``thinking`` block (e.g. Bedrock Converse sends an empty+ reasoning delta mid-block) crashes strict Anthropic SDK clients with+ "Content block is not a text block".++ Also gates re-emission after a block transition: when an upstream+ chunk both *triggers* a new content block (its type differs from the+ active block) and *carries* delta content, that content belongs to+ the new block. The synthesized ``content_block_start`` only ever+ carries an empty body — see``_translate_streaming_openai_chunk_to_anthropic_content_block``,which returns an empty ``TextBlock``/``ToolUseBlock``/thinking block —so the trigger chunk's delta must be re-queued or the first token ofdiff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_first_delta.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_first_delta.pyindex d9d9474d6f3..6ed79763753 100644--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_first_delta.py+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_streaming_iterator_first_delta.py@@ -12,6 +12,13 @@ silently dropped — e.g. text resuming after a tool call started from the secontoken ("The weather is nice." was lost, "Hi" rendered as ""). Bundled``input_json_delta`` tool arguments were already preserved and must staypreserved, and empty trigger deltas must not produce spurious events.++Also covers the inverse regression: a chunk whose translated delta carries no+payload must not be emitted at all. The translate fallback types empty deltas+as ``text_delta`` regardless of the active block, so an empty reasoning delta+mid-thinking-block (Bedrock Converse sends these) used to emit ``text_delta``+into an open ``thinking`` block, crashing Anthropic SDK clients (Claude Code)+with "Content block is not a text block"."""import os@@ -51,6 +58,13 @@ def _make_chunk(delta: Delta, finish_reason: Optional[str] = None) -> MagicMock:return chunk+def _thinking_chunk(thinking: str, signature: str = "") -> MagicMock:+ block = {"type": "thinking", "thinking": thinking}+ if signature:+ block["signature"] = signature+ return _make_chunk(Delta(content=None, thinking_blocks=[block]))++def _tool_chunk(call_id: str, name: Optional[str], arguments: Optional[str]) -> MagicMock:@@ -109,6 +123,47 @@ def _input_json_deltas(events: List[dict]) -> List[str]:]+def _thinking_deltas(events: List[dict]) -> List[str]:+ return [+ e["delta"]["thinking"]+ for e in events+ if e.get("type") == "content_block_delta"+ and e["delta"].get("type") == "thinking_delta"+ ]+++def _signature_deltas(events: List[dict]) -> List[str]:+ return [+ e["delta"]["signature"]+ for e in events+ if e.get("type") == "content_block_delta"+ and e["delta"].get("type") == "signature_delta"+ ]+++_DELTA_TYPES_PER_BLOCK_TYPE = {+ "text": {"text_delta"},+ "thinking": {"thinking_delta", "signature_delta"},+ "tool_use": {"input_json_delta"},+}+++def _assert_deltas_match_their_block_type(events: List[dict]) -> None:+ """Enforce the invariant the Anthropic SDK enforces client-side: every+ ``content_block_delta`` must be of a type valid for the block opened by+ the most recent ``content_block_start`` at the same index.+ """+ block_types = {}+ for event in events:+ if event.get("type") == "content_block_start":+ block_types[event["index"]] = event["content_block"]["type"]+ if event.get("type") == "content_block_delta":+ block_type = block_types[event["index"]]+ assert event["delta"]["type"] in _DELTA_TYPES_PER_BLOCK_TYPE[block_type], (+ f"{event['delta']['type']} emitted into a {block_type} block: {event}"+ )++def test_held_stop_reason_usage_merge_preserves_openai_cache_token_details():"""OpenAI-compatible usage chunks carry cache reads in prompt_tokens_details."""wrapper = AnthropicStreamWrapper(completion_stream=iter([]), model="claude-x")@@ -332,11 +387,70 @@ def test_bundled_tool_args_on_transition_still_preserved_sync():({"type": "content_block_delta", "delta": None}, False),],)-def test_trigger_delta_has_content_branches(processed_chunk, expected):- """Directly exercise the re-emit predicate across all delta types and the+def test_delta_has_content_branches(processed_chunk, expected):+ """Directly exercise the emission predicate across all delta types and theempty/malformed guards, so the helper's behavior is pinned independently ofupstream chunk-translation details."""- assert (- AnthropicStreamWrapper._trigger_delta_has_content(processed_chunk) is expected+ assert AnthropicStreamWrapper._delta_has_content(processed_chunk) is expected+++def _empty_reasoning_delta_mid_thinking_chunks() -> List[MagicMock]:+ return [+ _thinking_chunk("Let me think"),+ _thinking_chunk(""),+ _thinking_chunk("", signature="sig123"),+ _make_chunk(Delta(content="Hello")),+ _make_chunk(Delta(content=None), finish_reason="stop"),+ ]+++def _assert_empty_reasoning_delta_suppressed(events: List[dict]) -> None:+ _assert_deltas_match_their_block_type(events)+ assert _thinking_deltas(events) == ["Let me think"]+ assert _signature_deltas(events) == ["sig123"]+ assert _text_deltas(events) == ["Hello"]+++def test_empty_reasoning_delta_mid_thinking_block_is_suppressed_sync():+ """Bedrock Converse repro: an empty reasoning delta arriving inside an open+ thinking block used to be emitted as ``text_delta {"text": ""}`` at the+ thinking block's index (no block transition), which crashes Claude Code's+ Anthropic SDK with "Content block is not a text block". It must be dropped,+ while the surrounding thinking/signature/text deltas all still flow.+ """+ wrapper = AnthropicStreamWrapper(+ completion_stream=iter(_empty_reasoning_delta_mid_thinking_chunks()),+ model="claude-x",+ )+ _assert_empty_reasoning_delta_suppressed(_drain_sync(wrapper))+++async def test_empty_reasoning_delta_mid_thinking_block_is_suppressed_async():+ """Async twin of the Bedrock Converse repro — the proxy serves the async+ iterator, so the skip must exist on that path too.+ """+ wrapper = AnthropicStreamWrapper(+ completion_stream=_AsyncStream(_empty_reasoning_delta_mid_thinking_chunks()),+ model="claude-x",)+ _assert_empty_reasoning_delta_suppressed(await _drain_async(wrapper))+++def test_empty_content_chunk_mid_text_block_is_suppressed_sync():+ """An empty-content chunk arriving mid-text-block (no transition) used to+ emit a pointless ``text_delta {"text": ""}``; it must be dropped without+ affecting the surrounding text deltas.+ """+ chunks = [+ _make_chunk(Delta(content="Hi")),+ _make_chunk(Delta(content="")),+ _make_chunk(Delta(content=" there")),+ _make_chunk(Delta(content=None), finish_reason="stop"),+ ]+ wrapper = AnthropicStreamWrapper(completion_stream=iter(chunks), model="claude-x")+ events = _drain_sync(wrapper)++ assert _text_deltas(events) == ["Hi", " there"]+ _assert_deltas_match_their_block_type(events)