fix(ollama): process both thinking and content in same streaming chunk (#26098)

This commit is contained in:
VHash 2026-04-28 02:17:30 +09:00 committed by GitHub
parent db687bfeca
commit 764c128120
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View file

@ -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

View file

@ -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 <think> tags embedded in content are properly parsed.