This commit is contained in:
devin-ai-integration[bot] 2026-10-25 20:42:21 -07:00 • committed by GitHub
commit 697103aad5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 619 additions and 41 deletions

View file

@ -76,6 +76,7 @@ OPTIONAL_KWARGS_KEYS: Final = (
"itpm",
"otpm",
"use_xai_oauth",
"chatgpt_auth_file",
"github_copilot_auth_type",
"github_copilot_host",
"github_copilot_user_session",

View file

@ -842,7 +842,9 @@ def _get_openai_compatible_provider_info(
api_base,
dynamic_api_key,
custom_llm_provider,
) = litellm.ChatGPTConfig().get_openai_compatible_provider_info(model, api_base, api_key, custom_llm_provider)
) = litellm.ChatGPTConfig().get_openai_compatible_provider_info(
model, api_base, api_key, custom_llm_provider, litellm_params
)
elif custom_llm_provider == "novita":
api_base = api_base or get_secret("NOVITA_API_BASE") or "https://api.novita.ai/v3/openai"
dynamic_api_key = api_key or get_secret_str("NOVITA_API_KEY")

View file

@ -3,7 +3,8 @@ import json
import os
import time
from collections.abc import Mapping
from typing import Final, TypeAlias
from functools import lru_cache
from typing import TYPE_CHECKING, Final, TypeAlias
import httpx
from pydantic import JsonValue, TypeAdapter, ValidationError
@ -27,6 +28,9 @@ from .common_utils import (
RefreshAccessTokenError,
)
if TYPE_CHECKING:
from litellm.types.router import GenericLiteLLMParams
TOKEN_EXPIRY_SKEW_SECONDS: Final = 60
TOKEN_REFRESH_TIMEOUT_SECONDS: Final = 30
DEVICE_CODE_TIMEOUT_SECONDS: Final = 15 * 60
@ -44,6 +48,26 @@ def _optional_str(value: JsonValue | None) -> str | None:
return value if isinstance(value, str) else None
def get_chatgpt_auth_file(
litellm_params: "Mapping[str, object] | GenericLiteLLMParams | None",
) -> str | None:
if litellm_params is None:
return None
value: Final = (
litellm_params.get("chatgpt_auth_file")
if isinstance(litellm_params, Mapping)
else litellm_params.chatgpt_auth_file
)
if not isinstance(value, str) or not value:
return None
return os.path.abspath(os.path.expanduser(value))
@lru_cache(maxsize=128)
def get_cached_authenticator(auth_file: str) -> "Authenticator":
return Authenticator(auth_file=auth_file)
def _token_refresh_timeout() -> httpx.Timeout:
configured: Final = get_configured_request_timeout()
seconds: Final = (
@ -53,13 +77,13 @@ def _token_refresh_timeout() -> httpx.Timeout:
class Authenticator:
def __init__(self) -> None:
self.token_dir = os.getenv(
"CHATGPT_TOKEN_DIR",
os.path.expanduser("~/.config/litellm/chatgpt"),
def __init__(self, auth_file: str | None = None) -> None:
default_auth_file: Final = os.path.join(
os.getenv("CHATGPT_TOKEN_DIR", os.path.expanduser("~/.config/litellm/chatgpt")),
os.getenv("CHATGPT_AUTH_FILE", "auth.json"),
)
self.auth_file = os.path.join(self.token_dir, os.getenv("CHATGPT_AUTH_FILE", "auth.json"))
self._ensure_token_dir()
self.auth_file = os.path.abspath(os.path.expanduser(auth_file or default_auth_file))
self.token_dir = os.path.dirname(self.auth_file)
def get_api_base(self) -> str:
return os.getenv("CHATGPT_API_BASE") or os.getenv("OPENAI_CHATGPT_API_BASE") or CHATGPT_API_BASE
@ -129,6 +153,7 @@ class Authenticator:
def _write_auth_file(self, data: JsonObject) -> None:
try:
self._ensure_token_dir()
with open(self.auth_file, "w") as f:
json.dump(data, f)
except OSError as exc:

View file

@ -1,10 +1,12 @@
from collections.abc import Mapping
from typing import Any, Final
from litellm.exceptions import AuthenticationError
from litellm.llms.openai.openai import OpenAIConfig
from litellm.types.llms.openai import AllMessageValues
from litellm.types.router import GenericLiteLLMParams
from ..authenticator import Authenticator
from ..authenticator import Authenticator, get_cached_authenticator, get_chatgpt_auth_file
from ..common_utils import (
GetAccessTokenError,
ensure_chatgpt_session_id,
@ -23,6 +25,23 @@ class ChatGPTConfig(OpenAIConfig):
super().__init__()
self.authenticator = Authenticator()
def _resolve_authenticator(
self, litellm_params: Mapping[str, object] | GenericLiteLLMParams | None
) -> Authenticator:
auth_file: Final = get_chatgpt_auth_file(litellm_params)
return get_cached_authenticator(auth_file) if auth_file else self.authenticator
@staticmethod
def _get_access_token_or_raise(authenticator: Authenticator, model: str, llm_provider: str) -> str:
try:
return authenticator.get_access_token()
except GetAccessTokenError as e:
raise AuthenticationError(
model=model,
llm_provider=llm_provider,
message=str(e),
)
def api_base_without_login(self) -> str:
return self.authenticator.get_api_base()
@ -32,16 +51,11 @@ class ChatGPTConfig(OpenAIConfig):
api_base: str | None,
api_key: str | None,
custom_llm_provider: str,
litellm_params: Mapping[str, object] | GenericLiteLLMParams | None = None,
) -> tuple[str | None, str | None, str]:
dynamic_api_base: Final = self.api_base_without_login()
try:
dynamic_api_key: Final = self.authenticator.get_access_token()
except GetAccessTokenError as e:
raise AuthenticationError(
model=model,
llm_provider=custom_llm_provider,
message=str(e),
)
authenticator: Final = self._resolve_authenticator(litellm_params)
dynamic_api_base: Final = authenticator.get_api_base()
dynamic_api_key: Final = self._get_access_token_or_raise(authenticator, model, custom_llm_provider)
return dynamic_api_base, dynamic_api_key, custom_llm_provider
def get_openai_compatible_provider_info(
@ -50,8 +64,9 @@ class ChatGPTConfig(OpenAIConfig):
api_base: str | None,
api_key: str | None,
custom_llm_provider: str,
litellm_params: Mapping[str, object] | GenericLiteLLMParams | None = None,
) -> tuple[str | None, str | None, str]:
return self._get_openai_compatible_provider_info(model, api_base, api_key, custom_llm_provider)
return self._get_openai_compatible_provider_info(model, api_base, api_key, custom_llm_provider, litellm_params)
def validate_environment(
self,
@ -59,17 +74,23 @@ class ChatGPTConfig(OpenAIConfig):
model: str,
messages: list[AllMessageValues],
optional_params: dict,
litellm_params: dict,
litellm_params: dict[str, object],
api_key: str | None = None,
api_base: str | None = None,
) -> dict:
authenticator: Final = self._resolve_authenticator(litellm_params)
resolved_api_key: Final = (
self._get_access_token_or_raise(authenticator, model, "chatgpt")
if get_chatgpt_auth_file(litellm_params)
else api_key
)
validated_headers: Final = super().validate_environment(
headers, model, messages, optional_params, litellm_params, api_key, api_base
headers, model, messages, optional_params, litellm_params, resolved_api_key, api_base
)
account_id: Final = self.authenticator.get_account_id()
account_id: Final = authenticator.get_account_id()
session_id: Final = ensure_chatgpt_session_id(litellm_params)
default_headers: Final = get_chatgpt_default_headers(api_key or "", account_id, session_id)
default_headers: Final = get_chatgpt_default_headers(resolved_api_key or "", account_id, session_id)
return {**default_headers, **validated_headers}
def post_stream_processing(self, stream: Any) -> ChatGPTToolCallNormalizer:

View file

@ -25,7 +25,7 @@ from litellm.types.llms.openai import (
from litellm.types.router import GenericLiteLLMParams
from litellm.types.utils import LlmProviders
from ..authenticator import Authenticator
from ..authenticator import Authenticator, get_cached_authenticator, get_chatgpt_auth_file
from ..common_utils import (
CHATGPT_API_BASE,
GetAccessTokenError,
@ -49,14 +49,19 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig):
def custom_llm_provider(self) -> LlmProviders:
return LlmProviders.CHATGPT
def _resolve_authenticator(self, litellm_params: GenericLiteLLMParams | None) -> Authenticator:
auth_file: Final = get_chatgpt_auth_file(litellm_params)
return get_cached_authenticator(auth_file) if auth_file else self.authenticator
def validate_environment(
self,
headers: dict,
model: str,
litellm_params: GenericLiteLLMParams | None,
) -> dict:
authenticator: Final = self._resolve_authenticator(litellm_params)
try:
access_token: Final = self.authenticator.get_access_token()
access_token: Final = authenticator.get_access_token()
except GetAccessTokenError as e:
raise AuthenticationError(
model=model,
@ -64,7 +69,7 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig):
message=str(e),
)
account_id: Final = self.authenticator.get_account_id()
account_id: Final = authenticator.get_account_id()
session_id: Final = ensure_chatgpt_session_id(litellm_params)
default_headers: Final = get_chatgpt_default_headers(access_token, account_id, session_id)
return {**default_headers, **headers}
@ -79,7 +84,7 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig):
) -> dict:
request: Final = super().transform_responses_api_request(
model,
input,
[{"role": "user", "content": input}] if isinstance(input, str) else input,
response_api_optional_request_params,
litellm_params,
headers,

View file

@ -5998,6 +5998,7 @@ def completion(
tpm=kwargs.get("tpm"),
rpm=kwargs.get("rpm"),
use_xai_oauth=kwargs.get("use_xai_oauth", False),
chatgpt_auth_file=routing_kwargs.get("chatgpt_auth_file"),
gigachat_scope=kwargs.get("gigachat_scope"),
gigachat_auth_url=kwargs.get("gigachat_auth_url"),
gigachat_access_token=kwargs.get("gigachat_access_token"),

View file

@ -736,7 +736,9 @@ def is_request_body_safe(
def _reject_request_credential_params(litellm_params: Mapping[str, object], *, manages_deployments: bool) -> None:
if manages_deployments:
reject_server_owned_wif_fields(litellm_params)
reject_server_owned_wif_fields(
MappingProxyType({key: value for key, value in litellm_params.items() if key != "chatgpt_auth_file"})
)
else:
reject_server_owned_wif_params(litellm_params)
reject_federated_credential_reference(litellm_params)

View file

@ -2304,7 +2304,15 @@ async def test_model_connection(
)
selected_by_id: Final = deployment_by_id is not None
reject_server_owned_wif_fields(request_litellm_params)
reject_server_owned_wif_fields(
MappingProxyType(
{
key: value
for key, value in _OBJECT_MAPPING.validate_python(request_litellm_params).items()
if key != "chatgpt_auth_file"
}
)
)
# Merge: config params (from proxy config) as base, request params override
litellm_params = {
**_config_base_for_health_check(

View file

@ -705,9 +705,11 @@ def _api_base_kwarg(kwargs: Mapping[str, object]) -> str | None:
return api_base if isinstance(api_base, str) else None
def _dispatched_model_name(model: str, custom_llm_provider: str, api_base: str | None) -> str:
def _dispatched_model_name(
model: str, custom_llm_provider: str, api_base: str | None, litellm_params: GenericLiteLLMParams | None = None
) -> str:
provider_model, _, _, _ = litellm.get_llm_provider(
model=model, custom_llm_provider=custom_llm_provider, api_base=api_base
model=model, custom_llm_provider=custom_llm_provider, api_base=api_base, litellm_params=litellm_params
)
return _strip_responses_routing_prefix(provider_model)
@ -718,6 +720,7 @@ def _will_bridge_to_chat_completions(
use_chat_completions_api: bool,
model_info: object,
api_base: str | None,
litellm_params: GenericLiteLLMParams | None = None,
) -> bool:
"""``_bridges_to_chat_completions`` for callers running before the provider config is resolved.
@ -734,7 +737,7 @@ def _will_bridge_to_chat_completions(
return True
return _bridges_to_chat_completions(
_resolve_responses_api_provider_config(
_dispatched_model_name(normalized_model[0], custom_llm_provider, api_base),
_dispatched_model_name(normalized_model[0], custom_llm_provider, api_base, litellm_params),
custom_llm_provider,
model_info,
api_base,
@ -750,12 +753,15 @@ def provider_stores_the_conversation(model: str, custom_llm_provider: str | None
first MCP round runs as an internal call that writes no spend log at all.
"""
api_base: Final = _api_base_kwarg(kwargs)
litellm_params: Final = GenericLiteLLMParams(**kwargs)
return not _will_bridge_to_chat_completions(
model,
custom_llm_provider or litellm.get_llm_provider(model=model, api_base=api_base)[1],
custom_llm_provider
or litellm.get_llm_provider(model=model, api_base=api_base, litellm_params=litellm_params)[1],
bool(kwargs.get("use_chat_completions_api")),
kwargs.get("model_info"),
api_base,
litellm_params,
)
@ -936,6 +942,7 @@ async def aresponses(
bool(kwargs.get("use_chat_completions_api")),
kwargs.get("model_info"),
_api_base_kwarg(kwargs),
GenericLiteLLMParams(**kwargs),
),
):
_stamp_injection_points_for_the_hook(kwargs, model, custom_llm_provider, input, tools)
@ -1051,11 +1058,14 @@ def _resolve_prompt_swapped_provider(
kwargs: Mapping[str, object],
prompt_id: str | None,
) -> str:
swapped_provider: Final = litellm.get_llm_provider(model=swapped_model)[1]
litellm_params: Final = GenericLiteLLMParams(**kwargs)
swapped_provider: Final = litellm.get_llm_provider(model=swapped_model, litellm_params=litellm_params)[1]
if kwargs.get("api_key") is None and kwargs.get("api_base") is None:
return swapped_provider
try:
original_provider: Final = custom_llm_provider or litellm.get_llm_provider(model=original_model)[1]
original_provider: Final = (
custom_llm_provider or litellm.get_llm_provider(model=original_model, litellm_params=litellm_params)[1]
)
except litellm.BadRequestError:
return swapped_provider
if swapped_provider == original_provider:
@ -1108,6 +1118,7 @@ def _apply_prompt_management_to_responses_call(
use_chat_completions_api,
kwargs.get("model_info"),
_api_base_kwarg(kwargs),
GenericLiteLLMParams(**kwargs),
),
):
_stamp_injection_points_for_the_hook(kwargs, model, custom_llm_provider, input, tools)

View file

@ -91,6 +91,7 @@ class ProviderConnection:
litellm_credential_name: str | None = None
configurable_clientside_auth_params: "Sequence[str | ConfigurableClientsideParamsCustomAuth] | None" = None
use_xai_oauth: bool | None = None
chatgpt_auth_file: str | None = None
github_copilot_auth_type: str | None = None
github_copilot_host: str | None = None
github_copilot_user_session: object | None = None

View file

@ -572,6 +572,10 @@ class GenericLiteLLMParams(CredentialLiteLLMParams, CustomPricingLiteLLMParams):
default=False,
description="Use stored xAI OAuth credentials when no xAI API key is configured.",
)
chatgpt_auth_file: str | None = Field(
default=None,
description="Path to a ChatGPT OAuth auth.json for this deployment.",
)
fireworks_forward_user_id: bool | None = Field(
default=None,
description="Send the LiteLLM user id of the calling key as the `user` field on Fireworks AI chat, responses and messages requests.",

View file

@ -4360,6 +4360,7 @@ server_owned_wif_litellm_params: Final = (
+ openai_wif_litellm_params
+ oauth_token_exchange_litellm_params
+ github_copilot_oauth_litellm_params
+ ("chatgpt_auth_file",)
)
secret_bearing_wif_litellm_params: Final = tuple(sorted(WIF_SECRET_BEARING_KEYS))

View file

@ -0,0 +1,217 @@
import asyncio
import json
from pathlib import Path
from types import MappingProxyType
from typing import Final
import httpx
import pytest
import respx
import litellm
from litellm.integrations.dotprompt.dotprompt_manager import DotpromptManager
from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider
from litellm.llms.chatgpt.chat.transformation import ChatGPTConfig
from litellm.types.router import GenericLiteLLMParams
def _write_auth_record(path: Path, token: str, account_id: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps(
{
"access_token": token,
"account_id": account_id,
"expires_at": 4102444800,
}
)
)
@pytest.fixture
def auth_files(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Path]:
monkeypatch.setattr("time.time", lambda: 1700000000)
default_dir: Final = tmp_path / "default"
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(default_dir))
monkeypatch.delenv("CHATGPT_AUTH_FILE", raising=False)
_write_auth_record(default_dir / "auth.json", "token-default", "acct-default")
file_a: Final = tmp_path / "account-a" / "auth.json"
file_b: Final = tmp_path / "account-b" / "auth.json"
_write_auth_record(file_a, "token-a", "acct-a")
_write_auth_record(file_b, "token-b", "acct-b")
return file_a, file_b
class TestChatGPTMultiAccountChat:
def test_validate_environment_uses_per_model_auth_file(self, auth_files: tuple[Path, Path]) -> None:
file_a, file_b = auth_files
config: Final = ChatGPTConfig()
headers_a: Final = config.validate_environment(
headers={},
model="gpt-6.1-sol",
messages=[],
optional_params={},
litellm_params={"chatgpt_auth_file": str(file_a)},
api_key="token-default",
)
headers_b: Final = config.validate_environment(
headers={},
model="gpt-6.1-sol",
messages=[],
optional_params={},
litellm_params={"chatgpt_auth_file": str(file_b)},
api_key="token-default",
)
assert headers_a["Authorization"] == "Bearer token-a"
assert headers_a["ChatGPT-Account-Id"] == "acct-a"
assert headers_b["Authorization"] == "Bearer token-b"
assert headers_b["ChatGPT-Account-Id"] == "acct-b"
def test_validate_environment_defaults_to_global_auth_file(self, auth_files: tuple[Path, Path]) -> None:
config: Final = ChatGPTConfig()
headers: Final = config.validate_environment(
headers={},
model="gpt-6.1-sol",
messages=[],
optional_params={},
litellm_params={},
api_key="token-default",
)
assert headers["Authorization"] == "Bearer token-default"
assert headers["ChatGPT-Account-Id"] == "acct-default"
def test_provider_info_uses_per_model_auth_file(self, auth_files: tuple[Path, Path]) -> None:
file_a, _ = auth_files
config: Final = ChatGPTConfig()
_, per_model_key, _ = config._get_openai_compatible_provider_info(
"gpt-6.1-sol", None, None, "chatgpt", {"chatgpt_auth_file": str(file_a)}
)
_, default_key, _ = config._get_openai_compatible_provider_info("gpt-6.1-sol", None, None, "chatgpt")
assert per_model_key == "token-a"
assert default_key == "token-default"
def test_get_llm_provider_threads_auth_file(self, auth_files: tuple[Path, Path]) -> None:
file_a, _ = auth_files
model, provider, dynamic_api_key, _ = get_llm_provider(
model="chatgpt/gpt-6.1-sol",
litellm_params=GenericLiteLLMParams(chatgpt_auth_file=str(file_a)),
)
assert model == "gpt-6.1-sol"
assert provider == "chatgpt"
assert dynamic_api_key == "token-a"
def _subscription_response(request: httpx.Request) -> httpx.Response:
account: Final = request.headers["ChatGPT-Account-Id"]
assert request.headers["Authorization"] == f"Bearer token-{account.removeprefix('acct-')}"
assert "chatgpt_auth_file" not in request.content.decode()
message: Final = {
"id": "msg_test",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": account, "annotations": []}],
}
response: Final = {
"id": "resp_test",
"object": "response",
"created_at": 1700000000,
"status": "completed",
"model": "gpt-6.1-sol",
"output": [message],
"usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2},
}
events: Final = (
{"type": "response.created", "response": {**response, "status": "in_progress", "output": []}},
{
"type": "response.output_text.delta",
"item_id": "msg_test",
"output_index": 0,
"content_index": 0,
"delta": account,
},
{"type": "response.output_item.done", "output_index": 0, "item": message},
{"type": "response.completed", "response": response},
)
return httpx.Response(
200,
text="".join(f"data: {json.dumps(event)}\n\n" for event in events),
headers={"content-type": "text/event-stream"},
)
@pytest.mark.parametrize("blocked_default", [False, True])
@pytest.mark.parametrize("with_prompt", [False, True])
@pytest.mark.parametrize("endpoint", ["chat", "responses"])
@pytest.mark.parametrize("through_router", [False, True])
@pytest.mark.asyncio
async def test_requests_use_selected_subscription(
auth_files: tuple[Path, Path],
endpoint: str,
through_router: bool,
with_prompt: bool,
blocked_default: bool,
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
default_dir: Final = auth_files[0].parent / "missing-default"
if blocked_default:
default_dir.write_text("ordinary file")
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(default_dir / "unused"))
if with_prompt:
monkeypatch.setattr(
litellm,
"callbacks",
[
DotpromptManager(
prompt_id="account-check",
prompt_data={"content": "Saved account prompt", "metadata": {"model": "chatgpt/gpt-6.1-sol"}},
)
],
)
router: Final = (
litellm.Router(
model_list=[
{
"model_name": f"account-{index}",
"litellm_params": {"model": "chatgpt/gpt-6.1-sol", "chatgpt_auth_file": str(path)},
}
for index, path in enumerate(auth_files)
]
)
if through_router
else None
)
async def request(index: int, path: Path) -> str:
params: Final = MappingProxyType(
{
"model": f"account-{index}" if router else "chatgpt/gpt-6.1-sol",
**({"chatgpt_auth_file": str(path)} if router is None else {}),
**({"prompt_id": "account-check"} if with_prompt else {}),
}
)
if endpoint == "chat":
result: Final = await (router.acompletion if router else litellm.acompletion)(
**params, messages=[{"role": "user", "content": "Hello"}]
)
return result.choices[0].message.content
response: Final = await (router.aresponses if router else litellm.aresponses)(
**params, input=[{"role": "user", "content": "Hello"}]
)
return response.output[0].content[0].text
with respx.mock as transport:
transport.post("https://chatgpt.com/backend-api/codex/responses").mock(side_effect=_subscription_response)
results: Final = await asyncio.gather(*(request(index, path) for index, path in enumerate(auth_files)))
assert results == ["acct-a", "acct-b"]
if with_prompt:
assert all("Saved account prompt" in call.request.content.decode() for call in transport.calls)

View file

@ -6,6 +6,7 @@ Source: litellm/llms/chatgpt/responses/transformation.py
import json
from collections.abc import Generator
from pathlib import Path
from typing import Final
from unittest.mock import MagicMock, patch
@ -395,3 +396,59 @@ class TestChatGPTResponsesAPITransformation:
assert exc_info.value.message == "ChatGPT upstream failed"
assert exc_info.value.status_code == 502
class TestChatGPTResponsesMultiAccount:
@pytest.fixture(autouse=True)
def fixed_clock(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("time.time", lambda: 1700000000)
@staticmethod
def _write_auth_record(path: Path, token: str, account_id: str) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(
json.dumps(
{
"access_token": token,
"account_id": account_id,
"expires_at": 4102444800,
}
)
)
def test_validate_environment_uses_per_model_auth_file(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
default_dir: Final = tmp_path / "default"
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(default_dir))
monkeypatch.delenv("CHATGPT_AUTH_FILE", raising=False)
self._write_auth_record(default_dir / "auth.json", "token-default", "acct-default")
file_a: Final = tmp_path / "account-a" / "auth.json"
file_b: Final = tmp_path / "account-b" / "auth.json"
self._write_auth_record(file_a, "token-a", "acct-a")
self._write_auth_record(file_b, "token-b", "acct-b")
config: Final = ChatGPTResponsesAPIConfig()
headers_a: Final = config.validate_environment(
headers={},
model="gpt-6.1-sol",
litellm_params=GenericLiteLLMParams(chatgpt_auth_file=str(file_a)),
)
headers_b: Final = config.validate_environment(
headers={},
model="gpt-6.1-sol",
litellm_params=GenericLiteLLMParams(chatgpt_auth_file=str(file_b)),
)
headers_default: Final = config.validate_environment(
headers={},
model="gpt-6.1-sol",
litellm_params=GenericLiteLLMParams(),
)
assert headers_a["Authorization"] == "Bearer token-a"
assert headers_a["ChatGPT-Account-Id"] == "acct-a"
assert headers_b["Authorization"] == "Bearer token-b"
assert headers_b["ChatGPT-Account-Id"] == "acct-b"
assert headers_default["Authorization"] == "Bearer token-default"
assert headers_default["ChatGPT-Account-Id"] == "acct-default"

View file

@ -2,6 +2,8 @@ import base64
import json
import time
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Final
from unittest.mock import MagicMock, mock_open, patch
import pytest
@ -11,8 +13,11 @@ from litellm.constants import DEFAULT_REQUEST_TIMEOUT_SECONDS, HTTP_HANDLER_CONN
from litellm.llms.chatgpt.authenticator import (
TOKEN_REFRESH_TIMEOUT_SECONDS,
Authenticator,
get_cached_authenticator,
get_chatgpt_auth_file,
)
from litellm.llms.chatgpt.common_utils import GetAccessTokenError
from litellm.types.router import GenericLiteLLMParams
def _make_jwt(payload: dict) -> str:
@ -161,3 +166,98 @@ class TestChatGPTAuthenticator:
assert account_id == "acct-123"
mock_write.assert_called_once()
assert mock_write.call_args[0][0]["account_id"] == "acct-123"
def _write_auth_record(path: Path, token: str, account_id: str) -> None:
path.write_text(
json.dumps(
{
"access_token": token,
"account_id": account_id,
"expires_at": 4102444800,
}
)
)
class TestChatGPTMultiAccountAuthenticator:
@pytest.fixture(autouse=True)
def fixed_clock(self, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr("time.time", lambda: 1700000000)
def test_explicit_auth_file_overrides_env(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "env-dir"))
monkeypatch.setenv("CHATGPT_AUTH_FILE", "env.json")
custom_file: Final = tmp_path / "account-a" / "auth.json"
authenticator: Final = Authenticator(auth_file=str(custom_file))
assert authenticator.auth_file == str(custom_file)
assert authenticator.token_dir == str(custom_file.parent)
assert not custom_file.parent.exists()
authenticator._write_auth_file({"access_token": "token-a"})
assert json.loads(custom_file.read_text())["access_token"] == "token-a"
def test_default_auth_file_from_env(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "env-dir"))
monkeypatch.setenv("CHATGPT_AUTH_FILE", "env.json")
authenticator: Final = Authenticator()
assert authenticator.auth_file == str(tmp_path / "env-dir" / "env.json")
def test_bare_filename_auth_file_resolves_against_cwd(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
authenticator: Final = Authenticator(auth_file="auth.json")
assert authenticator.auth_file == str(tmp_path / "auth.json")
assert authenticator.token_dir == str(tmp_path)
def test_two_auth_files_are_isolated(self, tmp_path: Path) -> None:
file_a: Final = tmp_path / "account-a.json"
file_b: Final = tmp_path / "account-b.json"
_write_auth_record(file_a, "token-a", "acct-a")
_write_auth_record(file_b, "token-b", "acct-b")
authenticator_a: Final = Authenticator(auth_file=str(file_a))
authenticator_b: Final = Authenticator(auth_file=str(file_b))
assert authenticator_a.get_access_token() == "token-a"
assert authenticator_a.get_account_id() == "acct-a"
assert authenticator_b.get_access_token() == "token-b"
assert authenticator_b.get_account_id() == "acct-b"
def test_get_cached_authenticator_reuses_instance_per_path(self, tmp_path: Path) -> None:
file_a: Final = tmp_path / "account-a.json"
file_b: Final = tmp_path / "account-b.json"
authenticator_a: Final = get_cached_authenticator(str(file_a))
assert get_cached_authenticator(str(file_a)) is authenticator_a
assert get_cached_authenticator(str(file_b)) is not authenticator_a
def test_get_chatgpt_auth_file(self) -> None:
assert get_chatgpt_auth_file(None) is None
assert get_chatgpt_auth_file({}) is None
assert get_chatgpt_auth_file({"chatgpt_auth_file": ""}) is None
assert get_chatgpt_auth_file({"chatgpt_auth_file": "/a/auth.json"}) == "/a/auth.json"
assert get_chatgpt_auth_file(GenericLiteLLMParams()) is None
assert get_chatgpt_auth_file(GenericLiteLLMParams(chatgpt_auth_file="/b/auth.json")) == "/b/auth.json"
def test_relative_and_absolute_auth_file_share_cached_authenticator(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
monkeypatch.chdir(tmp_path)
get_cached_authenticator.cache_clear()
auth_file: Final = tmp_path / "x" / "auth.json"
auth_file.parent.mkdir(parents=True)
auth_file.write_text("{}")
relative_key: Final = get_chatgpt_auth_file({"chatgpt_auth_file": "x/auth.json"})
absolute_key: Final = get_chatgpt_auth_file({"chatgpt_auth_file": str(auth_file)})
assert relative_key is not None and absolute_key is not None
assert get_cached_authenticator(relative_key) is get_cached_authenticator(absolute_key)

View file

@ -3626,6 +3626,18 @@ class TestIsRequestBodySafeBlocksEndpointTargetingFields:
)
@pytest.mark.parametrize("allow_client_side_credentials", [False, True])
@pytest.mark.parametrize("nested", [False, True])
def test_request_cannot_select_chatgpt_auth_file(allow_client_side_credentials: bool, nested: bool) -> None:
selector: Final = {"chatgpt_auth_file": "/tokens/account-b/auth.json"}
with pytest.raises(ValueError, match="chatgpt_auth_file"):
is_request_body_safe(
request_body={"model": "chatgpt-account-a", **({"extra_body": selector} if nested else selector)},
general_settings={"allow_client_side_credentials": allow_client_side_credentials},
llm_router=None,
model="chatgpt-account-a",
)
class TestIsRequestBodySafeBlocksBedrockProjectOverride:
"""``aws_bedrock_project_id`` pins a deployment to a Bedrock project so
that project's data-retention policy applies to its requests. A
@ -5304,3 +5316,27 @@ async def test_a_non_numeric_max_request_size_mb_is_ignored_without_premium():
patch("litellm.proxy.proxy_server.premium_user", False),
):
assert await check_if_request_size_is_safe(request=_request_with_content_length(1024)) is True
@pytest.mark.parametrize("allow_client_side_credentials", [False, True])
@pytest.mark.parametrize("route", ["/model/new", "/model/update", "/model/account-a/update", "/health/test_connection"])
def test_chatgpt_model_configuration_reaches_role_authorization(
allow_client_side_credentials: bool, route: str
) -> None:
body: Final = {"litellm_params": {"model": "chatgpt/gpt-6-luna", "chatgpt_auth_file": "/tokens/account-a/auth.json"}}
assert is_request_body_safe(
request_body=body,
general_settings={"allow_client_side_credentials": allow_client_side_credentials},
llm_router=None,
model="",
route=route,
)
for inference_route in ("/v1/chat/completions", "/v1/responses"):
with pytest.raises(ValueError, match="chatgpt_auth_file"):
is_request_body_safe(
request_body=body,
general_settings={"allow_client_side_credentials": allow_client_side_credentials},
llm_router=None,
model="",
route=inference_route,
)

View file

@ -5437,3 +5437,63 @@ async def test_health_services_endpoint_langfuse_missing_keys_errors(monkeypatch
with pytest.raises(ProxyException, match="auth_check failed") as raised:
await health_services_endpoint(service="langfuse")
assert "connection refused by lf.internal.example" in str(raised.value.message)
@pytest.mark.asyncio
@pytest.mark.parametrize("admin", [False, True])
async def test_unsaved_chatgpt_account_probe_requires_proxy_admin(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, admin: bool
) -> None:
from starlette.requests import Request
from litellm.proxy import proxy_server
auth_file: Final = tmp_path / "auth.json"
auth_file.write_text(
json.dumps({"access_token": "token-probe", "account_id": "acct-probe", "expires_at": 4102444800})
)
monkeypatch.setattr(litellm, "disable_aiohttp_transport", True)
monkeypatch.setattr(proxy_server, "prisma_client", MagicMock())
monkeypatch.setattr(proxy_server, "llm_router", None)
monkeypatch.setattr(proxy_server, "general_settings", {})
output: Final = {
"id": "msg_probe",
"type": "message",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "ok", "annotations": []}],
}
response: Final = {
"id": "resp_probe",
"object": "response",
"created_at": 1700000000,
"status": "completed",
"model": "gpt-6.1-sol",
"output": [output],
"usage": {"input_tokens": 1, "output_tokens": 1, "total_tokens": 2},
}
with respx.mock(assert_all_called=False) as upstream:
upstream.post("https://chatgpt.com/backend-api/codex/responses").respond(
200,
text="data: " + json.dumps({"type": "response.completed", "response": response}) + "\n\n",
headers={"content-type": "text/event-stream"},
)
call: Final = health_test_model_connection(
request=Request({"type": "http", "headers": []}),
mode="responses",
litellm_params={"model": "chatgpt/gpt-6.1-sol", "chatgpt_auth_file": str(auth_file)},
model_info={},
user_api_key_dict=UserAPIKeyAuth(
user_role=LitellmUserRoles.PROXY_ADMIN if admin else LitellmUserRoles.INTERNAL_USER, token="test-key"
),
)
if admin:
result: Final = await call
assert result["status"] == "success"
assert upstream.calls.last.request.headers["Authorization"] == "Bearer token-probe"
assert upstream.calls.last.request.headers["ChatGPT-Account-Id"] == "acct-probe"
assert isinstance(json.loads(upstream.calls.last.request.content)["input"], list)
else:
with pytest.raises(ProxyException) as error:
await call
assert error.value.code == "403"
assert len(upstream.calls) == 0

View file

@ -8933,7 +8933,14 @@ class TestNonAdminCannotPersistWifFieldsOnModel:
mock_prisma.db.litellm_proxymodeltable.update.assert_not_called()
@pytest.mark.asyncio
async def test_patch_model_non_admin_cannot_set_wif_field(self):
@pytest.mark.parametrize(
"field,value",
[
("anthropic_keycloak_token_url", "https://identity.example.com/token"),
("chatgpt_auth_file", "/tokens/account-b/auth.json"),
],
)
async def test_patch_model_non_admin_cannot_set_wif_field(self, field: str, value: str) -> None:
from litellm.proxy.management_endpoints.model_management_endpoints import (
patch_model,
)
@ -8979,13 +8986,13 @@ class TestNonAdminCannotPersistWifFieldsOnModel:
model_id="m1",
patch_data=updateDeployment(
litellm_params=updateLiteLLMParams(
anthropic_keycloak_token_url="https://attacker.example/token",
**{field: value},
)
),
user_api_key_dict=non_admin,
)
err = exc_info.value
assert getattr(err, "param", "") == "anthropic_keycloak_token_url"
assert getattr(err, "param", "") == field
mock_prisma.db.litellm_proxymodeltable.update.assert_not_called()
@pytest.mark.asyncio
@ -9044,7 +9051,14 @@ class TestNonAdminCannotPersistWifFieldsOnModel:
mock_prisma.db.litellm_proxymodeltable.update.assert_not_called()
@pytest.mark.asyncio
async def test_patch_model_admin_can_set_wif_field(self):
@pytest.mark.parametrize(
"field,value",
[
("anthropic_keycloak_token_url", "https://identity.example.com/token"),
("chatgpt_auth_file", "/tokens/account-b/auth.json"),
],
)
async def test_patch_model_admin_can_set_wif_field(self, field: str, value: str) -> None:
from litellm.proxy.management_endpoints.model_management_endpoints import (
patch_model,
)
@ -9095,7 +9109,7 @@ class TestNonAdminCannotPersistWifFieldsOnModel:
model_id="m1",
patch_data=updateDeployment(
litellm_params=updateLiteLLMParams(
anthropic_keycloak_token_url="https://keycloak.internal/token",
**{field: value},
)
),
user_api_key_dict=admin,

View file

@ -94,6 +94,7 @@ CONNECTION_NAMES: Final = (
"github_copilot_host",
"configurable_clientside_auth_params",
"use_xai_oauth",
"chatgpt_auth_file",
"fireworks_forward_user_id",
"aws_batch_role_arn",
"aws_bedrock_control_plane_endpoint",

View file

@ -16,8 +16,8 @@ from litellm.types.router import (
holds_secret_pointer,
reject_server_owned_wif_params,
server_owned_wif_fields_named,
server_owned_wif_fields_written,
server_owned_wif_fields_present,
server_owned_wif_fields_written,
)
from litellm.types.utils import (
CustomPricingLiteLLMParams,
@ -367,6 +367,7 @@ def test_server_owned_registry_includes_anthropic_openai_and_oauth_token_exchang
+ openai_wif_litellm_params
+ oauth_token_exchange_litellm_params
+ github_copilot_oauth_litellm_params
+ ("chatgpt_auth_file",)
)
assert set(openai_wif_litellm_params) == {
"openai_identity_provider_id",

View file

@ -35309,6 +35309,11 @@ export interface components {
cache_read_input_token_cost_priority?: number | null;
/** Cache Read Input Token Cost Ultrafast */
cache_read_input_token_cost_ultrafast?: number | null;
/**
* Chatgpt Auth File
* @description Path to a ChatGPT OAuth auth.json for this deployment.
*/
chatgpt_auth_file?: string | null;
/** Citation Cost Per Token */
citation_cost_per_token?: number | null;
/** Client Id */
@ -50660,6 +50665,11 @@ export interface components {
cache_read_input_token_cost_priority?: number | null;
/** Cache Read Input Token Cost Ultrafast */
cache_read_input_token_cost_ultrafast?: number | null;
/**
* Chatgpt Auth File
* @description Path to a ChatGPT OAuth auth.json for this deployment.
*/
chatgpt_auth_file?: string | null;
/** Citation Cost Per Token */
citation_cost_per_token?: number | null;
/** Client Id */