-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Fix: Ollama chunk parser fails to handle complete reasoning blocks in single chunk #15166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielaskdd
wants to merge
3
commits into
BerriAI:main
Choose a base branch
from
danielaskdd:fix-ollama-think-tag
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−13
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
tests/test_litellm/llms/ollama/test_ollama_chunk_parser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
from litellm.llms.ollama.chat.transformation import OllamaChatCompletionResponseIterator | ||
from litellm.types.utils import Delta, StreamingChoices | ||
|
||
# Mock the Delta and other necessary classes if they are not directly importable or need special setup | ||
class MockModelResponseStream: | ||
def __init__(self, choices, model, object_type, system_fingerprint, usage=None, **kwargs): | ||
self.choices = choices | ||
self.model = model | ||
self.object = object_type | ||
self.system_fingerprint = system_fingerprint | ||
self.usage = usage | ||
for key, value in kwargs.items(): | ||
setattr(self, key, value) | ||
|
||
@pytest.fixture | ||
def mock_iterator(): | ||
"""Fixture to create a mock OllamaChatCompletionResponseIterator.""" | ||
iterator = OllamaChatCompletionResponseIterator( | ||
streaming_response=iter([]), | ||
sync_stream=True, | ||
json_mode=False | ||
) | ||
return iterator | ||
|
||
def test_full_think_block_in_one_chunk(mock_iterator): | ||
"""Test case where a complete <think>...</think> block is in a single chunk.""" | ||
chunk = {"message": {"content": "<think>This is a thought.</think>"}, "done": False, "model": "test-model"} | ||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
result = mock_iterator.chunk_parser(chunk) | ||
assert result.choices[0].delta.content == "" | ||
assert result.choices[0].delta.reasoning_content == "This is a thought." | ||
assert mock_iterator.started_reasoning_content | ||
assert mock_iterator.finished_reasoning_content | ||
|
||
def test_think_tags_split_across_chunks(mock_iterator): | ||
"""Test case where <think> and </think> tags are in separate chunks.""" | ||
chunk1 = {"message": {"content": "<think>This is a thought."}, "done": False, "model": "test-model"} | ||
chunk2 = {"message": {"content": " And it continues.</think>"}, "done": True, "model": "test-model"} | ||
|
||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
result1 = mock_iterator.chunk_parser(chunk1) | ||
assert result1.choices[0].delta.reasoning_content == "This is a thought." | ||
assert mock_iterator.started_reasoning_content | ||
assert not mock_iterator.finished_reasoning_content | ||
|
||
result2 = mock_iterator.chunk_parser(chunk2) | ||
assert result2.choices[0].delta.reasoning_content == " And it continues." | ||
assert mock_iterator.started_reasoning_content | ||
assert mock_iterator.finished_reasoning_content | ||
|
||
def test_content_before_and_after_think_tag(mock_iterator): | ||
"""Test case where there is content before and after the <think> ... </think> block""" | ||
chunk = {"message": {"content": "Here is a preamble. <think>This is a thought.</think> Here is a postamble."}, "done": True, "model": "test-model"} | ||
|
||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
result = mock_iterator.chunk_parser(chunk) | ||
|
||
assert result.choices[0].delta.content == "Here is a preamble. Here is a postamble." | ||
assert result.choices[0].delta.reasoning_content == "This is a thought." | ||
assert mock_iterator.started_reasoning_content | ||
assert mock_iterator.finished_reasoning_content | ||
|
||
@patch('litellm.llms.ollama.chat.transformation.OllamaChatCompletionResponseIterator.construct_empty_chunk', create=True) | ||
def test_whitespace_chunks(mock_construct_empty_chunk, mock_iterator): | ||
"""Test case where chunks contain only whitespace.""" | ||
mock_construct_empty_chunk.return_value = MockModelResponseStream( | ||
choices=[StreamingChoices(index=0, delta=Delta(content="", reasoning_content=None, role="assistant", tool_calls=None), finish_reason=None)], | ||
model="test-model", | ||
object_type="chat.completion.chunk", | ||
system_fingerprint=None | ||
) | ||
chunk1 = {"message": {"content": " "}, "done": False, "model": "test-model"} | ||
chunk2 = {"message": {"content": "\n\n"}, "done": True, "model": "test-model"} | ||
|
||
result1 = mock_iterator.chunk_parser(chunk1) | ||
assert result1.choices[0].delta.content == " " | ||
assert result1.choices[0].delta.reasoning_content == "" | ||
|
||
result2 = mock_iterator.chunk_parser(chunk2) | ||
assert result2.choices[0].delta.content == "\n\n" | ||
assert result2.choices[0].delta.reasoning_content == "" | ||
|
||
def test_content_before_think_tag(mock_iterator): | ||
"""Test case where there is regular content before the <think> tag in the same chunk.""" | ||
chunk = {"message": {"content": "Regular content <think>starting thought"}, "done": False, "model": "test-model"} | ||
|
||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
result = mock_iterator.chunk_parser(chunk) | ||
|
||
assert result.choices[0].delta.content == "Regular content " | ||
assert result.choices[0].delta.reasoning_content == "starting thought" | ||
assert mock_iterator.started_reasoning_content | ||
assert not mock_iterator.finished_reasoning_content | ||
|
||
def test_content_after_think_end_tag(mock_iterator): | ||
"""Test case where there is regular content after the </think> tag in the same chunk.""" | ||
# First start the reasoning | ||
chunk1 = {"message": {"content": "<think>This is a thought"}, "done": False, "model": "test-model"} | ||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
mock_iterator.chunk_parser(chunk1) | ||
|
||
# Then end it with content after | ||
chunk2 = {"message": {"content": " continued.</think> More regular content"}, "done": True, "model": "test-model"} | ||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
result = mock_iterator.chunk_parser(chunk2) | ||
|
||
assert result.choices[0].delta.reasoning_content == " continued." | ||
assert result.choices[0].delta.content == " More regular content" | ||
assert mock_iterator.started_reasoning_content | ||
assert mock_iterator.finished_reasoning_content | ||
|
||
def test_mixed_content_across_multiple_chunks(mock_iterator): | ||
"""Test case with mixed content and reasoning across multiple chunks.""" | ||
chunk1 = {"message": {"content": "Hello "}, "done": False, "model": "test-model"} | ||
chunk2 = {"message": {"content": "world <think>I'm thinking"}, "done": False, "model": "test-model"} | ||
chunk3 = {"message": {"content": " about this</think> and "}, "done": False, "model": "test-model"} | ||
chunk4 = {"message": {"content": "continuing."}, "done": True, "model": "test-model"} | ||
|
||
with patch("litellm.llms.ollama.chat.transformation.uuid.uuid4", return_value="1234"): | ||
# Chunk 1: Regular content before any reasoning | ||
result1 = mock_iterator.chunk_parser(chunk1) | ||
assert result1.choices[0].delta.content == "Hello " | ||
assert result1.choices[0].delta.reasoning_content == "" | ||
assert not mock_iterator.started_reasoning_content | ||
|
||
# Chunk 2: Content before <think> and start of reasoning | ||
result2 = mock_iterator.chunk_parser(chunk2) | ||
assert result2.choices[0].delta.content == "world " | ||
assert result2.choices[0].delta.reasoning_content == "I'm thinking" | ||
assert mock_iterator.started_reasoning_content | ||
assert not mock_iterator.finished_reasoning_content | ||
|
||
# Chunk 3: End of reasoning and content after </think> | ||
result3 = mock_iterator.chunk_parser(chunk3) | ||
assert result3.choices[0].delta.reasoning_content == " about this" | ||
assert result3.choices[0].delta.content == " and " | ||
assert mock_iterator.finished_reasoning_content | ||
|
||
# Chunk 4: Regular content after reasoning finished | ||
result4 = mock_iterator.chunk_parser(chunk4) | ||
assert result4.choices[0].delta.content == "continuing." | ||
assert result4.choices[0].delta.reasoning_content == "" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.