From 6f7026d4be29380927fc7c389d577833ec7d560a Mon Sep 17 00:00:00 2001 From: Urbanonymous Date: Sat, 19 Sep 2026 04:39:19 +0000 Subject: [PATCH 01/10] feat(chatgpt): support per-model chatgpt_auth_file for multiple ChatGPT accounts Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../litellm_core_utils/get_litellm_params.py | 1 + .../get_llm_provider_logic.py | 4 +- litellm/llms/chatgpt/authenticator.py | 35 +++++- litellm/llms/chatgpt/chat/transformation.py | 46 +++++--- .../llms/chatgpt/responses/transformation.py | 11 +- litellm/main.py | 1 + litellm/proxy/auth/auth_utils.py | 1 + litellm/types/router.py | 4 + litellm/types/utils.py | 1 + .../chat/test_chatgpt_chat_transformation.py | 107 ++++++++++++++++++ .../test_chatgpt_responses_transformation.py | 51 +++++++++ .../chatgpt/test_chatgpt_authenticator.py | 71 +++++++++++- .../proxy/auth/test_auth_utils.py | 32 ++++++ ui/litellm-dashboard/src/lib/http/schema.d.ts | 10 ++ 14 files changed, 351 insertions(+), 24 deletions(-) create mode 100644 tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py diff --git a/litellm/litellm_core_utils/get_litellm_params.py b/litellm/litellm_core_utils/get_litellm_params.py index 9b2db9aad18..bee15672333 100644 --- a/litellm/litellm_core_utils/get_litellm_params.py +++ b/litellm/litellm_core_utils/get_litellm_params.py @@ -60,6 +60,7 @@ OPTIONAL_KWARGS_KEYS: Final = ( "itpm", "otpm", "use_xai_oauth", + "chatgpt_auth_file", } ) | AWS_CREDENTIAL_KWARGS_KEYS diff --git a/litellm/litellm_core_utils/get_llm_provider_logic.py b/litellm/litellm_core_utils/get_llm_provider_logic.py index b7067a45117..d6cf1f5523b 100644 --- a/litellm/litellm_core_utils/get_llm_provider_logic.py +++ b/litellm/litellm_core_utils/get_llm_provider_logic.py @@ -773,7 +773,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") diff --git a/litellm/llms/chatgpt/authenticator.py b/litellm/llms/chatgpt/authenticator.py index 563826c2b93..4c89e15a062 100644 --- a/litellm/llms/chatgpt/authenticator.py +++ b/litellm/llms/chatgpt/authenticator.py @@ -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 @@ -24,6 +25,9 @@ from .common_utils import ( RefreshAccessTokenError, ) +if TYPE_CHECKING: + from litellm.types.router import GenericLiteLLMParams + TOKEN_EXPIRY_SKEW_SECONDS: Final = 60 DEVICE_CODE_TIMEOUT_SECONDS: Final = 15 * 60 DEVICE_CODE_COOLDOWN_SECONDS: Final = 5 * 60 @@ -40,13 +44,32 @@ 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 + ) + return value if isinstance(value, str) and value else None + + +@lru_cache(maxsize=128) +def get_cached_authenticator(auth_file: str) -> "Authenticator": + return Authenticator(auth_file=auth_file) + + 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.auth_file = os.path.expanduser(auth_file or default_auth_file) + self.token_dir = os.path.dirname(self.auth_file) self._ensure_token_dir() def get_api_base(self) -> str: diff --git a/litellm/llms/chatgpt/chat/transformation.py b/litellm/llms/chatgpt/chat/transformation.py index e35408b0829..72ea301b5de 100644 --- a/litellm/llms/chatgpt/chat/transformation.py +++ b/litellm/llms/chatgpt/chat/transformation.py @@ -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,22 +25,34 @@ 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 _get_openai_compatible_provider_info( self, model: str, 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.authenticator.get_api_base() - 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 validate_environment( @@ -51,13 +65,19 @@ class ChatGPTConfig(OpenAIConfig): 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) -> Any: diff --git a/litellm/llms/chatgpt/responses/transformation.py b/litellm/llms/chatgpt/responses/transformation.py index b96e06be3d8..89e88ffab09 100644 --- a/litellm/llms/chatgpt/responses/transformation.py +++ b/litellm/llms/chatgpt/responses/transformation.py @@ -19,7 +19,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, @@ -41,14 +41,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, @@ -56,7 +61,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} diff --git a/litellm/main.py b/litellm/main.py index 38184db1d10..8b485b83a8d 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -5568,6 +5568,7 @@ def completion( gigachat_scope=kwargs.get("gigachat_scope"), gigachat_auth_url=kwargs.get("gigachat_auth_url"), gigachat_access_token=kwargs.get("gigachat_access_token"), + chatgpt_auth_file=kwargs.get("chatgpt_auth_file"), **{key: kwargs[key] for key in FORWARDED_KWARGS_KEYS if key in kwargs}, ) cast(LiteLLMLoggingObj, logging).update_environment_variables( diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.py index 3372145e66c..e8e1f2d7e93 100644 --- a/litellm/proxy/auth/auth_utils.py +++ b/litellm/proxy/auth/auth_utils.py @@ -320,6 +320,7 @@ _BANNED_REQUEST_BODY_PARAMS: Final[tuple[str, ...]] = ( "aws_external_id", "aws_session_tags", "vertex_credentials", + "chatgpt_auth_file", # Azure managed-identity / federated-auth token. The Azure provider # transformer reads ``azure_ad_token`` (top-level or via # ``extra_body``) and resolves it through ``get_secret`` before diff --git a/litellm/types/router.py b/litellm/types/router.py index adadb053ab2..88fb983487e 100644 --- a/litellm/types/router.py +++ b/litellm/types/router.py @@ -359,6 +359,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; lets one instance run multiple ChatGPT accounts.", + ) model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True) merge_reasoning_content_in_choices: bool | None = False model_info: dict | None = None diff --git a/litellm/types/utils.py b/litellm/types/utils.py index c63d971b89b..01246876d24 100644 --- a/litellm/types/utils.py +++ b/litellm/types/utils.py @@ -3913,6 +3913,7 @@ all_litellm_params = ( "enable_tag_filtering", "enable_json_schema_validation", "use_xai_oauth", + "chatgpt_auth_file", "auto_router_config_path", "auto_router_config", "auto_router_default_model", diff --git a/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py b/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py new file mode 100644 index 00000000000..4157050fdb0 --- /dev/null +++ b/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py @@ -0,0 +1,107 @@ +""" +Tests for ChatGPT subscription chat transformation + +Source: litellm/llms/chatgpt/chat/transformation.py +""" + +import json +import time + +import pytest + +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, 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": time.time() + 3600, + } + ) + ) + + +@pytest.fixture +def auth_files(tmp_path, monkeypatch): + default_dir = 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 = tmp_path / "account-a" / "auth.json" + file_b = 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): + file_a, file_b = auth_files + config = ChatGPTConfig() + + headers_a = config.validate_environment( + headers={}, + model="gpt-5.4", + messages=[], + optional_params={}, + litellm_params={"chatgpt_auth_file": str(file_a)}, + api_key="token-default", + ) + headers_b = config.validate_environment( + headers={}, + model="gpt-5.4", + 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): + config = ChatGPTConfig() + + headers = config.validate_environment( + headers={}, + model="gpt-5.4", + 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): + file_a, _ = auth_files + config = ChatGPTConfig() + + _, per_model_key, _ = config._get_openai_compatible_provider_info( + "gpt-5.4", None, None, "chatgpt", {"chatgpt_auth_file": str(file_a)} + ) + _, default_key, _ = config._get_openai_compatible_provider_info("gpt-5.4", 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): + file_a, _ = auth_files + + model, provider, dynamic_api_key, _ = get_llm_provider( + model="chatgpt/gpt-5.4", + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=str(file_a)), + ) + + assert model == "gpt-5.4" + assert provider == "chatgpt" + assert dynamic_api_key == "token-a" diff --git a/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py b/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py index 9bf3eec61f9..c921d9c273f 100644 --- a/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py +++ b/tests/test_litellm/llms/chatgpt/responses/test_chatgpt_responses_transformation.py @@ -5,6 +5,7 @@ Source: litellm/llms/chatgpt/responses/transformation.py """ import json +import time from unittest.mock import MagicMock, patch import httpx @@ -354,3 +355,53 @@ class TestChatGPTResponsesAPITransformation: assert "ChatGPT upstream failed" in str(exc_info.value) assert exc_info.value.status_code == 502 + + +class TestChatGPTResponsesMultiAccount: + @staticmethod + def _write_auth_record(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": time.time() + 3600, + } + ) + ) + + def test_validate_environment_uses_per_model_auth_file(self, tmp_path, monkeypatch): + default_dir = 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 = tmp_path / "account-a" / "auth.json" + file_b = 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 = ChatGPTResponsesAPIConfig() + + headers_a = config.validate_environment( + headers={}, + model="gpt-5.4", + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=str(file_a)), + ) + headers_b = config.validate_environment( + headers={}, + model="gpt-5.4", + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=str(file_b)), + ) + headers_default = config.validate_environment( + headers={}, + model="gpt-5.4", + 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" diff --git a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py index a9ced2afcf9..e66209cee7d 100644 --- a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py +++ b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py @@ -5,7 +5,12 @@ from unittest.mock import mock_open, patch import pytest -from litellm.llms.chatgpt.authenticator import Authenticator +from litellm.llms.chatgpt.authenticator import ( + Authenticator, + get_cached_authenticator, + get_chatgpt_auth_file, +) +from litellm.types.router import GenericLiteLLMParams def _make_jwt(payload: dict) -> str: @@ -68,3 +73,67 @@ 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, token: str, account_id: str) -> None: + path.write_text( + json.dumps( + { + "access_token": token, + "account_id": account_id, + "expires_at": time.time() + 3600, + } + ) + ) + + +class TestChatGPTMultiAccountAuthenticator: + def test_explicit_auth_file_overrides_env(self, tmp_path, monkeypatch): + monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "env-dir")) + monkeypatch.setenv("CHATGPT_AUTH_FILE", "env.json") + custom_file = tmp_path / "account-a" / "auth.json" + + authenticator = Authenticator(auth_file=str(custom_file)) + + assert authenticator.auth_file == str(custom_file) + assert authenticator.token_dir == str(custom_file.parent) + assert custom_file.parent.exists() + + def test_default_auth_file_from_env(self, tmp_path, monkeypatch): + monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "env-dir")) + monkeypatch.setenv("CHATGPT_AUTH_FILE", "env.json") + + authenticator = Authenticator() + + assert authenticator.auth_file == str(tmp_path / "env-dir" / "env.json") + + def test_two_auth_files_are_isolated(self, tmp_path): + file_a = tmp_path / "account-a.json" + file_b = 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 = Authenticator(auth_file=str(file_a)) + authenticator_b = 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): + file_a = tmp_path / "account-a.json" + file_b = tmp_path / "account-b.json" + + authenticator_a = 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): + 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" diff --git a/tests/test_litellm/proxy/auth/test_auth_utils.py b/tests/test_litellm/proxy/auth/test_auth_utils.py index 965acd57bf3..4bc56ea8779 100644 --- a/tests/test_litellm/proxy/auth/test_auth_utils.py +++ b/tests/test_litellm/proxy/auth/test_auth_utils.py @@ -2693,6 +2693,38 @@ class TestIsRequestBodySafeBlocksEndpointTargetingFields: ) +class TestIsRequestBodySafeBlocksChatGPTAuthFileOverride: + """``chatgpt_auth_file`` selects which ChatGPT OAuth credential file a + deployment uses. A caller-supplied value would let an ordinary proxy user + route requests through any other configured ChatGPT account's auth file + (and have the proxy write refreshed tokens back to a caller-chosen path), + so it must only come from the admin's deployment config.""" + + def test_chatgpt_auth_file_in_request_body_is_rejected(self): + with pytest.raises(ValueError, match="chatgpt_auth_file"): + is_request_body_safe( + request_body={ + "model": "chatgpt-account-a", + "chatgpt_auth_file": "/tokens/account-b/auth.json", + }, + general_settings={}, + llm_router=None, + model="chatgpt-account-a", + ) + + def test_chatgpt_auth_file_in_extra_body_is_rejected(self): + with pytest.raises(ValueError, match="chatgpt_auth_file"): + is_request_body_safe( + request_body={ + "model": "chatgpt-account-a", + "extra_body": {"chatgpt_auth_file": "/tokens/account-b/auth.json"}, + }, + general_settings={}, + 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 diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.ts index 252f6030dbb..72d69dd8a3a 100644 --- a/ui/litellm-dashboard/src/lib/http/schema.d.ts +++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts @@ -30755,6 +30755,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; lets one instance run multiple ChatGPT accounts. + */ + chatgpt_auth_file?: string | null; /** Citation Cost Per Token */ citation_cost_per_token?: number | null; /** Client Id */ @@ -41406,6 +41411,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; lets one instance run multiple ChatGPT accounts. + */ + chatgpt_auth_file?: string | null; /** Citation Cost Per Token */ citation_cost_per_token?: number | null; /** Client Id */ From 3d61622c2325b54bd6a8c29c90b132dfe783c3c7 Mon Sep 17 00:00:00 2001 From: mateo Date: Sat, 19 Sep 2026 04:55:06 +0000 Subject: [PATCH 02/10] fix(chatgpt): resolve bare chatgpt_auth_file paths and drop redundant test docstring Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/llms/chatgpt/authenticator.py | 2 +- .../llms/chatgpt/chat/test_chatgpt_chat_transformation.py | 6 ------ .../llms/chatgpt/test_chatgpt_authenticator.py | 8 ++++++++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/litellm/llms/chatgpt/authenticator.py b/litellm/llms/chatgpt/authenticator.py index 4c89e15a062..2c9790ed703 100644 --- a/litellm/llms/chatgpt/authenticator.py +++ b/litellm/llms/chatgpt/authenticator.py @@ -68,7 +68,7 @@ class Authenticator: os.getenv("CHATGPT_TOKEN_DIR", os.path.expanduser("~/.config/litellm/chatgpt")), os.getenv("CHATGPT_AUTH_FILE", "auth.json"), ) - self.auth_file = os.path.expanduser(auth_file or default_auth_file) + self.auth_file = os.path.abspath(os.path.expanduser(auth_file or default_auth_file)) self.token_dir = os.path.dirname(self.auth_file) self._ensure_token_dir() diff --git a/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py b/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py index 4157050fdb0..aa0b0390039 100644 --- a/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py +++ b/tests/test_litellm/llms/chatgpt/chat/test_chatgpt_chat_transformation.py @@ -1,9 +1,3 @@ -""" -Tests for ChatGPT subscription chat transformation - -Source: litellm/llms/chatgpt/chat/transformation.py -""" - import json import time diff --git a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py index e66209cee7d..0e8d0869f67 100644 --- a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py +++ b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py @@ -107,6 +107,14 @@ class TestChatGPTMultiAccountAuthenticator: assert authenticator.auth_file == str(tmp_path / "env-dir" / "env.json") + def test_bare_filename_auth_file_resolves_against_cwd(self, tmp_path, monkeypatch): + monkeypatch.chdir(tmp_path) + + authenticator = 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): file_a = tmp_path / "account-a.json" file_b = tmp_path / "account-b.json" From 003352ff079fe74ba7b5da5d9f3cc0d289306710 Mon Sep 17 00:00:00 2001 From: mateo Date: Sat, 19 Sep 2026 06:09:18 +0000 Subject: [PATCH 03/10] fix(chatgpt): pass per-model auth file to early provider detection Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/main.py | 1 + litellm/responses/main.py | 12 ++++-- .../chatgpt/test_chatgpt_authenticator.py | 41 +++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/litellm/main.py b/litellm/main.py index 8b485b83a8d..e626374ad78 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -607,6 +607,7 @@ async def acompletion( model=model, custom_llm_provider=custom_llm_provider, api_base=kwargs.get("api_base") or base_url, + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=kwargs.get("chatgpt_auth_file")), ) fallbacks = fallbacks or litellm.model_fallbacks diff --git a/litellm/responses/main.py b/litellm/responses/main.py index 5a4a08b760c..1b9cffa29cd 100644 --- a/litellm/responses/main.py +++ b/litellm/responses/main.py @@ -621,7 +621,9 @@ async def aresponses( # get custom llm provider so we can use this for mapping exceptions if custom_llm_provider is None: _, custom_llm_provider, _, _ = litellm.get_llm_provider( - model=model, api_base=local_vars.get("base_url", None) + model=model, + api_base=local_vars.get("base_url", None), + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=kwargs.get("chatgpt_auth_file")), ) # Update local_vars with detected provider (fixes #19782) local_vars["custom_llm_provider"] = custom_llm_provider @@ -1197,7 +1199,9 @@ def responses( if custom_llm_provider is None: _, custom_llm_provider, _, _ = litellm.get_llm_provider( - model=model, api_base=local_vars.get("base_url", None) + model=model, + api_base=local_vars.get("base_url", None), + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=kwargs.get("chatgpt_auth_file")), ) local_vars["custom_llm_provider"] = custom_llm_provider @@ -2079,7 +2083,9 @@ async def acompact_responses( # get custom llm provider so we can use this for mapping exceptions if custom_llm_provider is None: _, custom_llm_provider, _, _ = litellm.get_llm_provider( - model=model, api_base=local_vars.get("base_url", None) + model=model, + api_base=local_vars.get("base_url", None), + litellm_params=GenericLiteLLMParams(chatgpt_auth_file=kwargs.get("chatgpt_auth_file")), ) # Update local_vars with detected provider (fixes #19782) local_vars["custom_llm_provider"] = custom_llm_provider diff --git a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py index 0e8d0869f67..213830af4a9 100644 --- a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py +++ b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py @@ -5,6 +5,7 @@ from unittest.mock import mock_open, patch import pytest +import litellm from litellm.llms.chatgpt.authenticator import ( Authenticator, get_cached_authenticator, @@ -145,3 +146,43 @@ class TestChatGPTMultiAccountAuthenticator: 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" + + +class TestChatGPTAuthFileEarlyProviderDetection: + @staticmethod + def _no_device_login(self): + raise AssertionError("device login must not run") + + async def test_completion_threads_chatgpt_auth_file(self, tmp_path, monkeypatch): + monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "default")) + monkeypatch.delenv("CHATGPT_AUTH_FILE", raising=False) + auth_file = tmp_path / "account-a" / "auth.json" + auth_file.parent.mkdir(parents=True) + _write_auth_record(auth_file, "token-a", "acct-a") + monkeypatch.setattr(Authenticator, "_login_device_code", self._no_device_login) + + response = await litellm.acompletion( + model="chatgpt/gpt-5.4", + messages=[{"role": "user", "content": "hi"}], + chatgpt_auth_file=str(auth_file), + mock_response="ok", + ) + + assert response is not None + + def test_responses_threads_chatgpt_auth_file(self, tmp_path, monkeypatch): + monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "default")) + monkeypatch.delenv("CHATGPT_AUTH_FILE", raising=False) + auth_file = tmp_path / "account-a" / "auth.json" + auth_file.parent.mkdir(parents=True) + _write_auth_record(auth_file, "token-a", "acct-a") + monkeypatch.setattr(Authenticator, "_login_device_code", self._no_device_login) + + response = litellm.responses( + model="chatgpt/gpt-5.4", + input="hi", + chatgpt_auth_file=str(auth_file), + mock_response="ok", + ) + + assert response is not None From 1a788a96a54dcc168df0d1e24bfbddd80d44e044 Mon Sep 17 00:00:00 2001 From: mateo Date: Sat, 19 Sep 2026 07:55:03 +0000 Subject: [PATCH 04/10] fix(chatgpt): normalize chatgpt_auth_file before caching the authenticator Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- litellm/llms/chatgpt/authenticator.py | 4 +++- .../llms/chatgpt/test_chatgpt_authenticator.py | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/litellm/llms/chatgpt/authenticator.py b/litellm/llms/chatgpt/authenticator.py index 2c9790ed703..8af45c82bc6 100644 --- a/litellm/llms/chatgpt/authenticator.py +++ b/litellm/llms/chatgpt/authenticator.py @@ -54,7 +54,9 @@ def get_chatgpt_auth_file( if isinstance(litellm_params, Mapping) else litellm_params.chatgpt_auth_file ) - return value if isinstance(value, str) and value else None + if not isinstance(value, str) or not value: + return None + return os.path.abspath(os.path.expanduser(value)) @lru_cache(maxsize=128) diff --git a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py index 213830af4a9..83114318950 100644 --- a/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py +++ b/tests/test_litellm/llms/chatgpt/test_chatgpt_authenticator.py @@ -147,6 +147,19 @@ class TestChatGPTMultiAccountAuthenticator: 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, monkeypatch): + monkeypatch.chdir(tmp_path) + get_cached_authenticator.cache_clear() + auth_file = tmp_path / "x" / "auth.json" + auth_file.parent.mkdir(parents=True) + auth_file.write_text("{}") + + relative_key = get_chatgpt_auth_file({"chatgpt_auth_file": "x/auth.json"}) + absolute_key = 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) + class TestChatGPTAuthFileEarlyProviderDetection: @staticmethod From 128974963da86ac9466832bf98d52ee5c26011c7 Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sat, 24 Oct 2026 21:39:58 -0700 Subject: [PATCH 05/10] fix(chatgpt): type account selection parameters --- litellm/llms/chatgpt/chat/transformation.py | 2 +- litellm/main.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/litellm/llms/chatgpt/chat/transformation.py b/litellm/llms/chatgpt/chat/transformation.py index 580a00e8bac..41e861b8eda 100644 --- a/litellm/llms/chatgpt/chat/transformation.py +++ b/litellm/llms/chatgpt/chat/transformation.py @@ -74,7 +74,7 @@ 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: diff --git a/litellm/main.py b/litellm/main.py index c0626b838cc..156e2355e5a 100644 --- a/litellm/main.py +++ b/litellm/main.py @@ -5998,7 +5998,7 @@ def completion( tpm=kwargs.get("tpm"), rpm=kwargs.get("rpm"), use_xai_oauth=kwargs.get("use_xai_oauth", False), - chatgpt_auth_file=kwargs.get("chatgpt_auth_file"), + 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"), From 9a1cd2bf73df12170d3e95880ca686f99163f20b Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sat, 24 Oct 2026 21:57:57 -0700 Subject: [PATCH 06/10] fix(chatgpt): preserve account selection through saved prompts --- litellm/llms/chatgpt/authenticator.py | 2 +- litellm/responses/main.py | 23 ++++++++++---- .../chat/test_chatgpt_chat_transformation.py | 30 +++++++++++++++++-- .../chatgpt/test_chatgpt_authenticator.py | 4 ++- tests/unit/types/test_router.py | 3 +- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/litellm/llms/chatgpt/authenticator.py b/litellm/llms/chatgpt/authenticator.py index 2e747028f14..69cdf8966ab 100644 --- a/litellm/llms/chatgpt/authenticator.py +++ b/litellm/llms/chatgpt/authenticator.py @@ -84,7 +84,6 @@ class Authenticator: ) self.auth_file = os.path.abspath(os.path.expanduser(auth_file or default_auth_file)) self.token_dir = os.path.dirname(self.auth_file) - self._ensure_token_dir() def get_api_base(self) -> str: return os.getenv("CHATGPT_API_BASE") or os.getenv("OPENAI_CHATGPT_API_BASE") or CHATGPT_API_BASE @@ -154,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: diff --git a/litellm/responses/main.py b/litellm/responses/main.py index 1ff80b0d71d..7bf42b053a7 100644 --- a/litellm/responses/main.py +++ b/litellm/responses/main.py @@ -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) diff --git a/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py b/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py index ec24593516e..675e14923a0 100644 --- a/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py +++ b/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py @@ -8,6 +8,7 @@ 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 @@ -146,14 +147,35 @@ def _subscription_response(request: httpx.Request) -> httpx.Response: ) +@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, monkeypatch: pytest.MonkeyPatch + 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) - monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(auth_files[0].parent / "missing-default")) + 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=[ @@ -174,6 +196,8 @@ async def test_requests_use_selected_subscription( if router else {"model": "chatgpt/gpt-6.1-sol", "chatgpt_auth_file": str(path)} ) + if with_prompt: + params["prompt_id"] = "account-check" if endpoint == "chat": result: Final = await (router.acompletion if router else litellm.acompletion)( **params, messages=[{"role": "user", "content": "Hello"}] @@ -188,3 +212,5 @@ async def test_requests_use_selected_subscription( 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) diff --git a/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py b/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py index fc2bdbc25f3..385fc78a061 100644 --- a/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py +++ b/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py @@ -194,7 +194,9 @@ class TestChatGPTMultiAccountAuthenticator: assert authenticator.auth_file == str(custom_file) assert authenticator.token_dir == str(custom_file.parent) - assert custom_file.parent.exists() + 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")) diff --git a/tests/unit/types/test_router.py b/tests/unit/types/test_router.py index 2892a829eab..7855eba0042 100644 --- a/tests/unit/types/test_router.py +++ b/tests/unit/types/test_router.py @@ -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", From 2bfb56921662f75d7f6cc9f35a1d61f1858dedde Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sat, 24 Oct 2026 22:02:16 -0700 Subject: [PATCH 07/10] fix(chatgpt): allow administrator account configuration through the proxy --- litellm/proxy/auth/auth_utils.py | 4 +++- tests/unit/proxy/auth/test_auth_utils.py | 24 ++++++++++++++++++++++++ tests/unit/types/test_litellm_params.py | 1 + 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.py index 25f900a5f9f..30ede935cfa 100644 --- a/litellm/proxy/auth/auth_utils.py +++ b/litellm/proxy/auth/auth_utils.py @@ -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) diff --git a/tests/unit/proxy/auth/test_auth_utils.py b/tests/unit/proxy/auth/test_auth_utils.py index 0d6e3d0f686..10a0f974a42 100644 --- a/tests/unit/proxy/auth/test_auth_utils.py +++ b/tests/unit/proxy/auth/test_auth_utils.py @@ -5316,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, + ) diff --git a/tests/unit/types/test_litellm_params.py b/tests/unit/types/test_litellm_params.py index b0086662f1c..740342f54d9 100644 --- a/tests/unit/types/test_litellm_params.py +++ b/tests/unit/types/test_litellm_params.py @@ -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", From 3fb2f3bf91c3686e64769ad34e385595e709ea7e Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sat, 24 Oct 2026 22:27:50 -0700 Subject: [PATCH 08/10] test(chatgpt): construct immutable request parameters --- .../chat/test_chatgpt_chat_transformation.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py b/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py index 675e14923a0..fcac7778fe2 100644 --- a/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py +++ b/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py @@ -1,6 +1,7 @@ import asyncio import json from pathlib import Path +from types import MappingProxyType from typing import Final import httpx @@ -191,13 +192,13 @@ async def test_requests_use_selected_subscription( ) async def request(index: int, path: Path) -> str: - params: Final = ( - {"model": f"account-{index}"} - if router - else {"model": "chatgpt/gpt-6.1-sol", "chatgpt_auth_file": str(path)} + 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 with_prompt: - params["prompt_id"] = "account-check" if endpoint == "chat": result: Final = await (router.acompletion if router else litellm.acompletion)( **params, messages=[{"role": "user", "content": "Hello"}] From cf28a4a831227292c62bf816e4a91dde36cb804e Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sun, 25 Oct 2026 20:30:44 -0700 Subject: [PATCH 09/10] fix(chatgpt): allow admins to probe unsaved account configurations --- .../health_endpoints/_health_endpoints.py | 6 +- .../health_endpoints/test_health_endpoints.py | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index 684f1619512..f5a78a67c9a 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -2304,7 +2304,11 @@ 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 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( diff --git a/tests/unit/proxy/health_endpoints/test_health_endpoints.py b/tests/unit/proxy/health_endpoints/test_health_endpoints.py index 950d1742d49..1a49a180877 100644 --- a/tests/unit/proxy/health_endpoints/test_health_endpoints.py +++ b/tests/unit/proxy/health_endpoints/test_health_endpoints.py @@ -5437,3 +5437,62 @@ 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" + else: + with pytest.raises(ProxyException) as error: + await call + assert error.value.code == "403" + assert len(upstream.calls) == 0 From 5cdc4c5a82f13c0420553b4eb147e2c4cca132e9 Mon Sep 17 00:00:00 2001 From: moe-berri Date: Sun, 25 Oct 2026 20:37:28 -0700 Subject: [PATCH 10/10] fix(chatgpt): normalize subscription health probe input --- litellm/llms/chatgpt/responses/transformation.py | 2 +- litellm/proxy/health_endpoints/_health_endpoints.py | 6 +++++- tests/unit/proxy/health_endpoints/test_health_endpoints.py | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/litellm/llms/chatgpt/responses/transformation.py b/litellm/llms/chatgpt/responses/transformation.py index 8a2b76381fb..b5a4f34e8e1 100644 --- a/litellm/llms/chatgpt/responses/transformation.py +++ b/litellm/llms/chatgpt/responses/transformation.py @@ -84,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, diff --git a/litellm/proxy/health_endpoints/_health_endpoints.py b/litellm/proxy/health_endpoints/_health_endpoints.py index f5a78a67c9a..53c54fe15bf 100644 --- a/litellm/proxy/health_endpoints/_health_endpoints.py +++ b/litellm/proxy/health_endpoints/_health_endpoints.py @@ -2306,7 +2306,11 @@ async def test_model_connection( selected_by_id: Final = deployment_by_id is not None reject_server_owned_wif_fields( MappingProxyType( - {key: value for key, value in request_litellm_params.items() if key != "chatgpt_auth_file"} + { + 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 diff --git a/tests/unit/proxy/health_endpoints/test_health_endpoints.py b/tests/unit/proxy/health_endpoints/test_health_endpoints.py index 1a49a180877..264ced8b87c 100644 --- a/tests/unit/proxy/health_endpoints/test_health_endpoints.py +++ b/tests/unit/proxy/health_endpoints/test_health_endpoints.py @@ -5491,6 +5491,7 @@ async def test_unsaved_chatgpt_account_probe_requires_proxy_admin( 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