litellm/tests/test_litellm_rust/ocr/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

152 lines
5.6 KiB
Python

from io import BytesIO
from typing import Final
import pytest
import litellm
from litellm.types.utils import admin_api_key_secret_reference
from tests.test_litellm_rust.ocr.data import (
OCR_DOCUMENT,
OCR_RESPONSE,
call,
call_aocr,
call_ocr,
)
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
@pytest.mark.asyncio
@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
@pytest.mark.parametrize("model", ["mistral/mistral-ocr-latest", "azure_ai/mistral-ocr-latest", "reducto/parse-v3"])
async def test_ocr_contract_native_format_supported(
server: RecordingServer,
asynchronous: bool,
model: str,
) -> None:
server.expected_requests = None
payload: Final = (
{"result": {"chunks": [{"content": "native OCR response"}]}, "usage": {"num_pages": 1}}
if model.startswith("reducto/")
else OCR_RESPONSE
)
server.default_response = ResponseSpec(body=payload)
arguments: Final = {
"model": model,
"req_format": "native",
"num_retries": 0,
"document": {"type": "document_url", "document_url": "reducto://ready.pdf"}
if model.startswith("reducto/")
else OCR_DOCUMENT,
}
response: Final = await call_aocr(server, **arguments) if asynchronous else call_ocr(server, **arguments)
assert response.pages[0].markdown == "native OCR response"
assert response.get_provider_native_response() == payload
assert len(server.requests) == 1
assert_served_natively(server, response)
@pytest.mark.asyncio
@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
async def test_ocr_contract_unknown_reducto_model_reaches_provider(
server: RecordingServer,
asynchronous: bool,
) -> None:
server.default_response = ResponseSpec(body={"result": {"chunks": [{"content": "future model response"}]}})
arguments: Final = {
"model": "reducto/future-parse-model",
"document": {"type": "document_url", "document_url": "reducto://ready.pdf"},
"num_retries": 0,
}
response: Final = await call_aocr(server, **arguments) if asynchronous else call_ocr(server, **arguments)
assert response.model == "future-parse-model"
assert response.pages[0].markdown == "future model response"
assert len(server.requests) == 1
assert server.requests[0].path == "/parse"
assert server.requests[0].body == {"input": "reducto://ready.pdf"}
@pytest.mark.asyncio
@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
async def test_native_azure_ocr_uses_token_provider_result_as_bearer_token(
server: RecordingServer, isolated_azure_auth: None, asynchronous: bool
) -> None:
calls: Final = []
def token_provider() -> str:
calls.append("token")
return "callback-token"
arguments: Final = {
"model": "azure_ai/mistral-ocr-latest",
"api_key": None,
"azure_ad_token_provider": token_provider,
}
response: Final = await call_aocr(server, **arguments) if asynchronous else call_ocr(server, **arguments)
assert calls == ["token"]
assert response.pages[0].markdown == "native OCR response"
assert_served_natively(server, response)
assert server.requests[0].headers["authorization"] == "Bearer callback-token"
def test_native_ocr_encodes_python_file_input_and_drops_unknown_arguments(server: RecordingServer) -> None:
response: Final = call_ocr(
server,
document={"type": "file", "file": BytesIO(b"abc"), "mime_type": "image/png"},
opaque_extension=object(),
)
assert response.pages[0].markdown == "native OCR response"
assert_served_natively(server, response)
assert server.requests[0].body == {
"model": "mistral-ocr-latest",
"document": {"type": "image_url", "image_url": "data:image/png;base64,YWJj"},
}
@pytest.mark.parametrize(
"drop_params,global_drop_params",
[(True, False), (" TRUE ", False), (None, True), (False, True)],
ids=["per_call", "per_call_spelled", "global", "global_wins"],
)
def test_azure_document_intelligence_drop_params_drops_an_unsupported_output_content_format(
server: RecordingServer,
monkeypatch: pytest.MonkeyPatch,
drop_params: object,
global_drop_params: bool,
) -> None:
monkeypatch.setattr(litellm, "drop_params", global_drop_params)
server.default_response = ResponseSpec(
body={"status": "succeeded", "analyzeResult": {"pages": [{"pageNumber": 1, "lines": [{"content": "hello"}]}]}}
)
response: Final = call_ocr(
server,
model="azure_ai/doc-intelligence/prebuilt-layout",
outputContentFormat="html",
drop_params=drop_params,
)
assert_served_natively(server, response)
assert response.pages[0].markdown == "hello"
assert "outputContentFormat" not in server.requests[0].path
assert "drop_params" not in server.requests[0].path
@pytest.mark.asyncio
@pytest.mark.parametrize("asynchronous", [False, True], ids=["sync", "async"])
async def test_ocr_sends_the_token_behind_an_admin_oidc_env_api_key(
server: RecordingServer, asynchronous: bool, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.setenv("MISTRAL_SA_TOKEN", "env-token")
reference: Final = admin_api_key_secret_reference(
{"api_key": "oidc/env/MISTRAL_SA_TOKEN", "api_base": server.base_url}
)["api_key"]
response: Final = await call(server, asynchronous, api_key=reference)
assert server.requests[0].headers["authorization"] == "Bearer env-token"
assert_served_natively(server, response)