From cb50d439b0f61bd2ee4c7f887757df39ab4cb0a8 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sun, 30 Aug 2026 12:17:38 +0530 Subject: [PATCH 01/12] fix(websearch): keep the provider prefix on the agentic follow-up model The agentic follow-up rebuilt its model string with "a slash means it is already qualified". Handlers are handed the provider-stripped model, and for a provider that routes through a sub-path the remainder still holds a slash, so bedrock/mantle/anthropic.claude-sonnet-5 arrived as mantle/anthropic.claude-sonnet-5 and stayed that way. The follow-up acompletion could not resolve a provider from it and raised "LLM Provider NOT provided". Prefix unless the model is already qualified with that same provider, in one shared helper used by both the handler and the websearch patch builder, which carried copies of the same heuristic. This also fixes openrouter, whose stripped model keeps a slash for the same reason, and the old startswith test that read openai as a prefix of openai_like. Fixes #38829 --- .../websearch_interception/handler.py | 8 ++-- litellm/litellm_core_utils/core_helpers.py | 14 +++++++ litellm/llms/custom_httpx/llm_http_handler.py | 6 +-- .../litellm_core_utils/test_core_helpers.py | 41 +++++++++++++++++++ 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.py index 15413140d9e..84040280cd3 100644 --- a/litellm/integrations/websearch_interception/handler.py +++ b/litellm/integrations/websearch_interception/handler.py @@ -1910,11 +1910,9 @@ class WebSearchInterceptionLogger(CustomLogger): k: v for k, v in kwargs.items() if not k.startswith("_websearch_interception") and k not in internal_params } - full_model_name = model - if "custom_llm_provider" in kwargs: - custom_llm_provider: Final = kwargs["custom_llm_provider"] - if not model.startswith(custom_llm_provider) and "/" not in model: - full_model_name = f"{custom_llm_provider}/{model}" + from litellm.litellm_core_utils.core_helpers import qualify_provider_stripped_model + + full_model_name: Final = qualify_provider_stripped_model(model, kwargs.get("custom_llm_provider", "")) verbose_logger.debug( "WebSearchInterception: Built chat completion request patch model=%s messages=%d", diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index e857461785c..3a137f49bed 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -31,6 +31,20 @@ def is_codex_user_agent(user_agent: str) -> bool: return bool(_CODEX_CLIENT_PREFIX_RE.match(user_agent)) +def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str: + """ + Put the provider prefix back on a model an agentic follow-up re-dispatches with. + + Handlers are handed the provider-stripped model, and for providers that route through + a sub-path (``bedrock/mantle/...``, ``openrouter/openai/...``) that remainder still + holds a slash. Treating any slash as "already qualified" drops the prefix and leaves a + string no provider can be resolved from. + """ + if not custom_llm_provider or model.startswith(f"{custom_llm_provider}/"): + return model + return f"{custom_llm_provider}/{model}" + + def safe_divide_seconds(seconds: float, denominator: float, default: float | None = None) -> float | None: """ Safely divide seconds by denominator, handling zero division. diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 4a9b8fee2e8..9d047670141 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -5789,9 +5789,9 @@ class BaseLLMHTTPHandler: if patch.messages is None: raise ValueError("Agentic loop plan missing patched messages") - full_model_name = patch.model or model - if "/" not in full_model_name: - full_model_name = f"{custom_llm_provider}/{full_model_name}" + from litellm.litellm_core_utils.core_helpers import qualify_provider_stripped_model + + full_model_name: Final = qualify_provider_stripped_model(patch.model or model, custom_llm_provider) optional_params_for_followup: Final = dict(optional_params) optional_params_for_followup.update(patch.optional_params) diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 1f716b41acd..c2fa9b9f7a1 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -19,6 +19,7 @@ from litellm.litellm_core_utils.core_helpers import ( map_finish_reason, normalize_drop_params, process_response_headers, + qualify_provider_stripped_model, reconstruct_model_name, redact_nested_match_and_regex_keys, set_provider_response_headers_in_hidden_params, @@ -586,3 +587,43 @@ class TestProviderResponseHeadersInHiddenParams: assert get_provider_response_headers_from_hidden_params(sibling) is None assert "additional_headers" not in sibling._hidden_params + + +class TestQualifyProviderStrippedModel: + """#38829: an agentic follow-up re-dispatched the provider-stripped model. For providers + that route through a sub-path the remainder still holds a slash, and the old + "any slash means already qualified" test dropped the prefix, leaving a string + litellm.acompletion could not resolve a provider from.""" + + @pytest.mark.parametrize( + "model,provider,expected", + [ + # the reported case: bedrock's OpenAI-compatible sub-path + ("mantle/anthropic.claude-sonnet-5", "bedrock", "bedrock/mantle/anthropic.claude-sonnet-5"), + ("invoke/anthropic.claude-v2", "bedrock", "bedrock/invoke/anthropic.claude-v2"), + # another provider whose stripped model keeps a slash + ("openai/gpt-4o", "openrouter", "openrouter/openai/gpt-4o"), + # the ordinary case still works + ("gpt-4o", "openai", "openai/gpt-4o"), + ("claude-sonnet-4-5", "anthropic", "anthropic/claude-sonnet-4-5"), + ], + ) + def test_the_provider_prefix_is_restored(self, model, provider, expected): + assert qualify_provider_stripped_model(model, provider) == expected + + @pytest.mark.parametrize( + "model,provider", + [ + ("bedrock/mantle/anthropic.claude-sonnet-5", "bedrock"), + ("openai/gpt-4o", "openai"), + ], + ) + def test_an_already_qualified_model_is_left_alone(self, model, provider): + assert qualify_provider_stripped_model(model, provider) == model + + def test_a_provider_that_only_shares_a_prefix_is_still_qualified(self): + """`openai` must not be read as a prefix of `openai_like`.""" + assert qualify_provider_stripped_model("openai_like/foo", "openai") == "openai/openai_like/foo" + + def test_no_provider_leaves_the_model_untouched(self): + assert qualify_provider_stripped_model("gpt-4o", "") == "gpt-4o" From 65ec35168dd894933ffb5a52a0987c798178d2d1 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sun, 30 Aug 2026 14:13:55 +0530 Subject: [PATCH 02/12] test(websearch): cover the agentic follow-up call site The helper had unit tests but the call site did not, so the two changed lines in _execute_chat_completion_agentic_plan were uncovered. Drive the real follow-up with mock_response instead of patching litellm, which keeps the test on the behaviour: against the old code the sub-path case raises "LLM Provider NOT provided", and the ordinary-model case passes either way as a guard. --- .../custom_httpx/test_llm_http_handler.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.py index 25d2d11b5ca..6cd74d1bbf1 100644 --- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py @@ -5056,3 +5056,51 @@ async def test_retrieve_file_rejects_unverified_bedrock_range_error( ) finally: sync_client.close() + + +class TestAgenticFollowUpKeepsTheProviderPrefix: + """#38829: the follow-up re-dispatched the provider-stripped model. For a provider that + routes through a sub-path the remainder still holds a slash, so the old + "a slash means already qualified" test dropped the prefix and the follow-up raised + "LLM Provider NOT provided".""" + + @staticmethod + def _plan(): + from litellm.types.integrations.custom_logger import ( + AgenticLoopPlan, + AgenticLoopRequestPatch, + ) + + return AgenticLoopPlan( + run_agentic_loop=True, + request_patch=AgenticLoopRequestPatch(messages=[{"role": "user", "content": "hi"}]), + ) + + async def _run_followup(self, model: str, custom_llm_provider: str): + from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler + + return await BaseLLMHTTPHandler()._execute_chat_completion_agentic_plan( + plan=self._plan(), + model=model, + messages=[{"role": "user", "content": "hi"}], + optional_params={"mock_response": "ok from followup"}, + kwargs={}, + custom_llm_provider=custom_llm_provider, + depth=0, + max_loops=2, + fingerprints=[], + fingerprint="fp", + ) + + @pytest.mark.asyncio + async def test_a_sub_path_model_still_resolves_a_provider(self): + """bedrock/mantle/... reaches the handler as mantle/..., which alone is unroutable.""" + response = await self._run_followup("mantle/anthropic.claude-sonnet-5", "bedrock") + + assert response.choices[0].message.content == "ok from followup" + + @pytest.mark.asyncio + async def test_an_ordinary_model_still_resolves_a_provider(self): + response = await self._run_followup("gpt-4o-mini", "openai") + + assert response.choices[0].message.content == "ok from followup" From 6820432194778858a4236d78c9b31dafd6991100 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sat, 5 Sep 2026 13:00:50 +0530 Subject: [PATCH 03/12] refactor(websearch): trim the comments on the provider-qualify helper --- litellm/litellm_core_utils/core_helpers.py | 9 +++------ tests/unit/litellm_core_utils/test_core_helpers.py | 3 --- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 3a137f49bed..f9216159cde 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -32,13 +32,10 @@ def is_codex_user_agent(user_agent: str) -> bool: def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str: - """ - Put the provider prefix back on a model an agentic follow-up re-dispatches with. + """Put the provider prefix back on a provider-stripped model. - Handlers are handed the provider-stripped model, and for providers that route through - a sub-path (``bedrock/mantle/...``, ``openrouter/openai/...``) that remainder still - holds a slash. Treating any slash as "already qualified" drops the prefix and leaves a - string no provider can be resolved from. + A sub-path provider (``bedrock/mantle/...``) leaves a slash in the remainder, so + treating any slash as "already qualified" would drop the prefix. """ if not custom_llm_provider or model.startswith(f"{custom_llm_provider}/"): return model diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index c2fa9b9f7a1..5cc861002f5 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -598,12 +598,9 @@ class TestQualifyProviderStrippedModel: @pytest.mark.parametrize( "model,provider,expected", [ - # the reported case: bedrock's OpenAI-compatible sub-path ("mantle/anthropic.claude-sonnet-5", "bedrock", "bedrock/mantle/anthropic.claude-sonnet-5"), ("invoke/anthropic.claude-v2", "bedrock", "bedrock/invoke/anthropic.claude-v2"), - # another provider whose stripped model keeps a slash ("openai/gpt-4o", "openrouter", "openrouter/openai/gpt-4o"), - # the ordinary case still works ("gpt-4o", "openai", "openai/gpt-4o"), ("claude-sonnet-4-5", "anthropic", "anthropic/claude-sonnet-4-5"), ], From 9905ecb2b02a5477acc60ac64053873bd0219002 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sun, 6 Sep 2026 11:34:39 +0530 Subject: [PATCH 04/12] refactor(websearch): cut the helper and test docstrings to one line --- litellm/litellm_core_utils/core_helpers.py | 6 +----- tests/unit/litellm_core_utils/test_core_helpers.py | 6 +----- tests/unit/llms/custom_httpx/test_llm_http_handler.py | 6 +----- 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index f9216159cde..2472b9bf9b1 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -32,11 +32,7 @@ def is_codex_user_agent(user_agent: str) -> bool: def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str: - """Put the provider prefix back on a provider-stripped model. - - A sub-path provider (``bedrock/mantle/...``) leaves a slash in the remainder, so - treating any slash as "already qualified" would drop the prefix. - """ + """Put the provider prefix back on a provider-stripped model.""" if not custom_llm_provider or model.startswith(f"{custom_llm_provider}/"): return model return f"{custom_llm_provider}/{model}" diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 5cc861002f5..803705b8a56 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -590,10 +590,7 @@ class TestProviderResponseHeadersInHiddenParams: class TestQualifyProviderStrippedModel: - """#38829: an agentic follow-up re-dispatched the provider-stripped model. For providers - that route through a sub-path the remainder still holds a slash, and the old - "any slash means already qualified" test dropped the prefix, leaving a string - litellm.acompletion could not resolve a provider from.""" + """#38829: a model whose remainder still holds a slash must keep its provider prefix.""" @pytest.mark.parametrize( "model,provider,expected", @@ -619,7 +616,6 @@ class TestQualifyProviderStrippedModel: assert qualify_provider_stripped_model(model, provider) == model def test_a_provider_that_only_shares_a_prefix_is_still_qualified(self): - """`openai` must not be read as a prefix of `openai_like`.""" assert qualify_provider_stripped_model("openai_like/foo", "openai") == "openai/openai_like/foo" def test_no_provider_leaves_the_model_untouched(self): diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.py index 6cd74d1bbf1..60b37452eca 100644 --- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py @@ -5059,10 +5059,7 @@ async def test_retrieve_file_rejects_unverified_bedrock_range_error( class TestAgenticFollowUpKeepsTheProviderPrefix: - """#38829: the follow-up re-dispatched the provider-stripped model. For a provider that - routes through a sub-path the remainder still holds a slash, so the old - "a slash means already qualified" test dropped the prefix and the follow-up raised - "LLM Provider NOT provided".""" + """#38829: the follow-up must re-dispatch a model litellm.acompletion can route.""" @staticmethod def _plan(): @@ -5094,7 +5091,6 @@ class TestAgenticFollowUpKeepsTheProviderPrefix: @pytest.mark.asyncio async def test_a_sub_path_model_still_resolves_a_provider(self): - """bedrock/mantle/... reaches the handler as mantle/..., which alone is unroutable.""" response = await self._run_followup("mantle/anthropic.claude-sonnet-5", "bedrock") assert response.choices[0].message.content == "ok from followup" From 70b45ead967b53a2947af5884b624592c99281c4 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sun, 27 Sep 2026 14:15:48 +0530 Subject: [PATCH 05/12] fix(websearch): leave a hook-qualified follow-up model on its own provider A hook that patches in its own model owns that choice, so re-qualifying it sent a cross-provider follow-up to the original request's provider instead. Only a bare patched model and the request's provider-stripped model take the prefix now, and the sibling agentic-loop path resolves the model the same way. --- .../chat_completion_agentic_loop.py | 5 ++-- litellm/litellm_core_utils/core_helpers.py | 13 ++++++++++ litellm/llms/custom_httpx/llm_http_handler.py | 4 +-- .../litellm_core_utils/test_core_helpers.py | 25 +++++++++++++++++++ .../custom_httpx/test_llm_http_handler.py | 15 +++++++++-- 5 files changed, 55 insertions(+), 7 deletions(-) diff --git a/litellm/litellm_core_utils/chat_completion_agentic_loop.py b/litellm/litellm_core_utils/chat_completion_agentic_loop.py index 9d7c9864e62..c804c4d0722 100644 --- a/litellm/litellm_core_utils/chat_completion_agentic_loop.py +++ b/litellm/litellm_core_utils/chat_completion_agentic_loop.py @@ -13,6 +13,7 @@ from litellm.litellm_core_utils.agentic_loop_settings import ( DEFAULT_MAX_AGENTIC_LOOPS, validated_max_agentic_loops, ) +from litellm.litellm_core_utils.core_helpers import qualify_agentic_followup_model from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObject from litellm.llms.base_llm.base_model_iterator import MockResponseIterator from litellm.types.integrations.custom_logger import ( @@ -170,9 +171,7 @@ async def _execute_chat_completion_agentic_plan( if patch.messages is None: raise ValueError("Agentic loop plan missing patched messages") - full_model_name = patch.model or model - if "/" not in full_model_name: - full_model_name = f"{custom_llm_provider}/{full_model_name}" + full_model_name: Final = qualify_agentic_followup_model(patch.model, model, custom_llm_provider) optional_params_for_followup: Final = {**optional_params, **patch.optional_params} if patch.tools is not None: diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 2472b9bf9b1..a758a6bb46a 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -38,6 +38,19 @@ def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str return f"{custom_llm_provider}/{model}" +def qualify_agentic_followup_model(patch_model: str | None, model: str, custom_llm_provider: str) -> str: + """Resolve the model an agentic follow-up re-dispatches. + + A hook that qualified its own patched model owns that choice, including a cross-provider + one, so only a bare patched model and the request's provider-stripped model get a prefix. + """ + if patch_model is None: + return qualify_provider_stripped_model(model, custom_llm_provider) + if "/" in patch_model: + return patch_model + return qualify_provider_stripped_model(patch_model, custom_llm_provider) + + def safe_divide_seconds(seconds: float, denominator: float, default: float | None = None) -> float | None: """ Safely divide seconds by denominator, handling zero division. diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 9d047670141..c9a74d2686a 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -5789,9 +5789,9 @@ class BaseLLMHTTPHandler: if patch.messages is None: raise ValueError("Agentic loop plan missing patched messages") - from litellm.litellm_core_utils.core_helpers import qualify_provider_stripped_model + from litellm.litellm_core_utils.core_helpers import qualify_agentic_followup_model - full_model_name: Final = qualify_provider_stripped_model(patch.model or model, custom_llm_provider) + full_model_name: Final = qualify_agentic_followup_model(patch.model, model, custom_llm_provider) optional_params_for_followup: Final = dict(optional_params) optional_params_for_followup.update(patch.optional_params) diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 803705b8a56..9964e1126ab 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -19,6 +19,7 @@ from litellm.litellm_core_utils.core_helpers import ( map_finish_reason, normalize_drop_params, process_response_headers, + qualify_agentic_followup_model, qualify_provider_stripped_model, reconstruct_model_name, redact_nested_match_and_regex_keys, @@ -620,3 +621,27 @@ class TestQualifyProviderStrippedModel: def test_no_provider_leaves_the_model_untouched(self): assert qualify_provider_stripped_model("gpt-4o", "") == "gpt-4o" + + +class TestQualifyAgenticFollowUpModel: + """A hook that qualified its own patched model owns that choice, so a cross-provider + follow-up must not be re-prefixed with the original request's provider.""" + + def test_a_cross_provider_patched_model_is_dispatched_as_the_hook_asked(self): + assert qualify_agentic_followup_model("anthropic/claude-sonnet-4-5", "gpt-4o", "openai") == ( + "anthropic/claude-sonnet-4-5" + ) + + def test_a_bare_patched_model_takes_the_request_provider(self): + assert qualify_agentic_followup_model("gpt-4o-mini", "gpt-4o", "openai") == "openai/gpt-4o-mini" + + def test_a_sub_path_request_model_keeps_its_provider(self): + assert qualify_agentic_followup_model(None, "mantle/anthropic.claude-sonnet-5", "bedrock") == ( + "bedrock/mantle/anthropic.claude-sonnet-5" + ) + + def test_an_unpatched_ordinary_model_takes_the_request_provider(self): + assert qualify_agentic_followup_model(None, "gpt-4o", "openai") == "openai/gpt-4o" + + def test_a_patched_model_already_holding_the_request_provider_is_left_alone(self): + assert qualify_agentic_followup_model("openai/gpt-4o", "gpt-4o", "openai") == "openai/gpt-4o" diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.py index 60b37452eca..56b49d7b922 100644 --- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py @@ -5073,11 +5073,15 @@ class TestAgenticFollowUpKeepsTheProviderPrefix: request_patch=AgenticLoopRequestPatch(messages=[{"role": "user", "content": "hi"}]), ) - async def _run_followup(self, model: str, custom_llm_provider: str): + async def _run_followup(self, model: str, custom_llm_provider: str, patched_model: str | None = None): from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler + plan = self._plan() + if patched_model is not None: + plan.request_patch.model = patched_model + return await BaseLLMHTTPHandler()._execute_chat_completion_agentic_plan( - plan=self._plan(), + plan=plan, model=model, messages=[{"role": "user", "content": "hi"}], optional_params={"mock_response": "ok from followup"}, @@ -5100,3 +5104,10 @@ class TestAgenticFollowUpKeepsTheProviderPrefix: response = await self._run_followup("gpt-4o-mini", "openai") assert response.choices[0].message.content == "ok from followup" + + @pytest.mark.asyncio + async def test_a_hook_that_patches_another_providers_model_reaches_that_provider(self): + response = await self._run_followup("gpt-4o", "openai", patched_model="anthropic/claude-sonnet-4-5") + + assert response.model == "claude-sonnet-4-5" + assert response._hidden_params["custom_llm_provider"] == "anthropic" From 68b378913188fdaad12f59b9882bad16fa7c248d Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Fri, 2 Oct 2026 11:16:31 +0530 Subject: [PATCH 06/12] fix(websearch): fall back to the request model when a patch carries no model An empty patched model dispatched a provider-only name instead of falling back, and a provider-stripped sub-path copied into the patch lost its prefix. A patched model now keeps its own provider only when its leading segment names one. --- litellm/litellm_core_utils/core_helpers.py | 20 ++++++++++++------- .../litellm_core_utils/test_core_helpers.py | 9 +++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index a758a6bb46a..55094197d7f 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -38,15 +38,21 @@ def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str return f"{custom_llm_provider}/{model}" -def qualify_agentic_followup_model(patch_model: str | None, model: str, custom_llm_provider: str) -> str: - """Resolve the model an agentic follow-up re-dispatches. +def names_a_provider(candidate: str) -> bool: + """Whether a model name's leading segment is a provider rather than part of the model.""" + import litellm + from litellm.litellm_core_utils.get_llm_provider_logic import is_registered_custom_provider - A hook that qualified its own patched model owns that choice, including a cross-provider - one, so only a bare patched model and the request's provider-stripped model get a prefix. - """ - if patch_model is None: + provider_names: Final = {getattr(provider, "value", provider) for provider in litellm.provider_list} + return candidate in provider_names or is_registered_custom_provider(candidate) + + +def qualify_agentic_followup_model(patch_model: str | None, model: str, custom_llm_provider: str) -> str: + """Resolve the model an agentic follow-up re-dispatches, keeping a hook's own provider.""" + if not patch_model: return qualify_provider_stripped_model(model, custom_llm_provider) - if "/" in patch_model: + leading_segment: Final = patch_model.split("/", 1)[0] + if leading_segment != patch_model and names_a_provider(leading_segment): return patch_model return qualify_provider_stripped_model(patch_model, custom_llm_provider) diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 9964e1126ab..faf916429a9 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -645,3 +645,12 @@ class TestQualifyAgenticFollowUpModel: def test_a_patched_model_already_holding_the_request_provider_is_left_alone(self): assert qualify_agentic_followup_model("openai/gpt-4o", "gpt-4o", "openai") == "openai/gpt-4o" + + @pytest.mark.parametrize("patched", ["", None]) + def test_a_patch_without_a_model_falls_back_to_the_request_model(self, patched): + assert qualify_agentic_followup_model(patched, "gpt-4o", "openai") == "openai/gpt-4o" + + def test_a_patched_sub_path_model_still_takes_the_request_provider(self): + assert qualify_agentic_followup_model("mantle/anthropic.claude-sonnet-5", "gpt-4o", "bedrock") == ( + "bedrock/mantle/anthropic.claude-sonnet-5" + ) From 577905e0223a8c492d7dd5d37e4fa0f2bac87ab4 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Fri, 2 Oct 2026 11:22:59 +0530 Subject: [PATCH 07/12] fix(websearch): keep the provider prefix on the responses follow-up model too The responses patch builder still used the old "a slash means already qualified" test, so a provider-stripped sub-path model reached the follow-up unqualified on that surface. It now resolves the model the same way the chat completion builder does. --- .../websearch_interception/handler.py | 10 ++-- .../test_websearch_responses.py | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.py index 84040280cd3..1a25842ba33 100644 --- a/litellm/integrations/websearch_interception/handler.py +++ b/litellm/integrations/websearch_interception/handler.py @@ -36,6 +36,7 @@ from litellm.integrations.websearch_interception.transformation import ( from litellm.litellm_core_utils.agentic_loop_settings import ( validated_max_agentic_loops, ) +from litellm.litellm_core_utils.core_helpers import qualify_provider_stripped_model from litellm.llms.base_llm.search.transformation import SearchResponse from litellm.types.integrations.custom_logger import ( CHAT_COMPLETION_AGENTIC_SURFACE, @@ -1242,9 +1243,10 @@ class WebSearchInterceptionLogger(CustomLogger): } } - full_model_name = model - if "/" not in model and isinstance(kwargs.get("custom_llm_provider"), str): - full_model_name = f"{kwargs['custom_llm_provider']}/{model}" + custom_llm_provider: Final = kwargs.get("custom_llm_provider") + full_model_name: Final = qualify_provider_stripped_model( + model, custom_llm_provider if isinstance(custom_llm_provider, str) else "" + ) verbose_logger.debug( "WebSearchInterception: Built responses request patch model=%s input_items=%d searches=%d", @@ -1910,8 +1912,6 @@ class WebSearchInterceptionLogger(CustomLogger): k: v for k, v in kwargs.items() if not k.startswith("_websearch_interception") and k not in internal_params } - from litellm.litellm_core_utils.core_helpers import qualify_provider_stripped_model - full_model_name: Final = qualify_provider_stripped_model(model, kwargs.get("custom_llm_provider", "")) verbose_logger.debug( diff --git a/tests/unit/integrations/websearch_interception/test_websearch_responses.py b/tests/unit/integrations/websearch_interception/test_websearch_responses.py index 00b7299cc31..8c5fd0daabe 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_responses.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_responses.py @@ -252,6 +252,52 @@ async def test_build_responses_plan_produces_responses_input(): assert patch_obj.model == "openai/gpt-4o" +@pytest.mark.asyncio +async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(): + """#38829 on the Responses surface: a provider-stripped model whose remainder still holds a + slash was left unqualified, so the follow-up could not resolve a provider.""" + logger = WebSearchInterceptionLogger(enabled_providers=[LlmProviders.BEDROCK]) + + tools_dict = { + "tool_calls": [ + { + "id": "fc_1", + "call_id": "fc_1", + "type": "function_call", + "name": "litellm_web_search", + "arguments": '{"query": "latest ai news"}', + "input": {"query": "latest ai news"}, + } + ], + "tool_type": "websearch", + "provider": "bedrock", + "response_format": "responses", + } + + with patch.object( + logger, + "_execute_search", + new=AsyncMock(return_value=("a search result", None)), + ): + plan = await logger.async_build_responses_agentic_loop_plan( + tools=tools_dict, + model="mantle/anthropic.claude-sonnet-5", + messages=[{"role": "user", "content": "What's the latest AI news?"}], + response=_responses_output_with_web_search(), + optional_params={"tools": [{"type": "function", "name": "litellm_web_search"}]}, + logging_obj=MagicMock(), + stream=False, + kwargs={ + "custom_llm_provider": "bedrock", + "_agentic_loop_api_surface": RESPONSES_AGENTIC_SURFACE, + }, + ) + + patch_obj = plan.request_patch + assert patch_obj is not None + assert patch_obj.model == "bedrock/mantle/anthropic.claude-sonnet-5" + + @pytest.mark.asyncio async def test_deployment_hook_converts_native_responses_web_search_tool(): """async_pre_call_deployment_hook converts a native Responses web_search tool From b1a14ff0de5ba9a88f5e0c8b32330621605172c0 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sat, 10 Oct 2026 11:19:25 +0530 Subject: [PATCH 08/12] fix(websearch): read custom_llm_provider without a member call on untyped kwargs --- litellm/integrations/websearch_interception/handler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.py index 1a25842ba33..cd1f6ba6fe6 100644 --- a/litellm/integrations/websearch_interception/handler.py +++ b/litellm/integrations/websearch_interception/handler.py @@ -1912,7 +1912,10 @@ class WebSearchInterceptionLogger(CustomLogger): k: v for k, v in kwargs.items() if not k.startswith("_websearch_interception") and k not in internal_params } - full_model_name: Final = qualify_provider_stripped_model(model, kwargs.get("custom_llm_provider", "")) + custom_llm_provider: Final = kwargs["custom_llm_provider"] if "custom_llm_provider" in kwargs else "" + full_model_name: Final = qualify_provider_stripped_model( + model, custom_llm_provider if isinstance(custom_llm_provider, str) else "" + ) verbose_logger.debug( "WebSearchInterception: Built chat completion request patch model=%s messages=%d", From cca1943b6ef65ae4ec13fe32e6fa9161edf2b46e Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sat, 10 Oct 2026 11:48:54 +0530 Subject: [PATCH 09/12] fix(websearch): type the chat patch builder kwargs instead of working around them --- litellm/integrations/websearch_interception/handler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/integrations/websearch_interception/handler.py b/litellm/integrations/websearch_interception/handler.py index cd1f6ba6fe6..3927bb8ecea 100644 --- a/litellm/integrations/websearch_interception/handler.py +++ b/litellm/integrations/websearch_interception/handler.py @@ -1826,7 +1826,7 @@ class WebSearchInterceptionLogger(CustomLogger): messages: list[dict], tool_calls: list[dict], optional_params: dict, - kwargs: dict, + kwargs: dict[str, object], response_format: str = "openai", ) -> AgenticLoopRequestPatch: """Execute litellm.search() and build chat-completion rerun patch.""" @@ -1912,7 +1912,7 @@ class WebSearchInterceptionLogger(CustomLogger): k: v for k, v in kwargs.items() if not k.startswith("_websearch_interception") and k not in internal_params } - custom_llm_provider: Final = kwargs["custom_llm_provider"] if "custom_llm_provider" in kwargs else "" + custom_llm_provider: Final = kwargs.get("custom_llm_provider") full_model_name: Final = qualify_provider_stripped_model( model, custom_llm_provider if isinstance(custom_llm_provider, str) else "" ) From df7155507a0726acebb426b1da25afbf42488635 Mon Sep 17 00:00:00 2001 From: Priyansh Nandwana Date: Sat, 10 Oct 2026 12:35:12 +0530 Subject: [PATCH 10/12] test(websearch): run the real search path and flatten the qualify helper tests --- .../test_websearch_responses.py | 40 +++---- .../litellm_core_utils/test_core_helpers.py | 106 +++++++++--------- 2 files changed, 75 insertions(+), 71 deletions(-) diff --git a/tests/unit/integrations/websearch_interception/test_websearch_responses.py b/tests/unit/integrations/websearch_interception/test_websearch_responses.py index 8c5fd0daabe..af6038d0455 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_responses.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_responses.py @@ -11,12 +11,16 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest +import litellm + from litellm.integrations.websearch_interception.handler import ( WebSearchInterceptionLogger, ) from litellm.types.integrations.custom_logger import ( RESPONSES_AGENTIC_SURFACE, ) +from litellm.llms.base_llm.search.transformation import SearchResponse +from litellm.proxy import proxy_server from litellm.types.utils import CallTypes, LlmProviders @@ -253,10 +257,12 @@ async def test_build_responses_plan_produces_responses_input(): @pytest.mark.asyncio -async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(): +async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(monkeypatch): """#38829 on the Responses surface: a provider-stripped model whose remainder still holds a slash was left unqualified, so the follow-up could not resolve a provider.""" logger = WebSearchInterceptionLogger(enabled_providers=[LlmProviders.BEDROCK]) + monkeypatch.setattr(proxy_server, "llm_router", None) + monkeypatch.setattr(litellm, "asearch", AsyncMock(return_value=SearchResponse(object="search", results=[]))) tools_dict = { "tool_calls": [ @@ -274,28 +280,24 @@ async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(): "response_format": "responses", } - with patch.object( - logger, - "_execute_search", - new=AsyncMock(return_value=("a search result", None)), - ): - plan = await logger.async_build_responses_agentic_loop_plan( - tools=tools_dict, - model="mantle/anthropic.claude-sonnet-5", - messages=[{"role": "user", "content": "What's the latest AI news?"}], - response=_responses_output_with_web_search(), - optional_params={"tools": [{"type": "function", "name": "litellm_web_search"}]}, - logging_obj=MagicMock(), - stream=False, - kwargs={ - "custom_llm_provider": "bedrock", - "_agentic_loop_api_surface": RESPONSES_AGENTIC_SURFACE, - }, - ) + plan = await logger.async_build_responses_agentic_loop_plan( + tools=tools_dict, + model="mantle/anthropic.claude-sonnet-5", + messages=[{"role": "user", "content": "What's the latest AI news?"}], + response=_responses_output_with_web_search(), + optional_params={"tools": [{"type": "function", "name": "litellm_web_search"}]}, + logging_obj=MagicMock(), + stream=False, + kwargs={ + "custom_llm_provider": "bedrock", + "_agentic_loop_api_surface": RESPONSES_AGENTIC_SURFACE, + }, + ) patch_obj = plan.request_patch assert patch_obj is not None assert patch_obj.model == "bedrock/mantle/anthropic.claude-sonnet-5" + litellm.asearch.assert_awaited_once() @pytest.mark.asyncio diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index faf916429a9..cb9d4a06544 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -590,67 +590,69 @@ class TestProviderResponseHeadersInHiddenParams: assert "additional_headers" not in sibling._hidden_params -class TestQualifyProviderStrippedModel: - """#38829: a model whose remainder still holds a slash must keep its provider prefix.""" +@pytest.mark.parametrize( + "model,provider,expected", + [ + ("mantle/anthropic.claude-sonnet-5", "bedrock", "bedrock/mantle/anthropic.claude-sonnet-5"), + ("invoke/anthropic.claude-v2", "bedrock", "bedrock/invoke/anthropic.claude-v2"), + ("openai/gpt-4o", "openrouter", "openrouter/openai/gpt-4o"), + ("gpt-4o", "openai", "openai/gpt-4o"), + ("claude-sonnet-4-5", "anthropic", "anthropic/claude-sonnet-4-5"), + ], +) +def test_the_provider_prefix_is_restored(model, provider, expected): + assert qualify_provider_stripped_model(model, provider) == expected - @pytest.mark.parametrize( - "model,provider,expected", - [ - ("mantle/anthropic.claude-sonnet-5", "bedrock", "bedrock/mantle/anthropic.claude-sonnet-5"), - ("invoke/anthropic.claude-v2", "bedrock", "bedrock/invoke/anthropic.claude-v2"), - ("openai/gpt-4o", "openrouter", "openrouter/openai/gpt-4o"), - ("gpt-4o", "openai", "openai/gpt-4o"), - ("claude-sonnet-4-5", "anthropic", "anthropic/claude-sonnet-4-5"), - ], + +@pytest.mark.parametrize( + "model,provider", + [ + ("bedrock/mantle/anthropic.claude-sonnet-5", "bedrock"), + ("openai/gpt-4o", "openai"), + ], +) +def test_an_already_qualified_model_is_left_alone(model, provider): + assert qualify_provider_stripped_model(model, provider) == model + + +def test_a_provider_that_only_shares_a_prefix_is_still_qualified(): + assert qualify_provider_stripped_model("openai_like/foo", "openai") == "openai/openai_like/foo" + + +def test_no_provider_leaves_the_model_untouched(): + assert qualify_provider_stripped_model("gpt-4o", "") == "gpt-4o" + + +def test_a_cross_provider_patched_model_is_dispatched_as_the_hook_asked(): + assert qualify_agentic_followup_model("anthropic/claude-sonnet-4-5", "gpt-4o", "openai") == ( + "anthropic/claude-sonnet-4-5" ) - def test_the_provider_prefix_is_restored(self, model, provider, expected): - assert qualify_provider_stripped_model(model, provider) == expected - @pytest.mark.parametrize( - "model,provider", - [ - ("bedrock/mantle/anthropic.claude-sonnet-5", "bedrock"), - ("openai/gpt-4o", "openai"), - ], + +def test_a_bare_patched_model_takes_the_request_provider(): + assert qualify_agentic_followup_model("gpt-4o-mini", "gpt-4o", "openai") == "openai/gpt-4o-mini" + + +def test_a_sub_path_request_model_keeps_its_provider(): + assert qualify_agentic_followup_model(None, "mantle/anthropic.claude-sonnet-5", "bedrock") == ( + "bedrock/mantle/anthropic.claude-sonnet-5" ) - def test_an_already_qualified_model_is_left_alone(self, model, provider): - assert qualify_provider_stripped_model(model, provider) == model - - def test_a_provider_that_only_shares_a_prefix_is_still_qualified(self): - assert qualify_provider_stripped_model("openai_like/foo", "openai") == "openai/openai_like/foo" - - def test_no_provider_leaves_the_model_untouched(self): - assert qualify_provider_stripped_model("gpt-4o", "") == "gpt-4o" -class TestQualifyAgenticFollowUpModel: - """A hook that qualified its own patched model owns that choice, so a cross-provider - follow-up must not be re-prefixed with the original request's provider.""" +def test_an_unpatched_ordinary_model_takes_the_request_provider(): + assert qualify_agentic_followup_model(None, "gpt-4o", "openai") == "openai/gpt-4o" - def test_a_cross_provider_patched_model_is_dispatched_as_the_hook_asked(self): - assert qualify_agentic_followup_model("anthropic/claude-sonnet-4-5", "gpt-4o", "openai") == ( - "anthropic/claude-sonnet-4-5" - ) - def test_a_bare_patched_model_takes_the_request_provider(self): - assert qualify_agentic_followup_model("gpt-4o-mini", "gpt-4o", "openai") == "openai/gpt-4o-mini" +def test_a_patched_model_already_holding_the_request_provider_is_left_alone(): + assert qualify_agentic_followup_model("openai/gpt-4o", "gpt-4o", "openai") == "openai/gpt-4o" - def test_a_sub_path_request_model_keeps_its_provider(self): - assert qualify_agentic_followup_model(None, "mantle/anthropic.claude-sonnet-5", "bedrock") == ( - "bedrock/mantle/anthropic.claude-sonnet-5" - ) - def test_an_unpatched_ordinary_model_takes_the_request_provider(self): - assert qualify_agentic_followup_model(None, "gpt-4o", "openai") == "openai/gpt-4o" +@pytest.mark.parametrize("patched", ["", None]) +def test_a_patch_without_a_model_falls_back_to_the_request_model(patched): + assert qualify_agentic_followup_model(patched, "gpt-4o", "openai") == "openai/gpt-4o" - def test_a_patched_model_already_holding_the_request_provider_is_left_alone(self): - assert qualify_agentic_followup_model("openai/gpt-4o", "gpt-4o", "openai") == "openai/gpt-4o" - @pytest.mark.parametrize("patched", ["", None]) - def test_a_patch_without_a_model_falls_back_to_the_request_model(self, patched): - assert qualify_agentic_followup_model(patched, "gpt-4o", "openai") == "openai/gpt-4o" - - def test_a_patched_sub_path_model_still_takes_the_request_provider(self): - assert qualify_agentic_followup_model("mantle/anthropic.claude-sonnet-5", "gpt-4o", "bedrock") == ( - "bedrock/mantle/anthropic.claude-sonnet-5" - ) +def test_a_patched_sub_path_model_still_takes_the_request_provider(): + assert qualify_agentic_followup_model("mantle/anthropic.claude-sonnet-5", "gpt-4o", "bedrock") == ( + "bedrock/mantle/anthropic.claude-sonnet-5" + ) From 6234434de69a800cbef0335145e34eb3af782cae Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 19:40:05 -0700 Subject: [PATCH 11/12] fix(websearch): route a hook's model on a JSON-configured provider and keep the request provider on an echoed model --- litellm/litellm_core_utils/core_helpers.py | 9 +++++++-- litellm/llms/custom_httpx/llm_http_handler.py | 7 ++++--- tests/unit/litellm_core_utils/test_core_helpers.py | 10 ++++++++++ tests/unit/llms/custom_httpx/test_llm_http_handler.py | 6 ++++++ 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index ae647e0cfc1..49ec398fa1d 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -42,14 +42,19 @@ def names_a_provider(candidate: str) -> bool: """Whether a model name's leading segment is a provider rather than part of the model.""" import litellm from litellm.litellm_core_utils.get_llm_provider_logic import is_registered_custom_provider + from litellm.llms.openai_like.json_loader import JSONProviderRegistry provider_names: Final = {getattr(provider, "value", provider) for provider in litellm.provider_list} - return candidate in provider_names or is_registered_custom_provider(candidate) + return ( + candidate in provider_names + or JSONProviderRegistry.exists(candidate) + or is_registered_custom_provider(candidate) + ) def qualify_agentic_followup_model(patch_model: str | None, model: str, custom_llm_provider: str) -> str: """Resolve the model an agentic follow-up re-dispatches, keeping a hook's own provider.""" - if not patch_model: + if not patch_model or patch_model == model: return qualify_provider_stripped_model(model, custom_llm_provider) leading_segment: Final = patch_model.split("/", 1)[0] if leading_segment != patch_model and names_a_provider(leading_segment): diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 7855ff1cffe..ea0e2a8a16b 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -48,7 +48,10 @@ from litellm.litellm_core_utils.audio_utils.subtitle_utils import ( SUBTITLE_RESPONSE_FORMATS, synthesize_subtitle_document, ) -from litellm.litellm_core_utils.core_helpers import set_provider_response_headers_in_hidden_params +from litellm.litellm_core_utils.core_helpers import ( + qualify_agentic_followup_model, + set_provider_response_headers_in_hidden_params, +) from litellm.litellm_core_utils.get_litellm_params import AWS_CREDENTIAL_KWARGS_KEYS from litellm.litellm_core_utils.llm_request_utils import serialize_multipart_form_fields from litellm.litellm_core_utils.realtime_errors import ( @@ -5847,8 +5850,6 @@ class BaseLLMHTTPHandler: if patch.messages is None: raise ValueError("Agentic loop plan missing patched messages") - from litellm.litellm_core_utils.core_helpers import qualify_agentic_followup_model - full_model_name: Final = qualify_agentic_followup_model(patch.model, model, custom_llm_provider) optional_params_for_followup: Final = dict(optional_params) diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index 2ddf15f5edc..55ce234c7a3 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -662,6 +662,16 @@ def test_a_patched_sub_path_model_still_takes_the_request_provider(): ) +def test_a_patched_model_on_a_json_configured_provider_reaches_that_provider(): + assert qualify_agentic_followup_model("aihubmix/gpt-4o", "gpt-4o", "openai") == "aihubmix/gpt-4o" + + +def test_a_patch_echoing_the_request_model_keeps_the_request_provider(): + assert qualify_agentic_followup_model("openai/gpt-4o", "openai/gpt-4o", "openrouter") == ( + "openrouter/openai/gpt-4o" + ) + + def test_spend_metadata_merge_survives_the_logging_thread_inserting_into_the_request_metadata() -> None: metadata: Final[dict[str, object]] = {} diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.py index 2b301c663c5..e662caee6a0 100644 --- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py @@ -5368,3 +5368,9 @@ class TestAgenticFollowUpKeepsTheProviderPrefix: assert response.model == "claude-sonnet-4-5" assert response._hidden_params["custom_llm_provider"] == "anthropic" + + @pytest.mark.asyncio + async def test_a_hook_echoing_an_org_style_model_stays_on_the_request_provider(self): + response = await self._run_followup("openai/gpt-4o", "openrouter", patched_model="openai/gpt-4o") + + assert response._hidden_params["custom_llm_provider"] == "openrouter" From 9665a15bf6fe8b78422c97ed34f5a18f4cd993be Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Tue, 20 Oct 2026 22:06:18 -0700 Subject: [PATCH 12/12] fix(websearch): leave a follow-up model alone when its prefix already routes to the request provider --- litellm/litellm_core_utils/core_helpers.py | 11 +++++ .../test_websearch_responses.py | 11 ++--- .../test_chat_completion_agentic_loop.py | 43 +++++++++++++++++++ .../litellm_core_utils/test_core_helpers.py | 29 +++++++------ 4 files changed, 76 insertions(+), 18 deletions(-) diff --git a/litellm/litellm_core_utils/core_helpers.py b/litellm/litellm_core_utils/core_helpers.py index 99209dcab45..62016b37edb 100644 --- a/litellm/litellm_core_utils/core_helpers.py +++ b/litellm/litellm_core_utils/core_helpers.py @@ -37,9 +37,20 @@ def qualify_provider_stripped_model(model: str, custom_llm_provider: str) -> str """Put the provider prefix back on a provider-stripped model.""" if not custom_llm_provider or model.startswith(f"{custom_llm_provider}/"): return model + if _already_routes_to(model, custom_llm_provider): + return model return f"{custom_llm_provider}/{model}" +def _already_routes_to(model: str, custom_llm_provider: str) -> bool: + from litellm.litellm_core_utils.get_llm_provider_logic import inferred_provider + + leading_segment: Final = model.split("/", 1)[0] + if leading_segment == model or not names_a_provider(leading_segment): + return False + return inferred_provider(model) == custom_llm_provider + + def names_a_provider(candidate: str) -> bool: """Whether a model name's leading segment is a provider rather than part of the model.""" import litellm diff --git a/tests/unit/integrations/websearch_interception/test_websearch_responses.py b/tests/unit/integrations/websearch_interception/test_websearch_responses.py index af6038d0455..feb3b0dd987 100644 --- a/tests/unit/integrations/websearch_interception/test_websearch_responses.py +++ b/tests/unit/integrations/websearch_interception/test_websearch_responses.py @@ -7,6 +7,7 @@ builds a Responses-format follow-up request. """ from types import SimpleNamespace +from typing import Final from unittest.mock import AsyncMock, MagicMock, patch import pytest @@ -257,14 +258,14 @@ async def test_build_responses_plan_produces_responses_input(): @pytest.mark.asyncio -async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(monkeypatch): +async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(monkeypatch: pytest.MonkeyPatch) -> None: """#38829 on the Responses surface: a provider-stripped model whose remainder still holds a slash was left unqualified, so the follow-up could not resolve a provider.""" - logger = WebSearchInterceptionLogger(enabled_providers=[LlmProviders.BEDROCK]) + logger: Final = WebSearchInterceptionLogger(enabled_providers=[LlmProviders.BEDROCK]) monkeypatch.setattr(proxy_server, "llm_router", None) monkeypatch.setattr(litellm, "asearch", AsyncMock(return_value=SearchResponse(object="search", results=[]))) - tools_dict = { + tools_dict: Final = { "tool_calls": [ { "id": "fc_1", @@ -280,7 +281,7 @@ async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(monke "response_format": "responses", } - plan = await logger.async_build_responses_agentic_loop_plan( + plan: Final = await logger.async_build_responses_agentic_loop_plan( tools=tools_dict, model="mantle/anthropic.claude-sonnet-5", messages=[{"role": "user", "content": "What's the latest AI news?"}], @@ -294,7 +295,7 @@ async def test_build_responses_plan_keeps_the_provider_on_a_sub_path_model(monke }, ) - patch_obj = plan.request_patch + patch_obj: Final = plan.request_patch assert patch_obj is not None assert patch_obj.model == "bedrock/mantle/anthropic.claude-sonnet-5" litellm.asearch.assert_awaited_once() diff --git a/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py b/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py index 3cbc3591009..6167e91c37f 100644 --- a/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py +++ b/tests/unit/litellm_core_utils/test_chat_completion_agentic_loop.py @@ -364,6 +364,49 @@ async def test_dispatcher_runs_followup_with_incremented_depth_and_patched_messa assert logger.cleanup_calls == 1 +@pytest.mark.asyncio +@pytest.mark.parametrize( + "model,custom_llm_provider,routed_model", + [ + ("cohere/command-a-03-2025", "cohere_chat", "command-a-03-2025"), + ("anthropic/claude-2", "anthropic_text", "claude-2"), + ], +) +async def test_sdk_followup_keeps_a_model_whose_prefix_already_routes_to_its_provider( + model: str, custom_llm_provider: str, routed_model: str +) -> None: + plan: Final = AgenticLoopPlan( + run_agentic_loop=True, + request_patch=AgenticLoopRequestPatch(messages=_patched_messages()), + ) + gate: Final = _GateOnlyLogger(plan=plan, tool_calls={"tool_calls": [{"id": "call_abc"}]}) + logging_obj: Final = Logging( + model=model, + messages=[{"role": "user", "content": "what is 6*7?"}], + stream=False, + call_type="acompletion", + start_time=datetime.datetime(2026, 1, 1, tzinfo=datetime.timezone.utc), + litellm_call_id="call-prefix", + function_id="fn-prefix", + dynamic_success_callbacks=[gate], + ) + + followup: Final = await maybe_run_chat_completion_agentic_loop( + response=_tool_call_model_response(), + model=model, + messages=[{"role": "user", "content": "what is 6*7?"}], + optional_params={}, + kwargs={"mock_response": "done"}, + logging_obj=logging_obj, + custom_llm_provider=custom_llm_provider, + stream=False, + ) + + assert isinstance(followup, ModelResponse) + assert followup.model == routed_model + assert followup._hidden_params["custom_llm_provider"] == custom_llm_provider + + @pytest.mark.asyncio async def test_dispatcher_followup_does_not_repeat_a_request_param_found_in_request_kwargs( restore_callbacks, diff --git a/tests/unit/litellm_core_utils/test_core_helpers.py b/tests/unit/litellm_core_utils/test_core_helpers.py index e0ccfb21df4..cc047a73285 100644 --- a/tests/unit/litellm_core_utils/test_core_helpers.py +++ b/tests/unit/litellm_core_utils/test_core_helpers.py @@ -647,7 +647,7 @@ class TestProviderResponseHeadersInHiddenParams: ("claude-sonnet-4-5", "anthropic", "anthropic/claude-sonnet-4-5"), ], ) -def test_the_provider_prefix_is_restored(model, provider, expected): +def test_the_provider_prefix_is_restored(model: str, provider: str, expected: str) -> None: assert qualify_provider_stripped_model(model, provider) == expected @@ -656,60 +656,63 @@ def test_the_provider_prefix_is_restored(model, provider, expected): [ ("bedrock/mantle/anthropic.claude-sonnet-5", "bedrock"), ("openai/gpt-4o", "openai"), + ("cohere/command-a-03-2025", "cohere_chat"), + ("ai21/jamba-1.5-large", "ai21_chat"), + ("anthropic/claude-2", "anthropic_text"), ], ) -def test_an_already_qualified_model_is_left_alone(model, provider): +def test_an_already_qualified_model_is_left_alone(model: str, provider: str) -> None: assert qualify_provider_stripped_model(model, provider) == model -def test_a_provider_that_only_shares_a_prefix_is_still_qualified(): +def test_a_provider_that_only_shares_a_prefix_is_still_qualified() -> None: assert qualify_provider_stripped_model("openai_like/foo", "openai") == "openai/openai_like/foo" -def test_no_provider_leaves_the_model_untouched(): +def test_no_provider_leaves_the_model_untouched() -> None: assert qualify_provider_stripped_model("gpt-4o", "") == "gpt-4o" -def test_a_cross_provider_patched_model_is_dispatched_as_the_hook_asked(): +def test_a_cross_provider_patched_model_is_dispatched_as_the_hook_asked() -> None: assert qualify_agentic_followup_model("anthropic/claude-sonnet-4-5", "gpt-4o", "openai") == ( "anthropic/claude-sonnet-4-5" ) -def test_a_bare_patched_model_takes_the_request_provider(): +def test_a_bare_patched_model_takes_the_request_provider() -> None: assert qualify_agentic_followup_model("gpt-4o-mini", "gpt-4o", "openai") == "openai/gpt-4o-mini" -def test_a_sub_path_request_model_keeps_its_provider(): +def test_a_sub_path_request_model_keeps_its_provider() -> None: assert qualify_agentic_followup_model(None, "mantle/anthropic.claude-sonnet-5", "bedrock") == ( "bedrock/mantle/anthropic.claude-sonnet-5" ) -def test_an_unpatched_ordinary_model_takes_the_request_provider(): +def test_an_unpatched_ordinary_model_takes_the_request_provider() -> None: assert qualify_agentic_followup_model(None, "gpt-4o", "openai") == "openai/gpt-4o" -def test_a_patched_model_already_holding_the_request_provider_is_left_alone(): +def test_a_patched_model_already_holding_the_request_provider_is_left_alone() -> None: assert qualify_agentic_followup_model("openai/gpt-4o", "gpt-4o", "openai") == "openai/gpt-4o" @pytest.mark.parametrize("patched", ["", None]) -def test_a_patch_without_a_model_falls_back_to_the_request_model(patched): +def test_a_patch_without_a_model_falls_back_to_the_request_model(patched: str | None) -> None: assert qualify_agentic_followup_model(patched, "gpt-4o", "openai") == "openai/gpt-4o" -def test_a_patched_sub_path_model_still_takes_the_request_provider(): +def test_a_patched_sub_path_model_still_takes_the_request_provider() -> None: assert qualify_agentic_followup_model("mantle/anthropic.claude-sonnet-5", "gpt-4o", "bedrock") == ( "bedrock/mantle/anthropic.claude-sonnet-5" ) -def test_a_patched_model_on_a_json_configured_provider_reaches_that_provider(): +def test_a_patched_model_on_a_json_configured_provider_reaches_that_provider() -> None: assert qualify_agentic_followup_model("aihubmix/gpt-4o", "gpt-4o", "openai") == "aihubmix/gpt-4o" -def test_a_patch_echoing_the_request_model_keeps_the_request_provider(): +def test_a_patch_echoing_the_request_model_keeps_the_request_provider() -> None: assert qualify_agentic_followup_model("openai/gpt-4o", "openai/gpt-4o", "openrouter") == ( "openrouter/openai/gpt-4o" )