Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 74 additions & 47 deletions litellm/llms/custom_httpx/llm_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5504,6 +5504,50 @@ async def _execute_chat_completion_agentic_plan(
**kwargs_for_followup,
)

def _maybe_wrap_in_fake_stream(
self,
response: Any,
logging_obj: Optional["LiteLLMLoggingObj"],
api_surface: str,
) -> Any:
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
"""
If the original request was streaming but converted to non-streaming for
WebSearch interception, wrap the dict response in a FakeAnthropicMessagesStreamIterator.

The converted-stream flag is only ever set by anthropic-messages websearch
interception, and the wrapper rebuilds an Anthropic SSE stream, so wrapping
is gated on ``api_surface == "anthropic_messages"`` to leave other surfaces
(e.g. the responses API) untouched.
"""
if api_surface != "anthropic_messages":
return response
websearch_converted_stream = (
logging_obj.model_call_details.get(
"websearch_interception_converted_stream", False
)
if logging_obj is not None
else False
)
if websearch_converted_stream and isinstance(response, dict):
from typing import cast

from litellm._logging import verbose_logger
from litellm.llms.anthropic.experimental_pass_through.messages.fake_stream_iterator import (
FakeAnthropicMessagesStreamIterator,
)
from litellm.types.llms.anthropic_messages.anthropic_response import (
AnthropicMessagesResponse,
)

verbose_logger.debug(
"WebSearchInterception: Agentic loop completed, "
"converting non-streaming response to fake stream"
)
return FakeAnthropicMessagesStreamIterator(
response=cast(AnthropicMessagesResponse, response)
)
return response

async def _call_agentic_completion_hooks(
self,
response: Any,
Expand Down Expand Up @@ -5586,7 +5630,7 @@ async def _call_agentic_completion_hooks(
is not CustomLogger.async_build_agentic_loop_plan
)
if not build_plan_overridden:
return await callback.async_run_agentic_loop(
agentic_result = await callback.async_run_agentic_loop(
tools=tool_calls,
model=model,
messages=messages,
Expand All @@ -5597,6 +5641,9 @@ async def _call_agentic_completion_hooks(
stream=stream,
kwargs=kwargs_with_provider,
)
return self._maybe_wrap_in_fake_stream(
agentic_result, logging_obj, api_surface
)

plan = await callback.async_build_agentic_loop_plan(
tools=tool_calls,
Expand All @@ -5611,14 +5658,18 @@ async def _call_agentic_completion_hooks(
)

if plan.response_override is not None:
return plan.response_override
return self._maybe_wrap_in_fake_stream(
plan.response_override, logging_obj, api_surface
)
if plan.terminate:
verbose_logger.debug(
"Agentic loop terminated by callback=%s reason=%s",
callback.__class__.__name__,
plan.stop_reason,
)
return response
return self._maybe_wrap_in_fake_stream(
response, logging_obj, api_surface
)
if not plan.run_agentic_loop:
continue

Expand All @@ -5636,19 +5687,23 @@ async def _call_agentic_completion_hooks(
callback=callback,
)

return await self._execute_anthropic_agentic_plan(
plan=plan,
model=model,
messages=messages,
anthropic_messages_optional_request_params=anthropic_messages_optional_request_params,
logging_obj=logging_obj,
kwargs=kwargs_with_provider,
depth=depth,
max_loops=max_loops,
fingerprints=fingerprints,
fingerprint=fingerprint,
stream=stream,
callback=callback,
return self._maybe_wrap_in_fake_stream(
await self._execute_anthropic_agentic_plan(
plan=plan,
model=model,
messages=messages,
anthropic_messages_optional_request_params=anthropic_messages_optional_request_params,
logging_obj=logging_obj,
kwargs=kwargs_with_provider,
depth=depth,
max_loops=max_loops,
fingerprints=fingerprints,
fingerprint=fingerprint,
stream=stream,
callback=callback,
),
logging_obj,
api_surface,
)
except Exception as e:
_call_id = getattr(logging_obj, "litellm_call_id", "unknown")
Expand All @@ -5665,37 +5720,9 @@ async def _call_agentic_completion_hooks(
# 1. Stream was originally True but converted to False for WebSearch interception
# 2. No agentic loop ran (LLM didn't use the tool)
# 3. We have a non-streaming response that needs to be converted to streaming
websearch_converted_stream = (
logging_obj.model_call_details.get(
"websearch_interception_converted_stream", False
)
if logging_obj is not None
else False
)

if api_surface == "anthropic_messages" and websearch_converted_stream:
from typing import cast

from litellm._logging import verbose_logger
from litellm.llms.anthropic.experimental_pass_through.messages.fake_stream_iterator import (
FakeAnthropicMessagesStreamIterator,
)
from litellm.types.llms.anthropic_messages.anthropic_response import (
AnthropicMessagesResponse,
)

verbose_logger.debug(
"WebSearchInterception: No tool call made, converting non-streaming response to fake stream"
)

# Convert the non-streaming response to a fake stream
# The response should be an AnthropicMessagesResponse (dict)
if isinstance(response, dict):
# Create a fake streaming iterator
fake_stream = FakeAnthropicMessagesStreamIterator(
response=cast(AnthropicMessagesResponse, response)
)
return fake_stream
result = self._maybe_wrap_in_fake_stream(response, logging_obj, api_surface)
if result is not response:
return result

return None

Expand Down
Loading
Loading