Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(responses): run prompt hook before provider credential resolution in sync responses()

mateo-berrif824ca7433b0
diff --git a/litellm/responses/main.py b/litellm/responses/main.py
index 0aa40371a39..2cad68e3c11 100644
--- a/litellm/responses/main.py
+++ b/litellm/responses/main.py
@@ -961,6 +961,21 @@ def responses(
# Update local_vars to include the converted text parameter
local_vars["text"] = text
+ #########################################################
+ # PROMPT MANAGEMENT
+ # If aresponses() already ran the async hook, it pops prompt_id and
+ # passes the result via _async_prompt_merged_params — apply those
+ # directly and skip the sync hook to avoid double-merging.
+ #########################################################
+ input, model, custom_llm_provider = _apply_prompt_management_to_responses_call(
+ input=input,
+ model=model,
+ custom_llm_provider=custom_llm_provider,
+ litellm_logging_obj=litellm_logging_obj,
+ kwargs=kwargs,
+ local_vars=local_vars,
+ )
+
# get llm provider logic
litellm_params: Final = GenericLiteLLMParams(**kwargs)
@@ -982,21 +997,6 @@ def responses(
local_vars=local_vars,
)
- #########################################################
- # PROMPT MANAGEMENT
- # If aresponses() already ran the async hook, it pops prompt_id and
- # passes the result via _async_prompt_merged_params — apply those
- # directly and skip the sync hook to avoid double-merging.
- #########################################################
- input, model, custom_llm_provider = _apply_prompt_management_to_responses_call(
- input=input,
- model=model,
- custom_llm_provider=custom_llm_provider,
- litellm_logging_obj=litellm_logging_obj,
- kwargs=kwargs,
- local_vars=local_vars,
- )
-
#########################################################
# Update input and tools with provider-specific file IDs if managed files are used
#########################################################
diff --git a/tests/test_litellm/integrations/dotprompt/test_prompt_manager.py b/tests/test_litellm/integrations/dotprompt/test_prompt_manager.py
index fc7b55d4763..c33e7e35ffa 100644
--- a/tests/test_litellm/integrations/dotprompt/test_prompt_manager.py
+++ b/tests/test_litellm/integrations/dotprompt/test_prompt_manager.py
@@ -12,7 +12,9 @@ from unittest.mock import MagicMock, Mock, patch
import httpx
import litellm
+from litellm.integrations.dotprompt.dotprompt_manager import DotpromptManager
from litellm.integrations.dotprompt.prompt_manager import PromptManager, PromptTemplate
+from litellm.types.prompts.init_prompts import PromptLiteLLMParams, PromptSpec
def test_prompt_manager_initialization():
@@ -579,10 +581,7 @@ async def test_dotprompt_with_prompt_version():
assert "Test v2" in v2_rendered
-def _swap_prompt_manager_and_spec(ignore_prompt_manager_model: bool):
- from litellm.integrations.dotprompt.dotprompt_manager import DotpromptManager
- from litellm.types.prompts.init_prompts import PromptLiteLLMParams, PromptSpec
-
+def _swap_prompt_manager_and_spec(ignore_prompt_manager_model: bool) -> tuple[DotpromptManager, PromptSpec]:
manager = DotpromptManager(
prompt_data={"content": "You are a pirate assistant.", "metadata": {"model": "gpt-4o-mini"}},
prompt_id="swap-prompt",
diff --git a/tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py b/tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py
index 66971df76d2..e99e34d65d4 100644
--- a/tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py
+++ b/tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py
@@ -847,7 +847,7 @@ async def test_process_prompt_template_aresponses_swaps_model_and_merges_input(p
{},
)
)
- data: Dict[str, Any] = {"input": "Who are you?", "model": "anthropic-haiku-4-5", "prompt_id": "x"}
+ data: dict[str, object] = {"input": "Who are you?", "model": "anthropic-haiku-4-5", "prompt_id": "x"}
await proxy_logging._process_prompt_template(
data=data,
litellm_logging_obj=logging_obj,
diff --git a/tests/test_litellm/responses/test_responses_prompt_management.py b/tests/test_litellm/responses/test_responses_prompt_management.py
index c6880c55a7a..4610e56733f 100644
--- a/tests/test_litellm/responses/test_responses_prompt_management.py
+++ b/tests/test_litellm/responses/test_responses_prompt_management.py
@@ -52,12 +52,19 @@ def _make_logging_obj(
return logging_obj
+def _provider_by_model(model: str, **_: object) -> tuple[str, str, None, None]:
+ provider, _, bare_model = model.partition("/")
+ if not bare_model:
+ return (model, "anthropic" if "claude" in model else "openai", None, None)
+ return (bare_model, provider, None, None)
+
+
def _patch_responses_dispatch():
"""Patch everything after the prompt management block so tests stay unit-level."""
return [
patch(
"litellm.responses.main.litellm.get_llm_provider",
- return_value=("gpt-4o", "openai", None, None),
+ side_effect=_provider_by_model,
),
patch(
"litellm.responses.mcp.litellm_proxy_mcp_handler."
@@ -278,7 +285,7 @@ class TestResponsesAPIPromptManagement:
# The model passed to the downstream handler should be the overridden one
handler_call_kwargs = mock_handler.call_args.kwargs
- assert handler_call_kwargs.get("model") == "openai/gpt-4o-mini"
+ assert handler_call_kwargs.get("model") == "gpt-4o-mini"
def test_non_message_input_items_filtered(self):
"""[F] Non-message items in ResponseInputParam (e.g. function_call_output) are
@@ -388,10 +395,7 @@ class TestResponsesAPIPromptManagement:
with (
patch(
"litellm.responses.main.litellm.get_llm_provider",
- side_effect=[
- ("gpt-4o", "openai", None, None),
- ("claude-3-5-sonnet", "anthropic", None, None),
- ],
+ side_effect=_provider_by_model,
),
patches[1],
patches[2],
@@ -590,6 +594,23 @@ def test_resolve_prompt_swapped_provider_allows_same_provider_swap_with_credenti
)
+def test_sync_prompt_swap_resolves_credentials_for_swapped_provider(monkeypatch: pytest.MonkeyPatch):
+ import litellm
+
+ monkeypatch.setenv("XAI_API_KEY", "sk-xai-test")
+ logging_obj = _make_logging_obj("gpt-4o-mini", [{"role": "user", "content": "hi"}])
+ with patch( # test-quality-ok: handler boundary stub proves creds resolve for the swapped provider without network
+ "litellm.responses.main.base_llm_http_handler.response_api_handler", return_value=MagicMock()
+ ) as mock_handler:
+ litellm.responses(input="hi", model="xai/grok-4", prompt_id="p1", litellm_logging_obj=logging_obj)
+
+ handler_kwargs = mock_handler.call_args.kwargs
+ assert handler_kwargs["model"] == "gpt-4o-mini"
+ assert handler_kwargs["custom_llm_provider"] == "openai"
+ assert handler_kwargs["litellm_params"].api_base is None
+ assert handler_kwargs["litellm_params"].api_key != "sk-xai-test"
+
+
def test_sync_prompt_swap_cross_provider_with_credentials_raises():
import litellm
from litellm.responses.main import _apply_prompt_management_to_responses_call