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: make TextCompletionStreamWrapper conversion retain reasoning_content (#12377)

Adam Holmberg38278d9583b5
diff --git a/litellm/utils.py b/litellm/utils.py
index 793acfa9634..3ac19ca1a82 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -5712,7 +5712,9 @@ class TextCompletionStreamWrapper:
chunk, Choices
): # chunk should always be of type StreamingChoices
raise Exception
- text_choices["text"] = chunk["choices"][0]["delta"]["content"]
+ delta = chunk["choices"][0]["delta"]
+ text_choices["text"] = delta["content"]
+ text_choices["reasoning_content"] = delta.get("reasoning_content")
text_choices["index"] = chunk["choices"][0]["index"]
text_choices["finish_reason"] = chunk["choices"][0]["finish_reason"]
response["choices"] = [text_choices]
diff --git a/tests/test_litellm/test_utils.py b/tests/test_litellm/test_utils.py
index 437ec8cc1eb..feed6300b15 100644
--- a/tests/test_litellm/test_utils.py
+++ b/tests/test_litellm/test_utils.py
@@ -11,9 +11,10 @@ sys.path.insert(
) # Adds the parent directory to the system path
import litellm
-from litellm.types.utils import LlmProviders
+from litellm.types.utils import Delta, LlmProviders, ModelResponseStream, StreamingChoices
from litellm.utils import (
ProviderConfigManager,
+ TextCompletionStreamWrapper,
get_llm_provider,
get_optional_params_image_gen,
)
@@ -2074,6 +2075,41 @@ def test_register_model_with_scientific_notation():
assert registered_model["mode"] == "chat"
+def test_reasoning_content_preserved_in_text_completion_wrapper():
+ """Ensure reasoning_content is copied from delta to text_choices."""
+ chunk = ModelResponseStream(
+ id="test-id",
+ created=1234567890,
+ model="test-model",
+ object="chat.completion.chunk",
+ choices=[
+ StreamingChoices(
+ finish_reason=None,
+ index=0,
+ delta=Delta(
+ content="Some answer text",
+ role="assistant",
+ reasoning_content="Here's my chain of thought...",
+ ),
+ )
+ ],
+ )
+
+ wrapper = TextCompletionStreamWrapper(
+ completion_stream=None, # Not used in convert_to_text_completion_object
+ model="test-model",
+ stream_options=None,
+ )
+
+ transformed = wrapper.convert_to_text_completion_object(chunk)
+
+ assert "choices" in transformed
+ assert len(transformed["choices"]) == 1
+ choice = transformed["choices"][0]
+ assert choice["text"] == "Some answer text"
+ assert choice["reasoning_content"] == "Here's my chain of thought..."
+
+
if __name__ == "__main__":
# Allow running this test file directly for debugging
pytest.main([__file__, "-v"])