Repository navigation
fix(websearch): websearch_interception agentic loop fixes for chat completions and anthropic messages #31669
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
krrish-berri-2
merged 10 commits into
litellm_internal_staging
from
litellm_websearch-interception-fixes
Jul 1, 2026
Merged
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6c24f64
fix(websearch): wire chat completion agentic loop to correct hooks
krrish-berri-2 a8d44d0
fix(websearch): strip tool_choice from follow-up request
krrish-berri-2 a9bcd23
fix(websearch): inject api_key into agentic hook kwargs for anthropic…
krrish-berri-2 bd3acc7
chore: merge litellm_internal_staging, keep both test additions
krrish-berri-2 45b60e0
ci: trigger CI run
krrish-berri-2 087edeb
fix(websearch): support unified agentic hooks alongside chat-completi…
krrish-berri-2 7a41f6e
fix(websearch): strip tool_choice from legacy chat completion follow-…
krrish-berri-2 8eeee03
fix(websearch): always strip tool_choice from plan-path follow-up params
krrish-berri-2 c16096f
refactor(websearch): simplify agentic hook dispatch to single boolean
krrish-berri-2 b0ced1d
refactor(websearch): unify agentic hook dispatch to single flow
krrish-berri-2 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
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 |
|---|---|---|
|
|
@@ -34,12 +34,24 @@ def _gate_overridden(callback: CustomLogger) -> bool: | |
| return getattr(func, "__func__", func) is not getattr(base, "__func__", base) | ||
|
|
||
|
|
||
| def _chat_completion_gate_overridden(callback: CustomLogger) -> bool: | ||
| base = CustomLogger.async_should_run_chat_completion_agentic_loop | ||
| func = type(callback).async_should_run_chat_completion_agentic_loop | ||
| return getattr(func, "__func__", func) is not getattr(base, "__func__", base) | ||
|
|
||
|
|
||
| def _build_plan_overridden(callback: CustomLogger) -> bool: | ||
| base = CustomLogger.async_build_agentic_loop_plan | ||
| func = type(callback).async_build_agentic_loop_plan | ||
| return getattr(func, "__func__", func) is not getattr(base, "__func__", base) | ||
|
|
||
|
|
||
| def _chat_completion_build_plan_overridden(callback: CustomLogger) -> bool: | ||
| base = CustomLogger.async_build_chat_completion_agentic_loop_plan | ||
| func = type(callback).async_build_chat_completion_agentic_loop_plan | ||
| return getattr(func, "__func__", func) is not getattr(base, "__func__", base) | ||
|
|
||
|
|
||
| def _post_hook_overridden(callback: CustomLogger) -> bool: | ||
| base = CustomLogger.async_post_agentic_loop_response_hook | ||
| func = type(callback).async_post_agentic_loop_response_hook | ||
|
|
@@ -137,8 +149,8 @@ async def _execute_chat_completion_agentic_plan( | |
| optional_params_for_followup = {**optional_params, **patch.optional_params} | ||
| if patch.tools is not None: | ||
| optional_params_for_followup["tools"] = patch.tools | ||
| if "tool_choice" not in patch.optional_params: | ||
| optional_params_for_followup.pop("tool_choice", None) | ||
| if "tool_choice" not in patch.optional_params: | ||
| optional_params_for_followup.pop("tool_choice", None) | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| kwargs_for_followup = _filter_followup_kwargs(kwargs) | ||
| kwargs_for_followup.update( | ||
|
|
@@ -206,7 +218,10 @@ async def maybe_run_chat_completion_agentic_loop( | |
| for callback in callbacks: | ||
| if not isinstance(callback, CustomLogger): | ||
| continue | ||
| if not _gate_overridden(callback): | ||
|
|
||
| uses_chat_completion_hooks = _chat_completion_gate_overridden(callback) | ||
| uses_unified_hooks = not uses_chat_completion_hooks and _gate_overridden(callback) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a lot of conditional logic is there a cleaner way we can do this ? |
||
| if not uses_chat_completion_hooks and not uses_unified_hooks: | ||
| continue | ||
|
|
||
| gate_kwargs = { | ||
|
|
@@ -215,15 +230,26 @@ async def maybe_run_chat_completion_agentic_loop( | |
| "custom_llm_provider": custom_llm_provider, | ||
| } | ||
| try: | ||
| should_run, tool_calls = await callback.async_should_run_agentic_loop( | ||
| response=response, | ||
| model=model, | ||
| messages=messages, | ||
| tools=tools, | ||
| stream=stream, | ||
| custom_llm_provider=custom_llm_provider, | ||
| kwargs=gate_kwargs, | ||
| ) | ||
| if uses_chat_completion_hooks: | ||
| should_run, tool_calls = await callback.async_should_run_chat_completion_agentic_loop( | ||
| response=response, | ||
| model=model, | ||
| messages=messages, | ||
| tools=tools, | ||
| stream=stream, | ||
| custom_llm_provider=custom_llm_provider, | ||
| kwargs=gate_kwargs, | ||
| ) | ||
| else: | ||
| should_run, tool_calls = await callback.async_should_run_agentic_loop( | ||
| response=response, | ||
| model=model, | ||
| messages=messages, | ||
| tools=tools, | ||
| stream=stream, | ||
| custom_llm_provider=custom_llm_provider, | ||
| kwargs=gate_kwargs, | ||
| ) | ||
| except Exception as e: | ||
| verbose_logger.exception( | ||
| "LiteLLM.AgenticHookError: Exception in chat completion agentic gate: %s", | ||
|
|
@@ -248,8 +274,31 @@ async def maybe_run_chat_completion_agentic_loop( | |
| "_agentic_loop_api_surface": CHAT_COMPLETION_AGENTIC_SURFACE, | ||
| "custom_llm_provider": custom_llm_provider, | ||
| } | ||
| if not _build_plan_overridden(callback): | ||
| return await callback.async_run_agentic_loop( | ||
| if uses_chat_completion_hooks and not _chat_completion_build_plan_overridden(callback): | ||
| return await callback.async_run_chat_completion_agentic_loop( | ||
| tools=tool_calls, | ||
| model=model, | ||
| messages=messages, | ||
| response=response, | ||
| optional_params=optional_params, | ||
| logging_obj=logging_obj, | ||
| stream=stream, | ||
| kwargs=plan_kwargs, | ||
| ) | ||
|
|
||
| if uses_chat_completion_hooks: | ||
| plan = await callback.async_build_chat_completion_agentic_loop_plan( | ||
| tools=tool_calls, | ||
| model=model, | ||
| messages=messages, | ||
| response=response, | ||
| optional_params=optional_params, | ||
| logging_obj=logging_obj, | ||
| stream=stream, | ||
| kwargs=plan_kwargs, | ||
| ) | ||
| else: | ||
| plan = await callback.async_build_agentic_loop_plan( | ||
| tools=tool_calls, | ||
| model=model, | ||
| messages=messages, | ||
|
|
@@ -261,18 +310,6 @@ async def maybe_run_chat_completion_agentic_loop( | |
| kwargs=plan_kwargs, | ||
| ) | ||
|
|
||
| plan = await callback.async_build_agentic_loop_plan( | ||
| tools=tool_calls, | ||
| model=model, | ||
| messages=messages, | ||
| response=response, | ||
| anthropic_messages_provider_config=None, | ||
| anthropic_messages_optional_request_params=optional_params, | ||
| logging_obj=logging_obj, | ||
| stream=stream, | ||
| kwargs=plan_kwargs, | ||
| ) | ||
|
|
||
| if plan.response_override is not None: | ||
| return plan.response_override | ||
| if plan.terminate: | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This removes
tool_choicefrom the patch, but it does not clear the original value during follow-up execution. Both the typed dispatcher and the legacy direct path start from the originaloptional_paramsand then mergerequest_patch.optional_params, so omission here leaves a forcedlitellm_web_searchchoice in place. Please explicitly cleartool_choiceafter merging, or encode a clear/delete signal in the request patch, and add a regression test that proves the follow-uplitellm.acompletioncall has notool_choicewhen the original request forced web searchThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this done ?