Skip to content

fix(websearch_interception): ensure spend/cost logging runs when stream=True#25424

Merged
ishaan-berri merged 2 commits intolitellm_ishaan_april10from
litellm_fix-websearch-interception-logging
Apr 10, 2026
Merged

fix(websearch_interception): ensure spend/cost logging runs when stream=True#25424
ishaan-berri merged 2 commits intolitellm_ishaan_april10from
litellm_fix-websearch-interception-logging

Conversation

@Sameerlite
Copy link
Copy Markdown
Collaborator

@Sameerlite Sameerlite commented Apr 9, 2026

Summary

  • Websearch interception with stream=True was silently skipping all spend/cost logging because the stream conversion happened inside anthropic_messages (too late for wrapper_async to see)
  • Moved stream conversion to async_pre_call_deployment_hook so wrapper_async sees stream=False and takes the non-streaming logging path
  • Synced logging_obj.stream after the deployment hook so _success_handler_helper_fn correctly runs cost calculation
  • Recovered original stream intent via _websearch_interception_converted_stream flag for the short-circuit path

Test plan

  • Added regression test test_deployment_hook_converts_stream_and_logging_obj_syncs
  • All 52 existing websearch interception tests pass
  • All 6 E2E websearch tests pass
image

…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
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 9, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Apr 10, 2026 7:13pm

Request Review

@codspeed-hq
Copy link
Copy Markdown
Contributor

codspeed-hq bot commented Apr 9, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks


Comparing litellm_fix-websearch-interception-logging (bec448d) with main (d0e347a)

Open in CodSpeed

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 9, 2026

Greptile Summary

This PR fixes a spend/cost logging gap where stream=True Anthropic Messages requests with websearch interception would silently skip logging. The root cause was that wrapper_async saw stream=True and took the streaming early-return path before cost logging could run. The fix moves stream conversion from async_pre_request_hook (inside anthropic_messages) up into async_pre_call_deployment_hook so wrapper_async observes stream=False and follows the full non-streaming logging path. A logging_obj.stream sync in utils.py ensures the logging object stays consistent after the hook runs.

Confidence Score: 5/5

Safe 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 — _websearch_interception_converted_stream still leaks into anthropic_messages_handler via **kwargs (harmless but a prior open concern).

Important Files Changed

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
Loading

Reviews (2): Last reviewed commit: "Merge pull request #25524 from BerriAI/m..." | Re-trigger Greptile

@Sameerlite Sameerlite temporarily deployed to integration-postgres April 10, 2026 19:11 — with GitHub Actions Inactive
@Sameerlite Sameerlite temporarily deployed to integration-postgres April 10, 2026 19:11 — with GitHub Actions Inactive
@Sameerlite Sameerlite temporarily deployed to integration-postgres April 10, 2026 19:11 — with GitHub Actions Inactive
@ishaan-berri ishaan-berri changed the base branch from main to litellm_ishaan_april10 April 10, 2026 23:52
@ishaan-berri ishaan-berri merged commit 02b4934 into litellm_ishaan_april10 Apr 10, 2026
101 of 107 checks passed
@ishaan-berri ishaan-berri deleted the litellm_fix-websearch-interception-logging branch April 10, 2026 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants