From 35c54078e244cdb00d39c009b248e22b2f659cb4 Mon Sep 17 00:00:00 2001 From: jesco-absolut Date: Fri, 31 Jul 2026 23:20:45 -0400 Subject: [PATCH 1/3] fix(anthropic): preserve reasoning fallback --- .../pass_through/adapters/transformation.py | 34 ++++++++--------- .../test_streaming_iterator_first_delta.py | 37 +++++++++++++++++++ 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/transformation.py b/litellm/llms/anthropic/pass_through/adapters/transformation.py index 2ee23852894..20280fd6d99 100644 --- a/litellm/llms/anthropic/pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/pass_through/adapters/transformation.py @@ -1656,8 +1656,8 @@ class LiteLLMAnthropicMessagesAdapter: choice.delta ) is not None: return "text", TextBlock(type="text", text="") - elif isinstance(choice, StreamingChoices) and hasattr(choice.delta, "thinking_blocks"): - thinking_blocks = choice.delta.thinking_blocks or [] + elif isinstance(choice, StreamingChoices): + thinking_blocks = getattr(choice.delta, "thinking_blocks", None) or [] if len(thinking_blocks) > 0: thinking_block = thinking_blocks[0] if thinking_block["type"] == "thinking": @@ -1670,13 +1670,8 @@ class LiteLLMAnthropicMessagesAdapter: return "thinking", ChatCompletionThinkingBlock( type="thinking", thinking=thinking, signature=signature ) - # OpenAI-compatible reasoning backends (e.g. vLLM/SGLang reasoning - # parsers) populate ``reasoning_content`` without ``thinking_blocks``. - # ``Delta`` deletes the ``thinking_blocks`` attribute when unset, so the - # branch above is skipped entirely; open a ``thinking`` block here so the - # matching ``thinking_delta`` stream is not emitted into a text block. - elif isinstance(choice, StreamingChoices) and getattr(choice.delta, "reasoning_content", None): - return "thinking", ChatCompletionThinkingBlock(type="thinking", thinking="", signature="") + if getattr(choice.delta, "reasoning_content", None): + return "thinking", ChatCompletionThinkingBlock(type="thinking", thinking="", signature="") return "text", TextBlock(type="text", text="") @@ -1702,8 +1697,10 @@ class LiteLLMAnthropicMessagesAdapter: for tool in choice.delta.tool_calls: if tool.function is not None and tool.function.arguments is not None: partial_json = (partial_json or "") + tool.function.arguments - elif isinstance(choice, StreamingChoices) and hasattr(choice.delta, "thinking_blocks"): - thinking_blocks = choice.delta.thinking_blocks or [] + elif isinstance(choice, StreamingChoices): + thinking_blocks = getattr(choice.delta, "thinking_blocks", None) or [] + choice_reasoning_content = "" + choice_reasoning_signature = "" if len(thinking_blocks) > 0: for thinking_block in thinking_blocks: if thinking_block["type"] == "thinking": @@ -1713,13 +1710,14 @@ class LiteLLMAnthropicMessagesAdapter: assert isinstance(thinking, str) assert isinstance(signature, str) - reasoning_content += thinking - reasoning_signature += signature - # Handle reasoning_content when thinking_blocks is not present - # This handles providers like OpenRouter that return reasoning_content - elif isinstance(choice, StreamingChoices) and hasattr(choice.delta, "reasoning_content"): - if choice.delta.reasoning_content is not None: - reasoning_content += choice.delta.reasoning_content + choice_reasoning_content += thinking + choice_reasoning_signature += signature + if choice_reasoning_content or choice_reasoning_signature: + reasoning_content += choice_reasoning_content + reasoning_signature += choice_reasoning_signature + elif hasattr(choice.delta, "reasoning_content"): + if choice.delta.reasoning_content is not None: + reasoning_content += choice.delta.reasoning_content if partial_json is not None: return "input_json_delta", ContentJsonBlockDelta(type="input_json_delta", partial_json=partial_json) diff --git a/tests/unit/llms/anthropic/pass_through/adapters/test_streaming_iterator_first_delta.py b/tests/unit/llms/anthropic/pass_through/adapters/test_streaming_iterator_first_delta.py index 92234282bb2..afb45153991 100644 --- a/tests/unit/llms/anthropic/pass_through/adapters/test_streaming_iterator_first_delta.py +++ b/tests/unit/llms/anthropic/pass_through/adapters/test_streaming_iterator_first_delta.py @@ -699,6 +699,43 @@ def test_reasoning_content_first_stream_opens_thinking_block_at_index_zero_sync( _assert_thinking_first_block_opens_at_index_zero(_drain_sync(wrapper)) +@pytest.mark.parametrize( + "thinking_blocks", + [ + pytest.param([], id="empty"), + pytest.param( + [{"type": "redacted_thinking", "data": "redacted"}], + id="redacted-only", + ), + ], +) +def test_reasoning_content_falls_back_without_usable_thinking_blocks_sync( + thinking_blocks, +): + chunks = [ + _make_chunk( + Delta( + content=None, + reasoning_content="fallback thought", + thinking_blocks=thinking_blocks, + ) + ), + _make_chunk(Delta(content=None), finish_reason="stop"), + ] + wrapper = AnthropicStreamWrapper(completion_stream=iter(chunks), model="claude-x") + events = _drain_sync(wrapper) + + starts = [ + (event["index"], event["content_block"]["type"]) + for event in events + if event.get("type") == "content_block_start" + ] + assert starts == [(0, "thinking")] + assert _thinking_deltas(events) == ["fallback thought"] + assert _text_deltas(events) == [] + _assert_deltas_match_their_block_type(events) + + def _blank_lead_chunks() -> List[MagicMock]: return [ _make_chunk(Delta(content=None)), From 7f1efd18befd086907c2687861df4f31f95c3efa Mon Sep 17 00:00:00 2001 From: jesco-absolut Date: Sat, 1 Aug 2026 16:54:28 -0400 Subject: [PATCH 2/3] fix(anthropic): reduce reasoning fallback complexity --- .../pass_through/adapters/transformation.py | 45 ++++++++++--------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/transformation.py b/litellm/llms/anthropic/pass_through/adapters/transformation.py index 20280fd6d99..25711e7e87f 100644 --- a/litellm/llms/anthropic/pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/pass_through/adapters/transformation.py @@ -1679,6 +1679,28 @@ class LiteLLMAnthropicMessagesAdapter: translate_streaming_openai_chunk_to_anthropic_content_block ) + @staticmethod + def _streaming_reasoning_fields(choice: StreamingChoices) -> tuple[str, str]: + reasoning_content = "" + reasoning_signature = "" + thinking_blocks = getattr(choice.delta, "thinking_blocks", None) or [] + for thinking_block in thinking_blocks: + if thinking_block["type"] == "thinking": + thinking = thinking_block.get("thinking") or "" + signature = thinking_block.get("signature") or "" + + assert isinstance(thinking, str) + assert isinstance(signature, str) + + reasoning_content += thinking + reasoning_signature += signature + + if reasoning_content or reasoning_signature: + return reasoning_content, reasoning_signature + + fallback = getattr(choice.delta, "reasoning_content", None) + return fallback or "", "" + def _translate_streaming_openai_chunk_to_anthropic( self, choices: list[OpenAIStreamingChoice | StreamingChoices] ) -> tuple[ @@ -1698,26 +1720,9 @@ class LiteLLMAnthropicMessagesAdapter: if tool.function is not None and tool.function.arguments is not None: partial_json = (partial_json or "") + tool.function.arguments elif isinstance(choice, StreamingChoices): - thinking_blocks = getattr(choice.delta, "thinking_blocks", None) or [] - choice_reasoning_content = "" - choice_reasoning_signature = "" - if len(thinking_blocks) > 0: - for thinking_block in thinking_blocks: - if thinking_block["type"] == "thinking": - thinking = thinking_block.get("thinking") or "" - signature = thinking_block.get("signature") or "" - - assert isinstance(thinking, str) - assert isinstance(signature, str) - - choice_reasoning_content += thinking - choice_reasoning_signature += signature - if choice_reasoning_content or choice_reasoning_signature: - reasoning_content += choice_reasoning_content - reasoning_signature += choice_reasoning_signature - elif hasattr(choice.delta, "reasoning_content"): - if choice.delta.reasoning_content is not None: - reasoning_content += choice.delta.reasoning_content + choice_reasoning_content, choice_reasoning_signature = self._streaming_reasoning_fields(choice) + reasoning_content += choice_reasoning_content + reasoning_signature += choice_reasoning_signature if partial_json is not None: return "input_json_delta", ContentJsonBlockDelta(type="input_json_delta", partial_json=partial_json) From 02422da2e83b71e271a40955d2caa2a912f6c499 Mon Sep 17 00:00:00 2001 From: jesco-absolut Date: Sat, 10 Oct 2026 02:59:58 -0400 Subject: [PATCH 3/3] fix(anthropic): type reasoning fallback fields --- .../pass_through/adapters/transformation.py | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/litellm/llms/anthropic/pass_through/adapters/transformation.py b/litellm/llms/anthropic/pass_through/adapters/transformation.py index 25711e7e87f..b1f0df26967 100644 --- a/litellm/llms/anthropic/pass_through/adapters/transformation.py +++ b/litellm/llms/anthropic/pass_through/adapters/transformation.py @@ -1657,7 +1657,9 @@ class LiteLLMAnthropicMessagesAdapter: ) is not None: return "text", TextBlock(type="text", text="") elif isinstance(choice, StreamingChoices): - thinking_blocks = getattr(choice.delta, "thinking_blocks", None) or [] + thinking_blocks = ( + choice.delta.thinking_blocks if hasattr(choice.delta, "thinking_blocks") else None + ) or [] if len(thinking_blocks) > 0: thinking_block = thinking_blocks[0] if thinking_block["type"] == "thinking": @@ -1681,24 +1683,18 @@ class LiteLLMAnthropicMessagesAdapter: @staticmethod def _streaming_reasoning_fields(choice: StreamingChoices) -> tuple[str, str]: - reasoning_content = "" - reasoning_signature = "" - thinking_blocks = getattr(choice.delta, "thinking_blocks", None) or [] - for thinking_block in thinking_blocks: - if thinking_block["type"] == "thinking": - thinking = thinking_block.get("thinking") or "" - signature = thinking_block.get("signature") or "" - - assert isinstance(thinking, str) - assert isinstance(signature, str) - - reasoning_content += thinking - reasoning_signature += signature + thinking_blocks: Final = choice.delta.thinking_blocks if hasattr(choice.delta, "thinking_blocks") else None + reasoning_content: Final = "".join( + block.get("thinking") or "" for block in thinking_blocks or () if block["type"] == "thinking" + ) + reasoning_signature: Final = "".join( + block.get("signature") or "" for block in thinking_blocks or () if block["type"] == "thinking" + ) if reasoning_content or reasoning_signature: return reasoning_content, reasoning_signature - fallback = getattr(choice.delta, "reasoning_content", None) + fallback: Final = choice.delta.reasoning_content if hasattr(choice.delta, "reasoning_content") else None return fallback or "", "" def _translate_streaming_openai_chunk_to_anthropic(