Repository navigation
fix(websearch): wrap agentic loop response in fake stream for streaming requests #31484
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
Merged
mateo-berri
merged 3 commits into
litellm_internal_staging
from
litellm_websearch_streaming_wrap
Jun 27, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f6fffdd
fix(websearch): wrap agentic loop response in fake stream for streami…
vokako 2a8e484
test(websearch): cover agentic-loop wrap paths; gate fake-stream on a…
mateo-berri bec6c87
test(websearch): cover _execute_anthropic_agentic_plan and tail wrap …
mateo-berri 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
70 changes: 70 additions & 0 deletions
70
tests/test_litellm/integrations/websearch_interception/test_websearch_streaming_wrap.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,70 @@ | ||
| """ | ||
| Unit tests for _maybe_wrap_in_fake_stream in BaseLLMHTTPHandler. | ||
|
|
||
| Tests that agentic loop responses are correctly wrapped in | ||
| FakeAnthropicMessagesStreamIterator when the original request was streaming. | ||
| """ | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
|
|
||
| from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler | ||
| from litellm.llms.anthropic.experimental_pass_through.messages.fake_stream_iterator import ( | ||
| FakeAnthropicMessagesStreamIterator, | ||
| ) | ||
|
|
||
|
|
||
| class TestMaybeWrapInFakeStream: | ||
| def setup_method(self): | ||
| self.handler = BaseLLMHTTPHandler() | ||
|
|
||
| def test_wraps_dict_when_converted_stream_flag_is_true(self): | ||
| """When websearch_interception_converted_stream is True and response is dict, wrap it.""" | ||
| logging_obj = MagicMock() | ||
| logging_obj.model_call_details = { | ||
| "websearch_interception_converted_stream": True | ||
| } | ||
| response = { | ||
| "id": "msg_123", | ||
| "type": "message", | ||
| "role": "assistant", | ||
| "content": [{"type": "text", "text": "hello"}], | ||
| "stop_reason": "end_turn", | ||
| "usage": {"input_tokens": 10, "output_tokens": 5}, | ||
| } | ||
|
|
||
| result = self.handler._maybe_wrap_in_fake_stream(response, logging_obj) | ||
|
|
||
| assert isinstance(result, FakeAnthropicMessagesStreamIterator) | ||
|
|
||
| def test_returns_response_unchanged_when_flag_is_false(self): | ||
| """When flag is False, return response as-is.""" | ||
| logging_obj = MagicMock() | ||
| logging_obj.model_call_details = { | ||
| "websearch_interception_converted_stream": False | ||
| } | ||
| response = {"id": "msg_123", "content": []} | ||
|
|
||
| result = self.handler._maybe_wrap_in_fake_stream(response, logging_obj) | ||
|
|
||
| assert result is response | ||
|
|
||
| def test_returns_response_unchanged_when_not_dict(self): | ||
| """When response is not a dict (e.g., already a stream), return as-is.""" | ||
| logging_obj = MagicMock() | ||
| logging_obj.model_call_details = { | ||
| "websearch_interception_converted_stream": True | ||
| } | ||
| response = MagicMock() # Not a dict | ||
|
|
||
| result = self.handler._maybe_wrap_in_fake_stream(response, logging_obj) | ||
|
|
||
| assert result is response | ||
|
|
||
| def test_returns_response_when_logging_obj_is_none(self): | ||
| """When logging_obj is None, return response as-is.""" | ||
| response = {"id": "msg_123", "content": []} | ||
|
|
||
| result = self.handler._maybe_wrap_in_fake_stream(response, None) | ||
|
|
||
| assert result is response |
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.