mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* fix(sap): reuse the AI Core OAuth token across requests * test(sap): add mock AI Core fixture * test(sap): check chat and embeddings reuse the OAuth token * test(sap): check the token refreshes inside the expiry buffer * test(sap): check a reused token refreshes once time enters the buffer * fix(sap): drop a cached token SAP rejects and refresh short tokens halfway A 401 from the deployment lookup or the chat or embedding request now clears the shared OAuth token, so the next call fetches a new one instead of reusing the rejected token until it nears expiry. Tokens that live two hours or less refresh halfway through their lifetime instead of on every call. * fix(sap): retry a rejected token once and share failed token fetches The deployment lookup that starts every SAP chat and embedding call now retries once with a new token after a 401, and the request sends the token that lookup ended with, so a token SAP rejects no longer fails the call. A late 401 only drops the token that request sent, never a newer one. Requests waiting on a failing token fetch get its error instead of each fetching again behind the lock * fix(sap): reuse a still-valid token when a refresh fails and drop rejected tokens without the lock * fix(sap): back off a failed token refresh and never reuse a token that stopped working during it * fix(sap): never let a failed refresh undo a 401 that drops the cached token --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
import json
|
|
import uuid
|
|
from dataclasses import dataclass
|
|
from typing import Final
|
|
|
|
import httpx
|
|
import pytest
|
|
import respx
|
|
|
|
import litellm
|
|
|
|
|
|
@dataclass(frozen=True, slots=True)
|
|
class MockAICore:
|
|
deployment_url: str
|
|
token: respx.Route
|
|
deployments: respx.Route
|
|
|
|
def reject_token(self, route: respx.Route, authorization: str) -> None:
|
|
accepted: Final = route.return_value
|
|
route.side_effect = lambda request: (
|
|
httpx.Response(401, text="Unauthorized") if request.headers["Authorization"] == authorization else accepted
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def aicore(respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch) -> MockAICore:
|
|
for var in (
|
|
"AICORE_CLIENT_ID",
|
|
"AICORE_CLIENT_SECRET",
|
|
"AICORE_AUTH_URL",
|
|
"AICORE_RESOURCE_GROUP",
|
|
"AICORE_BASE_URL",
|
|
"AICORE_CERT_URL",
|
|
"AICORE_SERVICE_KEY",
|
|
"VCAP_SERVICES",
|
|
):
|
|
monkeypatch.delenv(var, raising=False)
|
|
monkeypatch.setenv("AICORE_HOME", "notexist")
|
|
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
|
|
|
|
host: Final = f"https://{uuid.uuid4().hex}.mock-sap.com"
|
|
service_key: Final = {
|
|
"clientid": "mock-client",
|
|
"clientsecret": "mock-secret",
|
|
"url": f"{host}/auth",
|
|
"serviceurls": {"AI_API_URL": f"{host}/api"},
|
|
}
|
|
monkeypatch.setattr(litellm, "sap_service_key", json.dumps(service_key))
|
|
|
|
base_url: Final = f"{host}/api/v2"
|
|
deployment_url: Final = f"{base_url}/inference/deployments/orchestration"
|
|
deployments: Final = respx_mock.get(f"{base_url}/lm/deployments").respond(
|
|
json={
|
|
"resources": [
|
|
{
|
|
"configurationId": "orchestration-config",
|
|
"scenarioId": "orchestration",
|
|
"deploymentUrl": deployment_url,
|
|
"createdAt": "2026-01-01T00:00:00Z",
|
|
}
|
|
]
|
|
}
|
|
)
|
|
respx_mock.get(f"{base_url}/lm/configurations/orchestration-config").respond(json={"executableId": "orchestration"})
|
|
return MockAICore(
|
|
deployment_url=deployment_url,
|
|
token=respx_mock.post(f"{host}/auth/oauth/token"),
|
|
deployments=deployments,
|
|
)
|