Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions litellm/integrations/websearch_interception/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ async def _execute_chat_completion_agentic_loop(
raise ValueError("WebSearchInterception: missing follow-up messages")
params = dict(optional_params)
params.update(request_patch.optional_params)
params.pop("tool_choice", None)
return await litellm.acompletion(
model=request_patch.model or model,
messages=request_patch.messages,
Expand Down Expand Up @@ -1203,6 +1204,7 @@ async def _build_chat_completion_request_patch(
if k
not in {
"tools",
"tool_choice",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removes tool_choice from 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 original optional_params and then merge request_patch.optional_params, so omission here leaves a forced litellm_web_search choice in place. Please explicitly clear tool_choice after merging, or encode a clear/delete signal in the request patch, and add a regression test that proves the follow-up litellm.acompletion call has no tool_choice when the original request forced web search

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this done ?

"extra_body",
"model_alias_map",
"stream_response",
Expand Down
89 changes: 63 additions & 26 deletions litellm/litellm_core_utils/chat_completion_agentic_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

kwargs_for_followup = _filter_followup_kwargs(kwargs)
kwargs_for_followup.update(
Expand Down Expand Up @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 = {
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions litellm/llms/custom_httpx/llm_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,7 @@ async def async_anthropic_messages_handler(
anthropic_messages_optional_request_params=anthropic_messages_optional_request_params,
logging_obj=logging_obj,
custom_llm_provider=custom_llm_provider,
kwargs=kwargs,
kwargs={**kwargs, "api_key": api_key} if api_key else kwargs,
)
return initial_response
else:
Expand All @@ -2121,6 +2121,10 @@ async def async_anthropic_messages_handler(
logging_obj=logging_obj,
)

# Inject api_key into kwargs so follow-up calls in agentic hooks can
# authenticate. api_key is a named param here (not in kwargs), so
# _prepare_followup_kwargs would miss it otherwise.
kwargs_for_agentic = {**kwargs, "api_key": api_key} if api_key else kwargs
# Call agentic completion hooks (non-streaming path only)
final_response = await self._call_agentic_completion_hooks(
response=initial_response,
Expand All @@ -2131,7 +2135,7 @@ async def async_anthropic_messages_handler(
logging_obj=logging_obj,
stream=False,
custom_llm_provider=custom_llm_provider,
kwargs=kwargs,
kwargs=kwargs_for_agentic,
)

return self._maybe_wrap_in_fake_stream(
Expand Down
Loading
Loading