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

This commit is contained in:
vhash 2026-04-20 04:18:48 +09:00
parent 2f22a1293e
commit 97718bcfae
No known key found for this signature in database
GPG key ID: F33F5E5B07847AEF
2 changed files with 25 additions and 1 deletions

View file

@ -505,7 +505,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.