litellm/tests/test_litellm_rust/messages/test_requests.py
linhqm 26dfc09086
fix(proxy): resolve admin oidc api_key references on batch cancel, fine-tuning listing, OCR, /v1/messages and cache prediction (#53678)
* fix: resolve admin oidc/ api_key references on batch cancel, fine-tuning list, OCR, cache prediction and Rust messages

Batch cancel, the fine-tuning job list, OCR, the Anthropic cache prediction and
Anthropic /v1/messages on the Rust path sent an admin-set oidc/ api_key reference
upstream as the literal string. Each now reads the secret behind it per call, the
way chat does, and a deployment carrying one keeps /v1/messages on Python.

* test(rust-bridge): expect native OCR to copy named credential values per call, as chat does

* fix(proxy): stop the autorouter baseline lookup from holding a request on a slow metadata server

* fix(proxy): catch asyncio.TimeoutError in the autorouter baseline hook so Python 3.10 keeps its timeout records

* fix: keep the admin api_key reference on batch list and retrieve, wildcard credentials and decisions and extraction health checks

* fix(proxy): never hold a request or routing check on a Google token fetch for cache estimates

* fix(proxy): resolve admin api_key references on wildcard health checks, cursor and Gemini RAG credentials

Native routes now swap only the resolved api_key into the stored credential values, so values that
cannot be deep copied survive and an unknown credential name warns once

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-23 10:11:10 -08:00

179 lines
6.4 KiB
Python

from typing import Final
import pytest
import litellm
from litellm.types.utils import admin_api_key_secret_reference
from tests.test_litellm_rust.messages.data import MESSAGES, MESSAGES_RESPONSE, call
from tests.test_litellm_rust.support.recording_server import RecordingServer, ResponseSpec
from tests.test_litellm_rust.support.routing import assert_served_natively
pytestmark = pytest.mark.requires_rust_extension
CAPABILITY_MODEL: Final = "claude-test-runtime-capabilities"
def sent(server: RecordingServer) -> tuple[dict[str, object], dict[str, str]]:
assert_served_natively(server)
request: Final = server.requests[0]
assert isinstance(request.body, dict)
return request.body, request.headers
def capabilities(monkeypatch: pytest.MonkeyPatch, *, sampling: bool) -> None:
monkeypatch.setitem(
litellm.model_cost,
CAPABILITY_MODEL,
{"litellm_provider": "anthropic", "mode": "chat", "supports_sampling_params": sampling},
)
@pytest.mark.asyncio
async def test_replayed_tool_ids_are_sanitized_before_they_reach_the_provider(server: RecordingServer) -> None:
history: Final = [
{"role": "user", "content": "run it"},
{
"role": "assistant",
"content": [
{"type": "text", "text": ""},
{
"type": "tool_use",
"id": "functions.Bash:0",
"name": "Bash",
"input": {},
"provider_specific_fields": {"x": 1},
},
],
},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "functions.Bash:0", "content": "ok"}]},
]
await call(server, messages=history)
body, _ = sent(server)
assert body["messages"] == [
{"role": "user", "content": "run it"},
{"role": "assistant", "content": [{"type": "tool_use", "id": "functions_Bash_0", "name": "Bash", "input": {}}]},
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "functions_Bash_0", "content": "ok"}]},
]
@pytest.mark.asyncio
async def test_a_success_body_that_is_not_a_message_passes_through(server: RecordingServer) -> None:
overloaded: Final = {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}
server.default_response = ResponseSpec(body=overloaded)
response: Final = await call(server)
assert_served_natively(server, response)
assert isinstance(response, dict)
assert {name: value for name, value in response.items() if name != "_hidden_params"} == {
**overloaded,
"stop_sequence": None,
}
@pytest.mark.asyncio
async def test_provider_specific_headers_scoped_to_anthropic_reach_the_wire(server: RecordingServer) -> None:
await call(
server,
provider_specific_header=[
{"custom_llm_provider": "anthropic, azure_ai", "extra_headers": {"x-scoped": "yes"}},
{"custom_llm_provider": "openai", "extra_headers": {"x-other": "no"}},
],
)
_, headers = sent(server)
assert headers["x-scoped"] == "yes"
assert "x-other" not in headers
@pytest.mark.asyncio
async def test_scoped_headers_override_extra_headers_which_override_forwarded_headers(server: RecordingServer) -> None:
await call(
server,
headers={"x-priority": "forwarded", "x-forwarded-only": "kept"},
extra_headers={"x-priority": "extra", "x-extra-only": "kept"},
provider_specific_header={"custom_llm_provider": "anthropic", "extra_headers": {"x-priority": "scoped"}},
)
_, headers = sent(server)
assert {name: headers.get(name) for name in ("x-priority", "x-forwarded-only", "x-extra-only")} == {
"x-priority": "scoped",
"x-forwarded-only": "kept",
"x-extra-only": "kept",
}
@pytest.mark.asyncio
async def test_model_capabilities_are_read_from_model_cost_at_call_time(
server: RecordingServer, monkeypatch: pytest.MonkeyPatch
) -> None:
server.expected_requests = 2
model: Final = f"anthropic/{CAPABILITY_MODEL}"
capabilities(monkeypatch, sampling=True)
await call(server, model=model, temperature=0.2, drop_params=True)
capabilities(monkeypatch, sampling=False)
await call(server, model=model, temperature=0.2, drop_params=True)
assert_served_natively(server, count=2)
first, second = (request.body for request in server.requests)
assert isinstance(first, dict)
assert first["temperature"] == 0.2
assert second == {name: value for name, value in first.items() if name != "temperature"}
@pytest.mark.asyncio
async def test_global_drop_params_drops_unsupported_params(
server: RecordingServer, monkeypatch: pytest.MonkeyPatch
) -> None:
capabilities(monkeypatch, sampling=False)
monkeypatch.setattr(litellm, "drop_params", True)
await call(server, model=f"anthropic/{CAPABILITY_MODEL}", top_k=5)
body, _ = sent(server)
assert "top_k" not in body
@pytest.mark.asyncio
async def test_unsupported_params_are_rejected_before_io_without_drop_params(
server: RecordingServer, monkeypatch: pytest.MonkeyPatch
) -> None:
capabilities(monkeypatch, sampling=False)
monkeypatch.setattr(litellm, "drop_params", False)
server.expected_requests = 0
with pytest.raises(litellm.BadRequestError, match="top_k"):
await call(server, model=f"anthropic/{CAPABILITY_MODEL}", top_k=5)
assert server.requests == []
@pytest.mark.asyncio
async def test_optional_positional_body_parameters_reach_the_wire(server: RecordingServer) -> None:
metadata: Final = {"user_id": "caller"}
args: Final = (16, list(MESSAGES), "anthropic/claude-test", metadata, None, False, "Be brief", 0.25)
response: Final = await litellm.anthropic_messages(*args, api_key="test-key", api_base=server.base_url)
assert isinstance(response, dict)
assert response["id"] == MESSAGES_RESPONSE["id"]
body, _ = sent(server)
assert (body["metadata"], body["system"], body["temperature"]) == (args[3], args[6], args[7])
@pytest.mark.asyncio
async def test_the_secret_behind_an_admin_oidc_env_api_key_reaches_the_provider(
server: RecordingServer, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("ANTHROPIC_SA_TOKEN", "env-token")
reference: Final = admin_api_key_secret_reference(
{"api_key": "oidc/env/ANTHROPIC_SA_TOKEN", "api_base": server.base_url}
)["api_key"]
await call(server, api_key=reference)
_, headers = sent(server)
assert headers["x-api-key"] == "env-token"