fix(websearch_interception): ensure spend/cost logging runs when stream=True#25424
Conversation
…am=True The deployment hook now converts stream=True→False in wrapper_async's scope so the streaming early-return path is skipped and logging executes. logging_obj.stream is synced after the hook, and the original stream intent is recovered for the short-circuit path. Made-with: Cursor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Merging this PR will not alter performance
Comparing |
Greptile SummaryThis PR fixes a spend/cost logging gap where Confidence Score: 5/5Safe to merge; the fix correctly routes websearch-intercepted streaming requests through non-streaming cost logging, and all remaining open questions are P2 style concerns previously flagged. No new P0/P1 issues found. The logical flow is sound: deployment hook converts stream early enough for wrapper_async to observe it, logging_obj.stream is synced correctly, and _is_streaming_request returns False as intended. The two pre-existing concerns (flag leaking into anthropic_messages_handler kwargs and MagicMock-based test simulation) were already flagged in prior review rounds and are both P2 — neither causes a runtime error since anthropic_messages_handler accepts **kwargs. litellm/llms/anthropic/experimental_pass_through/messages/handler.py —
|
| Filename | Overview |
|---|---|
| litellm/integrations/websearch_interception/handler.py | Adds stream→False conversion to async_pre_call_deployment_hook so wrapper_async sees the converted value; retains the same logic in async_pre_request_hook as a fallback for direct callers. |
| litellm/llms/anthropic/experimental_pass_through/messages/handler.py | Changes original_stream computation to account for the deployment-hook-converted flag; the _websearch_interception_converted_stream key is not popped from kwargs before being passed into anthropic_messages_handler (harmless but leaky — previously flagged). |
| litellm/utils.py | Adds post-deployment-hook sync of logging_obj.stream to match the (potentially converted) kwargs["stream"]; guards correctly against None stream values. |
| tests/test_litellm/integrations/websearch_interception/test_websearch_interception_handler.py | New regression test verifies the deployment hook sets stream=False and the converted flag; the wrapper_async sync is simulated via MagicMock rather than exercised through real code (previously flagged). |
Sequence Diagram
sequenceDiagram
participant Caller
participant wrapper_async as wrapper_async (utils.py)
participant DeployHook as async_pre_call_deployment_hook
participant anthropic_messages
participant PreReqHook as async_pre_request_hook
Caller->>wrapper_async: stream=True, tools=[web_search]
Note over wrapper_async: function_setup → logging_obj.stream=True
wrapper_async->>DeployHook: kwargs {stream=True}
DeployHook-->>wrapper_async: kwargs {stream=False, _converted=True}
Note over wrapper_async: Sync logging_obj.stream=False<br/>(NEW: PR fix in utils.py)
wrapper_async->>anthropic_messages: stream=False, _converted=True, **kwargs
Note over anthropic_messages: original_stream = False OR True = True<br/>(NEW: flag check at line 190)
anthropic_messages->>PreReqHook: stream=False (already converted)
PreReqHook-->>anthropic_messages: no change (stream already False)
anthropic_messages->>anthropic_messages: short-circuit check with original_stream=True
anthropic_messages-->>wrapper_async: non-streaming AnthropicMessagesResponse
Note over wrapper_async: stream=False → takes non-streaming path<br/>✅ spend/cost logging runs
Reviews (2): Last reviewed commit: "Merge pull request #25524 from BerriAI/m..." | Re-trigger Greptile
merge main
02b4934
into
litellm_ishaan_april10
Summary
stream=Truewas silently skipping all spend/cost logging because the stream conversion happened insideanthropic_messages(too late forwrapper_asyncto see)async_pre_call_deployment_hooksowrapper_asyncseesstream=Falseand takes the non-streaming logging pathlogging_obj.streamafter the deployment hook so_success_handler_helper_fncorrectly runs cost calculation_websearch_interception_converted_streamflag for the short-circuit pathTest plan
test_deployment_hook_converts_stream_and_logging_obj_syncs