litellm/tests/unit/responses/test_responses_prompt_management.py
yuneng-jiang a11a93f44a
test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit (#43199)
* ci: run the unit_selection.sh shard files on every event instead of only fork pull requests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* ci: rename fork-flag to unit-flag now that it applies on every event

* test: move tests/test_litellm root and small trees into tests/unit

Pure renames, no content changes. Follow-up commits in this PR fix
references, merge the three files that already existed in tests/unit,
keep live-provider tests in tests/test_litellm and wire CI.

* test: carry tests/test_litellm conftest isolation into tests/unit

Callback lists, routing fallbacks, cached HTTP clients, logger state, AWS,
proxy-URL and keychain env, and session-end client cleanup now reset for
unit tests too. The environment isolation owns its MonkeyPatch so a test's
own monkeypatch is undone before the model-cost teardown runs.

* test: merge, split and prune the moved root and small-tree tests

Merge batches/test_batch_utils.py and the chat_completions and messages
dispatch tests into the files that already existed in tests/unit. Keep
the live Gemini interactions tests, the async image-fetch format test and
the OpenAI embedding scorer test in tests/test_litellm since they need
real network or keys. Put test_router.py under tests/unit/test_router so
the existing package no longer shadows it. Delete eight tests the audit
found superseded by stronger ones kept in this move.

* ci: run the moved root and small-tree tests under their legacy flags

Add the misc and responses-caching-types flags to unit_selection.sh and
CircleCI, extend enterprise-routing and mcp-integration, and point the
legacy GHA shards, Makefile, redis-compat workflow, merge smoke manifest
and change classifier at the new paths.

* test: make the new tests/unit directories packages

tests/unit/test_package_layout.py requires every directory to carry an
__init__.py, and without one the moved and retained
test_litellm_responses_bridge.py modules collide on import.

* test: scope the unit socket block to tests/unit in shared sessions

The GHA shards collect the legacy test-path and the unit selection in one
pytest session. The unit conftest's loopback-only block leaked into legacy
modules that reach the network at import. The legacy conftest now lifts the
restriction at collect and setup time, and the unit conftest re-applies it
when collecting its own modules.

* test: move tests/test_litellm/llms into tests/unit/llms

Rename-only. Moves the provider tests and the fine-tuning fixtures they
load, mirroring the old paths. Follow-up commits merge, split and wire them.

* test: merge, split and prune the moved llms tests

Merges the Databricks chat transformation tests into the existing unit
file, keeps the tests that need real keys or the network in
tests/test_litellm, deletes the audited tests a stronger unit test
already covers, and points imports at tests.unit.llms.

* ci: run the moved llms tests under their legacy flags

The Vertex AI and All Other Providers shards keep their legacy test-path
for the retained files and add the llm-vertex-ai and llm-other-providers
unit selections. CircleCI gets matching unit jobs.

* test: make the tests/unit/llms directories packages

Adds __init__.py to the moved dirs and drops the legacy ones whose
directories no longer hold tests.

* test: drop script runners and path hacks the llms split left dangling

The __main__ runners in the split openai_like files and the Databricks e2e
runner called tests that now live in the other half of the split or were
deleted. The retained legacy halves also no longer need sys.path edits.

* test: give the shard-script tests their own GITHUB_OUTPUT

They only passed where the runner set it. The CircleCI unit job's env
allowlist drops it, so the script's redirect failed there.

* test: point the router and module-deletion checks at tests/unit

router_code_coverage and code_qa_check_tests only searched tests/test_litellm,
so the moved router tests no longer counted. The two silent-experiment tests
the audit deleted were the only direct callers of those methods; they are
replaced with tests that assert the forwarded shadow request and the
recursion guard.

* test: move tests/test_litellm integrations and secret_managers into tests/unit

Rename-only. Mirrors the old paths, including the directory conftests
and the prompt and JSON fixtures. Follow-up commits prune and wire them.

* test: prune and repoint the moved integrations tests

Deletes the 7 audited tests a stronger test in the same tree already
covers, imports the TLS sink helpers from their new conftest path, and
restores os.environ after each integrations test. Some presets write
OTEL_EXPORTER_OTLP_HEADERS straight into os.environ, and without the
legacy tree's test ordering that header leaked into the AgentOps tests.

* ci: run the moved integrations tests under their legacy flag

The integrations GHA shard and a new CircleCI job run the integrations
unit selection. secret_managers joins the misc selection.

* docs: point integrations and secret_managers references at tests/unit

* test: make the moved integrations directories packages

* test: keep the Databricks manual e2e runner and fix the SageMaker Nova run path

The Databricks e2e file is a manual script whose main() calls the tests
that were pruned, so pruning them broke the documented run. It is back to
its main version. The SageMaker Nova docstring now points at the file's
real location in tests/local_testing.

* test: move tests/test_litellm core utils, routing, responses, caching and rust_bridge into tests/unit

Rename-only. Mirrors the old paths, including fixtures, the stubtest config
and the native-route wheel script. Two files that collide with existing unit
files are merged in a follow-up commit.

* test: merge, prune and repoint the moved core, routing, responses, caching and rust_bridge tests

Merges the two files that collided with existing unit files, folding the
legacy extra case into test_is_chat_completion_cached_dict, and deletes the
9 audited tests a stronger test in the same file already covers.

Keeps what needs the network in tests/test_litellm: test_tokenizers pulls a
tokenizer from the Hugging Face hub, and the gpt2 and r50k_base tokenizer
cases download their BPE files. The unit core_utils conftest points
TIKTOKEN_CACHE_DIR at litellm's bundled encodings so the rest never depend on
import order to stay offline, and FakeSecretVault moves to a shared module
so both trees can build it.

* ci: run the moved core, routing, responses, caching and rust_bridge tests under their flags

core_utils gets a core-utils flag and CircleCI job, and its GHA shard keeps
the legacy path for the retained network tests. router_utils and
router_strategy join enterprise-routing, responses joins
responses-caching-types (minus responses/mcp, which mcp-integration owns),
caching joins caching-local and rust_bridge joins misc. The redis-compat,
test-rust, stubtest and merge-smoke paths follow the move.

* docs: point the Rust crate references at tests/unit

* test: make the moved core, routing and rust_bridge directories packages

* test: keep the no-loop DualCache batch_get_cache regression test

It runs the sync path outside any event loop, which the inside-loop test
cannot, so a change that picks the Redis client by loop state would only
show up there.

* test: keep the job's UNIT_FLAG out of the shard-script tests

* fix(url_utils): block 192.0.0.0/24 on every Python patch release

* test: move the new budget limiter tests into tests/unit/router_strategy

* test: move the new sentry scrubbing tests into tests/unit/litellm_core_utils

* test: move the new zerobus tests into tests/unit/integrations

* test: make tests/unit/integrations/zerobus a package

* test: load litellm's own tiktoken cache setup once instead of resetting it per test

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 17:10:13 -07:00

642 lines
24 KiB
Python

"""
Unit tests for prompt management support in the Responses API.
Covers:
A) str input is coerced to a message list before merging with the template
B) list input is merged with the template
C) no prompt_id → hook is skipped, input is unchanged
D) model override from the prompt template is applied
E) prompt_template_optional_params flow into the request
F) non-message items in input are filtered out
G) model override re-resolves provider
H) async path calls async_get_chat_completion_prompt
I) async path propagates optional params to downstream handler
"""
from importlib import import_module
import asyncio
from typing import List, cast
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from litellm.integrations.anthropic_cache_control_hook import (
AnthropicCacheControlHook,
)
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.types.llms.openai import (
AllMessageValues,
ResponseInputParam,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _make_logging_obj(
merged_model: str,
merged_messages: List[AllMessageValues],
should_run: bool = True,
merged_optional_params: dict = None,
) -> MagicMock:
"""Return a mock LiteLLMLoggingObj pre-configured for prompt management."""
if merged_optional_params is None:
merged_optional_params = {}
logging_obj = MagicMock()
logging_obj.__class__ = LiteLLMLoggingObj
logging_obj.should_run_prompt_management_hooks.return_value = should_run
prompt_return = (merged_model, merged_messages, merged_optional_params)
logging_obj.get_chat_completion_prompt.return_value = prompt_return
logging_obj.async_get_chat_completion_prompt = AsyncMock(return_value=prompt_return)
logging_obj.model_call_details = {}
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.object(
import_module("litellm.responses.main").litellm, "get_llm_provider",
side_effect=_provider_by_model,
),
patch.object(
import_module("litellm.responses.mcp.litellm_proxy_mcp_handler").LiteLLM_Proxy_MCP_Handler, "_should_use_litellm_mcp_gateway",
return_value=False,
),
patch.object(
import_module("litellm.responses.main").ProviderConfigManager, "get_provider_responses_api_config",
return_value=None,
),
patch.object(
import_module("litellm.responses.main").litellm_completion_transformation_handler, "response_api_handler",
return_value=MagicMock(),
),
]
def _make_cache_control_case() -> tuple[
ResponseInputParam,
list[AllMessageValues],
dict[str, object],
]:
system_message = cast(
AllMessageValues,
{"role": "system", "content": "Analyze the request"},
)
assistant_message = cast(
AllMessageValues,
{
"type": "message",
"id": "msg_1",
"role": "assistant",
"status": "completed",
"content": [
{
"type": "output_text",
"text": "The code has a bug",
"annotations": [],
}
],
},
)
user_message = cast(
AllMessageValues,
{"role": "user", "content": "Check for security issues"},
)
reasoning_item = {
"type": "reasoning",
"id": "rs_1",
"summary": [],
"encrypted_content": "encrypted",
}
original_input = cast(
ResponseInputParam,
[system_message, reasoning_item, assistant_message, user_message],
)
_, merged_messages, _ = AnthropicCacheControlHook().get_chat_completion_prompt(
model="azure/gpt-5-codex",
messages=[system_message, assistant_message, user_message],
non_default_params={"cache_control_injection_points": [{"location": "message", "role": "system"}]},
prompt_id=None,
prompt_variables=None,
dynamic_callback_params={},
)
return original_input, merged_messages, reasoning_item
# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
class TestResponsesAPIPromptManagement:
def test_str_input_coerced_and_merged(self):
"""[A] str input is wrapped into a message list before being passed to the hook."""
template_messages: List[AllMessageValues] = [
{"role": "system", "content": "You are a summariser."}, # type: ignore[list-item]
]
client_message: List[AllMessageValues] = [
{"role": "user", "content": "Tell me about AI."}, # type: ignore[list-item]
]
expected_merged = template_messages + client_message
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=expected_merged,
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3]:
import litellm
litellm.responses(
input="Tell me about AI.",
model="gpt-4o",
prompt_id="summariser-prompt",
prompt_variables={},
litellm_logging_obj=logging_obj,
)
logging_obj.get_chat_completion_prompt.assert_called_once()
call_kwargs = logging_obj.get_chat_completion_prompt.call_args.kwargs
# str was coerced to a single user message before being passed to the hook
assert call_kwargs["messages"] == [
{"role": "user", "content": "Tell me about AI."}
]
assert call_kwargs["prompt_id"] == "summariser-prompt"
def test_list_input_merged_with_template(self):
"""[B] list input is passed directly to the hook and merged with the template."""
template_messages: List[AllMessageValues] = [
{"role": "system", "content": "You are helpful."}, # type: ignore[list-item]
]
client_messages = [
{"role": "user", "content": [{"type": "input_text", "text": "Hello"}]},
]
expected_merged = template_messages + client_messages # type: ignore[operator]
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=expected_merged, # type: ignore[arg-type]
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3]:
import litellm
litellm.responses(
input=client_messages, # type: ignore[arg-type]
model="gpt-4o",
prompt_id="helper-prompt",
litellm_logging_obj=logging_obj,
)
logging_obj.get_chat_completion_prompt.assert_called_once()
call_kwargs = logging_obj.get_chat_completion_prompt.call_args.kwargs
assert call_kwargs["messages"] == client_messages
def test_no_prompt_id_skips_hook(self):
"""[C] When prompt_id is absent, prompt management hooks are not called."""
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=[],
should_run=False,
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3]:
import litellm
litellm.responses(
input="Hello",
model="gpt-4o",
litellm_logging_obj=logging_obj,
)
logging_obj.get_chat_completion_prompt.assert_not_called()
def test_optional_params_from_template_applied(self):
"""[E] prompt_template_optional_params (e.g. temperature) flow into the request."""
template_messages: List[AllMessageValues] = [
{"role": "user", "content": "Hello"}, # type: ignore[list-item]
]
# Simulate get_chat_completion_prompt returning merged optional params
# that include a template-defined temperature
merged_kwargs = {"temperature": 0.2}
logging_obj = MagicMock()
logging_obj.__class__ = LiteLLMLoggingObj
logging_obj.should_run_prompt_management_hooks.return_value = True
logging_obj.get_chat_completion_prompt.return_value = (
"openai/gpt-4o",
template_messages,
merged_kwargs,
)
logging_obj.model_call_details = {}
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3] as mock_handler:
import litellm
litellm.responses(
input="Hello",
model="gpt-4o",
prompt_id="t",
litellm_logging_obj=logging_obj,
)
# temperature from the template should reach the downstream handler via local_vars
handler_call_kwargs = mock_handler.call_args.kwargs
request_params = handler_call_kwargs.get("responses_api_request", {})
assert request_params.get("temperature") == 0.2
def test_model_override_from_template(self):
"""[D] Model returned by the prompt hook overrides the original request model."""
template_messages: List[AllMessageValues] = [
{"role": "user", "content": "{{query}}"}, # type: ignore[list-item]
]
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o-mini", # overridden model from template
merged_messages=template_messages,
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3] as mock_handler:
import litellm
litellm.responses(
input="What is AI?",
model="gpt-4o",
prompt_id="query-prompt",
prompt_variables={"query": "What is AI?"},
litellm_logging_obj=logging_obj,
)
# 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") == "gpt-4o-mini"
def test_non_message_input_items_filtered(self):
"""[F] Non-message items in ResponseInputParam (e.g. function_call_output) are
filtered out before being passed to the prompt hook, avoiding malformed merges.
"""
template_messages: List[AllMessageValues] = [
{"role": "system", "content": "You are helpful."}, # type: ignore[list-item]
]
mixed_input = [
{"role": "user", "content": "Hello"},
{"type": "function_call_output", "call_id": "abc", "output": "42"},
]
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=template_messages + [{"role": "user", "content": "Hello"}], # type: ignore[operator]
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3]:
import litellm
litellm.responses(
input=mixed_input, # type: ignore[arg-type]
model="gpt-4o",
prompt_id="filter-test",
litellm_logging_obj=logging_obj,
)
call_kwargs = logging_obj.get_chat_completion_prompt.call_args.kwargs
passed_messages = call_kwargs["messages"]
assert all(isinstance(m, dict) and "role" in m for m in passed_messages)
assert len(passed_messages) == 1
def test_cache_control_hook_preserves_reasoning_items(self):
original_input, merged_messages, reasoning_item = _make_cache_control_case()
logging_obj = _make_logging_obj(
merged_model="azure/gpt-5-codex",
merged_messages=merged_messages,
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3] as mock_handler:
import litellm
litellm.responses(
input=original_input,
model="azure/gpt-5-codex",
litellm_logging_obj=logging_obj,
cache_control_injection_points=[{"location": "message", "role": "system"}],
)
sent_input = mock_handler.call_args.kwargs["input"]
assert [item.get("type") for item in sent_input] == [
None,
"reasoning",
"message",
None,
]
assert sent_input[0]["cache_control"] == {"type": "ephemeral"}
assert sent_input[1] == reasoning_item
assert sent_input[2]["id"] == "msg_1"
def test_all_non_message_input_items_remain_unchanged(self):
reasoning_item = {
"type": "reasoning",
"id": "rs_1",
"summary": [],
"encrypted_content": "encrypted",
}
original_input = cast(ResponseInputParam, [reasoning_item])
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=[
cast(
AllMessageValues,
{"role": "system", "content": "Analyze the request"},
)
],
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3] as mock_handler:
import litellm
litellm.responses(
input=original_input,
model="gpt-4o",
prompt_id="all-non-message",
litellm_logging_obj=logging_obj,
)
assert mock_handler.call_args.kwargs["input"] == original_input
def test_model_override_re_resolves_provider(self):
"""[G] When the prompt template overrides the model to a different provider,
custom_llm_provider is re-resolved so downstream routing uses the correct provider.
"""
template_messages: List[AllMessageValues] = [
{"role": "user", "content": "Hi"}, # type: ignore[list-item]
]
logging_obj = _make_logging_obj(
merged_model="anthropic/claude-3-5-sonnet",
merged_messages=template_messages,
)
patches = _patch_responses_dispatch()
with (
patch.object(
import_module("litellm.responses.main").litellm, "get_llm_provider",
side_effect=_provider_by_model,
),
patches[1],
patches[2],
patches[3] as mock_handler,
):
import litellm
litellm.responses(
input="Hi",
model="gpt-4o",
prompt_id="cross-provider",
litellm_logging_obj=logging_obj,
)
handler_call_kwargs = mock_handler.call_args.kwargs
assert handler_call_kwargs.get("custom_llm_provider") == "anthropic"
class TestAsyncResponsesAPIPromptManagement:
"""Tests for the async aresponses() prompt management path.
aresponses() calls async_get_chat_completion_prompt at the outer async
level, then pops prompt_id from kwargs and passes merged_optional_params
via an internal kwarg. The sync responses() path sees no prompt_id and
skips the sync hook entirely — preventing double-merge of template messages.
"""
@pytest.mark.asyncio
async def test_async_calls_async_hook_not_sync(self):
"""[H] aresponses() invokes async_get_chat_completion_prompt and the
sync get_chat_completion_prompt is NOT called (no double-merge)."""
template_messages: List[AllMessageValues] = [
{"role": "system", "content": "You are helpful."}, # type: ignore[list-item]
]
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=template_messages + [{"role": "user", "content": "Hi"}], # type: ignore[list-item]
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3]:
import litellm
await litellm.aresponses(
input="Hi",
model="gpt-4o",
prompt_id="async-test",
prompt_variables={},
litellm_logging_obj=logging_obj,
)
logging_obj.async_get_chat_completion_prompt.assert_called_once()
logging_obj.get_chat_completion_prompt.assert_not_called()
call_kwargs = logging_obj.async_get_chat_completion_prompt.call_args.kwargs
assert call_kwargs["prompt_id"] == "async-test"
@pytest.mark.asyncio
async def test_async_optional_params_propagated(self):
"""[I] Template-defined optional params (e.g. temperature) from the async
hook reach the downstream handler — they are NOT silently discarded."""
template_messages: List[AllMessageValues] = [
{"role": "user", "content": "Hello"}, # type: ignore[list-item]
]
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=template_messages,
merged_optional_params={"temperature": 0.7},
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3] as mock_handler:
import litellm
await litellm.aresponses(
input="Hello",
model="gpt-4o",
prompt_id="async-temp",
litellm_logging_obj=logging_obj,
)
logging_obj.get_chat_completion_prompt.assert_not_called()
handler_call_kwargs = mock_handler.call_args.kwargs
request_params = handler_call_kwargs.get("responses_api_request", {})
assert request_params.get("temperature") == 0.7
@pytest.mark.asyncio
async def test_async_non_message_items_filtered(self):
"""[J] Non-message items are filtered in the async path too."""
template_messages: List[AllMessageValues] = [
{"role": "system", "content": "Be helpful."}, # type: ignore[list-item]
]
mixed_input = [
{"role": "user", "content": "Hello"},
{"type": "function_call_output", "call_id": "abc", "output": "42"},
]
logging_obj = _make_logging_obj(
merged_model="openai/gpt-4o",
merged_messages=template_messages + [{"role": "user", "content": "Hello"}], # type: ignore[operator]
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3]:
import litellm
await litellm.aresponses(
input=mixed_input, # type: ignore[arg-type]
model="gpt-4o",
prompt_id="async-filter",
litellm_logging_obj=logging_obj,
)
logging_obj.async_get_chat_completion_prompt.assert_called_once()
logging_obj.get_chat_completion_prompt.assert_not_called()
call_kwargs = logging_obj.async_get_chat_completion_prompt.call_args.kwargs
passed_messages = call_kwargs["messages"]
assert all(isinstance(m, dict) and "role" in m for m in passed_messages)
assert len(passed_messages) == 1
@pytest.mark.asyncio
async def test_async_cache_control_hook_preserves_reasoning_items(self):
original_input, merged_messages, reasoning_item = _make_cache_control_case()
logging_obj = _make_logging_obj(
merged_model="azure/gpt-5-codex",
merged_messages=merged_messages,
)
patches = _patch_responses_dispatch()
with patches[0], patches[1], patches[2], patches[3] as mock_handler:
import litellm
await litellm.aresponses(
input=original_input,
model="azure/gpt-5-codex",
litellm_logging_obj=logging_obj,
cache_control_injection_points=[{"location": "message", "role": "system"}],
)
sent_input = mock_handler.call_args.kwargs["input"]
assert [item.get("type") for item in sent_input] == [
None,
"reasoning",
"message",
None,
]
assert sent_input[0]["cache_control"] == {"type": "ephemeral"}
assert sent_input[1] == reasoning_item
assert sent_input[2]["id"] == "msg_1"
# ---------------------------------------------------------------------------
# Cross-provider model swap guard (prompt swaps model after credential resolution)
# ---------------------------------------------------------------------------
def test_resolve_prompt_swapped_provider_raises_cross_provider_with_credentials():
import litellm
from litellm.responses.main import _resolve_prompt_swapped_provider
with pytest.raises(litellm.BadRequestError, match="Refusing to send"):
_resolve_prompt_swapped_provider(
original_model="anthropic/claude-haiku-4-5",
swapped_model="gpt-4o-mini",
custom_llm_provider="anthropic",
kwargs={"api_key": "sk-ant-test"},
prompt_id="p1",
)
def test_resolve_prompt_swapped_provider_allows_swap_without_credentials():
from litellm.responses.main import _resolve_prompt_swapped_provider
assert (
_resolve_prompt_swapped_provider(
original_model="anthropic/claude-haiku-4-5",
swapped_model="gpt-4o-mini",
custom_llm_provider="anthropic",
kwargs={},
prompt_id="p1",
)
== "openai"
)
def test_resolve_prompt_swapped_provider_allows_same_provider_swap_with_credentials():
from litellm.responses.main import _resolve_prompt_swapped_provider
assert (
_resolve_prompt_swapped_provider(
original_model="openai/gpt-4o",
swapped_model="gpt-4o-mini",
custom_llm_provider="openai",
kwargs={"api_key": "sk-test", "api_base": "https://api.openai.com/v1"},
prompt_id="p1",
)
== "openai"
)
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.object( # test-quality-ok: handler boundary stub proves creds resolve for the swapped provider without network
import_module("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
logging_obj = _make_logging_obj("gpt-4o-mini", [{"role": "user", "content": "hi"}])
with pytest.raises(litellm.BadRequestError, match="Refusing to send"):
_apply_prompt_management_to_responses_call(
input="hi",
model="anthropic/claude-haiku-4-5",
custom_llm_provider="anthropic",
litellm_logging_obj=logging_obj,
kwargs={"prompt_id": "p1", "api_key": "sk-ant-test"},
local_vars={},
use_chat_completions_api=False,
)
@pytest.mark.asyncio
async def test_aresponses_prompt_swap_cross_provider_with_credentials_raises():
import litellm
logging_obj = _make_logging_obj("gpt-4o-mini", [{"role": "user", "content": "hi"}])
logging_obj.async_failure_handler = AsyncMock()
with pytest.raises(litellm.BadRequestError, match="Refusing to send"):
await litellm.aresponses(
input="hi",
model="anthropic/claude-haiku-4-5",
litellm_logging_obj=logging_obj,
prompt_id="p1",
api_key="sk-ant-test",
)