Skip to content

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]

fix(anthropic/adapter): emit thinking block for reasoning_content-only streaming chunks (#29600)

Mateo Wang53a206a179e0
diff --git a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
index 02e0c562654..150f056dc81 100644
--- a/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
+++ b/litellm/llms/anthropic/experimental_pass_through/adapters/transformation.py
@@ -1510,6 +1510,17 @@ 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=""
+ )
return "text", TextBlock(type="text", text="")
diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
index 74e1e17e6d7..a81261d5ffd 100644
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/adapters/test_anthropic_experimental_pass_through_adapters_transformation.py
@@ -170,6 +170,44 @@ def test_translate_streaming_openai_chunk_to_anthropic_thinking_content_block():
}
+def test_translate_streaming_openai_chunk_to_anthropic_reasoning_content_only_content_block():
+ """OpenAI-compatible reasoning backends (vLLM/SGLang) emit ``reasoning_content``
+ without ``thinking_blocks``. The content-block classifier must still open a
+ ``thinking`` block so the matching ``thinking_delta`` stream is not emitted
+ inside a text block (which silently drops chain-of-thought for /v1/messages
+ streaming clients)."""
+ choices = [
+ StreamingChoices(
+ finish_reason=None,
+ index=0,
+ delta=Delta(
+ reasoning_content="Let me think",
+ thinking_blocks=None,
+ content=None,
+ role="assistant",
+ function_call=None,
+ tool_calls=None,
+ audio=None,
+ ),
+ logprobs=None,
+ )
+ ]
+
+ (
+ block_type,
+ content_block_start,
+ ) = LiteLLMAnthropicMessagesAdapter()._translate_streaming_openai_chunk_to_anthropic_content_block(
+ choices=choices
+ )
+
+ assert block_type == "thinking"
+ assert content_block_start == {
+ "type": "thinking",
+ "thinking": "",
+ "signature": "",
+ }
+
+
def test_translate_streaming_openai_chunk_to_anthropic_thinking_signature_block():
choices = [
StreamingChoices(
diff --git a/tests/test_litellm/proxy/test_component_allowlists.py b/tests/test_litellm/proxy/test_component_allowlists.py
index d20e1781169..ad25856b972 100644
--- a/tests/test_litellm/proxy/test_component_allowlists.py
+++ b/tests/test_litellm/proxy/test_component_allowlists.py
@@ -23,9 +23,17 @@ import sys
# Importing ``litellm.proxy.proxy_server`` runs its module-level setup, which
# reads ``DATABASE_URL`` (Prisma) and ``LITELLM_MASTER_KEY``. Tier-zero CI
# runners don't set these. We pin throwaway values before the import so the
-# test never depends on a live database or master key.
-os.environ.setdefault("DATABASE_URL", "sqlite:///:memory:")
-os.environ.setdefault("LITELLM_MASTER_KEY", "sk-test-component-allowlist")
+# test never depends on a live database or master key, then restore the prior
+# environment so the throwaway values don't leak into sibling tests sharing the
+# xdist worker (a leaked non-postgres ``DATABASE_URL`` makes DB-backed tests
+# treat a phantom database as available instead of skipping).
+_THROWAWAY_ENV = {
+ "DATABASE_URL": "sqlite:///:memory:",
+ "LITELLM_MASTER_KEY": "sk-test-component-allowlist",
+}
+_PRE_EXISTING_ENV = {key: os.environ.get(key) for key in _THROWAWAY_ENV}
+for _key, _value in _THROWAWAY_ENV.items():
+ os.environ.setdefault(_key, _value)
from fastapi.routing import Mount
@@ -38,6 +46,12 @@ from backend.routes.allowlist import BACKEND_EXACT_PATHS, BACKEND_PATH_PREFIXES
from gateway.routes.allowlist import GATEWAY_EXACT_PATHS, GATEWAY_PATH_PREFIXES
from litellm.proxy.proxy_server import app
+for _key, _previous in _PRE_EXISTING_ENV.items():
+ if _previous is None:
+ os.environ.pop(_key, None)
+ else:
+ os.environ[_key] = _previous
+
def _component_paths(routes, exact_paths, path_prefixes) -> set[str]:
"""Reproduce ``gateway.main._is_gateway_route`` / ``backend.main._is_backend_route``."""