From 764c128120616b4b95dd32e113aec16883df3ca7 Mon Sep 17 00:00:00 2001 From: VHash <225398745+vhash0@users.noreply.github.com> Date: Tue, 28 Apr 2026 02:17:30 +0900 Subject: [PATCH] fix(ollama): process both thinking and content in same streaming chunk (#26098) --- litellm/llms/ollama/chat/transformation.py | 2 +- .../ollama/test_ollama_chat_transformation.py | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/litellm/llms/ollama/chat/transformation.py b/litellm/llms/ollama/chat/transformation.py index 48534799c97..bba2358b6a7 100644 --- a/litellm/llms/ollama/chat/transformation.py +++ b/litellm/llms/ollama/chat/transformation.py @@ -510,7 +510,7 @@ class OllamaChatCompletionResponseIterator(BaseModelResponseIterator): if chunk["message"].get("thinking") is not None: reasoning_content = chunk["message"].get("thinking") self.started_reasoning_content = True - elif chunk["message"].get("content") is not None: + if chunk["message"].get("content") is not None: if ( self.started_reasoning_content and not self.finished_reasoning_content diff --git a/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py b/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py index 05b96b88228..2ba2e1db9be 100644 --- a/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py +++ b/tests/test_litellm/llms/ollama/test_ollama_chat_transformation.py @@ -694,6 +694,30 @@ class TestOllamaReasoningContentStreaming: # reasoning_content is not set when there's no thinking in the chunk assert getattr(result2.choices[0].delta, "reasoning_content", None) is None + def test_thinking_and_content_in_same_chunk(self): + """ + Test that a chunk containing both thinking and content preserves both fields. + """ + iterator = OllamaChatCompletionResponseIterator( + streaming_response=iter([]), + sync_stream=True, + ) + + chunk = { + "model": "deepseek-r1", + "message": { + "role": "assistant", + "thinking": "Let me reason first.", + "content": "Final answer.", + }, + "done": False, + } + + result = iterator.chunk_parser(chunk) + + assert result.choices[0].delta.reasoning_content == "Let me reason first." + assert result.choices[0].delta.content == "Final answer." + def test_think_tags_in_content(self): """ Test that tags embedded in content are properly parsed.