litellm/tests/unit/fine_tuning/test_main.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

116 lines
4.3 KiB
Python

import json
from typing import Final, Literal
import httpx
import pytest
import respx
import litellm
from litellm.types.utils import LiteLLMFineTuningJob, admin_api_key_secret_reference
API_BASE: Final = "https://fine-tuning.test"
BASE_MODEL: Final = "gpt-4.1-nano-2025-04-14"
PROVIDER_JOB: Final = {
"id": "ftjob-abc123",
"object": "fine_tuning.job",
"created_at": 1700000000,
"error": None,
"fine_tuned_model": None,
"finished_at": None,
"hyperparameters": {"n_epochs": 3},
"model": BASE_MODEL,
"organization_id": "org-test",
"result_files": [],
"seed": 42,
"status": "validating_files",
"trained_tokens": None,
"training_file": "file-abc123",
"validation_file": None,
}
@pytest.mark.asyncio
@respx.mock
@pytest.mark.parametrize(
("custom_llm_provider", "route"),
[("openai", f"{API_BASE}/v1/fine_tuning/jobs"), ("azure", f"{API_BASE}/openai/fine_tuning/jobs")],
)
@pytest.mark.parametrize("prefixed", [True, False], ids=["provider-prefixed", "bare"])
async def test_create_sends_the_model_without_the_provider_prefix(
monkeypatch: pytest.MonkeyPatch, custom_llm_provider: str, route: str, prefixed: bool
) -> None:
sent: Final = respx.post(url__startswith=route).mock(return_value=httpx.Response(200, json=PROVIDER_JOB))
async with httpx.AsyncClient() as session:
monkeypatch.setattr(litellm, "aclient_session", session)
await litellm.acreate_fine_tuning_job(
model=f"{custom_llm_provider}/{BASE_MODEL}" if prefixed else BASE_MODEL,
training_file="file-abc123",
custom_llm_provider=custom_llm_provider,
api_key="sk-test",
api_base=f"{API_BASE}/v1" if custom_llm_provider == "openai" else API_BASE,
**({"api_version": "2024-10-21"} if custom_llm_provider == "azure" else {}),
)
assert json.loads(sent.calls.last.request.content)["model"] == BASE_MODEL
@pytest.mark.asyncio
@respx.mock
@pytest.mark.parametrize(
("custom_llm_provider", "api_base", "route", "sent_api_version"),
[
("openai", f"{API_BASE}/v1", f"{API_BASE}/v1/fine_tuning/jobs", None),
("azure", API_BASE, f"{API_BASE}/openai/fine_tuning/jobs", "2024-10-21"),
],
)
@pytest.mark.parametrize("is_async", [True, False], ids=["async", "sync"])
async def test_create_with_api_version_reaches_the_provider_and_only_azure_gets_it(
monkeypatch: pytest.MonkeyPatch,
custom_llm_provider: Literal["openai", "azure"],
api_base: str,
route: str,
sent_api_version: str | None,
is_async: bool,
) -> None:
sent: Final = respx.post(url__startswith=route).mock(return_value=httpx.Response(200, json=PROVIDER_JOB))
request: Final = {
"model": BASE_MODEL,
"training_file": "file-abc123",
"custom_llm_provider": custom_llm_provider,
"api_key": "sk-test",
"api_base": api_base,
"api_version": "2024-10-21",
}
async with httpx.AsyncClient() as session:
monkeypatch.setattr(litellm, "aclient_session", session)
job: Final = (
await litellm.acreate_fine_tuning_job(**request) if is_async else litellm.create_fine_tuning_job(**request)
)
assert isinstance(job, LiteLLMFineTuningJob)
assert job.id == PROVIDER_JOB["id"]
assert sent.calls.last.request.url.params.get("api-version") == sent_api_version
@pytest.mark.asyncio
@respx.mock
@pytest.mark.parametrize("is_async", [True, False], ids=["async", "sync"])
async def test_list_jobs_sends_the_token_behind_an_admin_oidc_env_api_key(
monkeypatch: pytest.MonkeyPatch, is_async: bool
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
monkeypatch.setenv("OPENAI_SA_TOKEN", "env-token")
listed: Final = respx.get(f"{API_BASE}/v1/fine_tuning/jobs").mock(
return_value=httpx.Response(200, json={"object": "list", "data": [PROVIDER_JOB], "has_more": False})
)
request: Final = {
"custom_llm_provider": "openai",
"api_base": f"{API_BASE}/v1",
**admin_api_key_secret_reference({"api_key": "oidc/env/OPENAI_SA_TOKEN", "api_base": f"{API_BASE}/v1"}),
}
await litellm.alist_fine_tuning_jobs(**request) if is_async else litellm.list_fine_tuning_jobs(**request)
assert listed.calls.last.request.headers["Authorization"] == "Bearer env-token"