diff --git a/litellm/llms/chatgpt/authenticator.py b/litellm/llms/chatgpt/authenticator.py index 692f6820ff4..40e605f444a 100644 --- a/litellm/llms/chatgpt/authenticator.py +++ b/litellm/llms/chatgpt/authenticator.py @@ -121,8 +121,10 @@ class Authenticator: tokens: Final = self._login_device_code() return tokens["access_token"] - def get_account_id(self) -> str | None: + def get_account_id(self, access_token: str | None = None) -> str | None: auth_data: Final = self._read_auth_file() + if access_token and (not auth_data or auth_data.get("access_token") != access_token): + return extract_chatgpt_account_id(access_token) if not auth_data: return None account_id: Final = _optional_str(auth_data.get("account_id")) @@ -361,8 +363,9 @@ class Authenticator: "refresh_token": _optional_str(data.get("refresh_token")) or refresh_token, "id_token": id_token, } + account_id: Final = self.get_account_id() auth_data: Final = self._build_auth_record(refreshed) - self._write_auth_file(auth_data) + self._write_auth_file({**auth_data, "account_id": account_id} if account_id else auth_data) return refreshed def _build_auth_record(self, tokens: dict[str, str]) -> JsonObject: diff --git a/litellm/llms/chatgpt/chat/transformation.py b/litellm/llms/chatgpt/chat/transformation.py index 5f6e88160fc..9005185931f 100644 --- a/litellm/llms/chatgpt/chat/transformation.py +++ b/litellm/llms/chatgpt/chat/transformation.py @@ -105,10 +105,16 @@ class ChatGPTConfig(OpenAIConfig): else api_key ) validated_headers: Final = super().validate_environment( - headers, model, messages, optional_params, litellm_params, resolved_api_key, api_base + dict(strip_chatgpt_client_credential_headers(headers)), + model, + messages, + optional_params, + litellm_params, + resolved_api_key, + api_base, ) - account_id: Final = authenticator.get_account_id() + account_id: Final = authenticator.get_account_id(resolved_api_key) session_id: Final = ensure_chatgpt_session_id(litellm_params) default_headers: Final = get_chatgpt_default_headers(resolved_api_key or "", account_id, session_id) return {**default_headers, **validated_headers} diff --git a/litellm/llms/chatgpt/responses/transformation.py b/litellm/llms/chatgpt/responses/transformation.py index 469218bef55..2352de35b5b 100644 --- a/litellm/llms/chatgpt/responses/transformation.py +++ b/litellm/llms/chatgpt/responses/transformation.py @@ -30,7 +30,6 @@ from ..common_utils import ( CHATGPT_API_BASE, GetAccessTokenError, ensure_chatgpt_session_id, - extract_chatgpt_account_id, get_chatgpt_client_credential, get_chatgpt_default_headers, get_chatgpt_default_instructions, @@ -69,15 +68,16 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig): **get_chatgpt_default_headers(client_credential.access_token, client_credential.account_id, session_id), **strip_chatgpt_client_credential_headers(headers), } + authenticator: Final = self._resolve_authenticator(litellm_params) + safe_headers: Final = strip_chatgpt_client_credential_headers(headers) configured_api_key: Final = litellm_params.api_key if litellm_params is not None else None if configured_api_key and not get_chatgpt_auth_file(litellm_params): return { **get_chatgpt_default_headers( - configured_api_key, extract_chatgpt_account_id(configured_api_key), session_id + configured_api_key, authenticator.get_account_id(configured_api_key), session_id ), - **headers, + **safe_headers, } - authenticator: Final = self._resolve_authenticator(litellm_params) try: access_token: Final = authenticator.get_access_token() except GetAccessTokenError as e: @@ -89,7 +89,7 @@ class ChatGPTResponsesAPIConfig(OpenAIResponsesAPIConfig): account_id: Final = authenticator.get_account_id() default_headers: Final = get_chatgpt_default_headers(access_token, account_id, session_id) - return {**default_headers, **headers} + return {**default_headers, **safe_headers} def transform_responses_api_request( self, diff --git a/litellm/llms/custom_httpx/llm_http_handler.py b/litellm/llms/custom_httpx/llm_http_handler.py index 905f0f22e43..973d0336ee9 100644 --- a/litellm/llms/custom_httpx/llm_http_handler.py +++ b/litellm/llms/custom_httpx/llm_http_handler.py @@ -2932,7 +2932,7 @@ class BaseLLMHTTPHandler: model=model, litellm_params=litellm_params, ), - extra_headers, + extra_headers if custom_llm_provider != "chatgpt" else None, ) # Check if streaming is requested @@ -3128,7 +3128,7 @@ class BaseLLMHTTPHandler: model=model, litellm_params=litellm_params, ), - extra_headers, + extra_headers if custom_llm_provider != "chatgpt" else None, ) # Check if streaming is requested 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 78fbea29c6b..f9d1dc28d12 100644 --- a/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py +++ b/tests/unit/llms/chatgpt/chat/test_chatgpt_chat_transformation.py @@ -172,7 +172,8 @@ class TestChatGPTMultiAccountChat: def _subscription_response(request: httpx.Request) -> httpx.Response: account: Final = request.headers["ChatGPT-Account-Id"] assert request.headers["Authorization"] in ( - f"Bearer token-{account.removeprefix('acct-')}", f"Bearer {_client_token(account)}" + f"Bearer token-{account.removeprefix('acct-')}", + f"Bearer {_client_token(account)}", ) assert "chatgpt_auth_file" not in request.content.decode() message: Final = { @@ -441,6 +442,7 @@ async def test_client_login_cannot_be_redirected_to_another_host( **credential, } ) + async def request() -> None: if endpoint == "chat": if async_call: @@ -566,3 +568,71 @@ async def test_sdk_extra_headers_use_client_login_without_gateway_auth( assert response.output[0].content[0].text == "acct-client-a" assert transport.calls.last.request.headers["Authorization"] == headers["Authorization"] assert len(transport.calls) == 1 + + +@pytest.mark.parametrize("endpoint", ["chat", "responses"]) +@pytest.mark.parametrize("sync", [False, True]) +@pytest.mark.parametrize("credential_source", ["default", "file", "api_key"]) +@pytest.mark.parametrize("authorization_header", [None, "Authorization", "authorization"]) +@pytest.mark.asyncio +async def test_requests_preserve_gateway_token_and_selected_workspace( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + endpoint: str, + sync: bool, + credential_source: str, + authorization_header: str | None, +) -> None: + monkeypatch.setattr("time.time", lambda: 1700000000) + monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) + monkeypatch.setenv("CHATGPT_AUTH_FILE", str(tmp_path / "default.json")) + token: Final = _client_token("acct-token-default") + auth_file: Final = tmp_path / ("selected.json" if credential_source == "file" else "default.json") + _write_auth_record(auth_file, token, "acct-saved-workspace") + configured_token: Final = _client_token("acct-configured") + expected_token: Final = configured_token if credential_source == "api_key" else token + expected_account: Final = "acct-configured" if credential_source == "api_key" else "acct-saved-workspace" + + def reply(request: httpx.Request) -> httpx.Response: + assert request.headers.get_list("authorization") == [f"Bearer {expected_token}"] + assert request.headers.get_list("chatgpt-account-id") == [expected_account] + assert request.headers["originator"] == "codex_cli_rs" + return _subscription_response( + httpx.Request( + "POST", + request.url, + content=request.content, + headers={ + "Authorization": f"Bearer token-{expected_account.removeprefix('acct-')}", + "ChatGPT-Account-Id": expected_account, + }, + ) + ) + + params: Final = MappingProxyType( + { + "model": "chatgpt/gpt-6.1-sol", + **({"chatgpt_auth_file": str(auth_file)} if credential_source == "file" else {}), + **({"api_key": configured_token} if credential_source == "api_key" else {}), + "extra_headers": { + "originator": "codex_cli_rs", + **({authorization_header: "Bearer sk-unrelated-example"} if authorization_header else {}), + }, + } + ) + with respx.mock as transport: + transport.post("https://chatgpt.com/backend-api/codex/responses").mock(side_effect=reply) + if endpoint == "chat": + result: Final = ( + await asyncio.to_thread(litellm.completion, **params, messages=[{"role": "user", "content": "Hello"}]) + if sync + else await litellm.acompletion(**params, messages=[{"role": "user", "content": "Hello"}]) + ) + assert result.choices[0].message.content == expected_account + else: + response: Final = ( + await asyncio.to_thread(litellm.responses, **params, input=[{"role": "user", "content": "Hello"}]) + if sync + else await litellm.aresponses(**params, input=[{"role": "user", "content": "Hello"}]) + ) + assert response.output[0].content[0].text == expected_account diff --git a/tests/unit/llms/chatgpt/responses/test_chatgpt_responses_transformation.py b/tests/unit/llms/chatgpt/responses/test_chatgpt_responses_transformation.py index 84ef5045c60..d88bdfc0b41 100644 --- a/tests/unit/llms/chatgpt/responses/test_chatgpt_responses_transformation.py +++ b/tests/unit/llms/chatgpt/responses/test_chatgpt_responses_transformation.py @@ -15,6 +15,7 @@ import httpx import pytest import litellm +from litellm.llms.chatgpt.authenticator import Authenticator from litellm.llms.chatgpt.responses.transformation import ChatGPTResponsesAPIConfig from litellm.llms.openai.common_utils import OpenAIError from litellm.main import responses_api_bridge_check @@ -455,17 +456,17 @@ class TestChatGPTClientCredentialForwarding: @pytest.mark.parametrize( "bearer", ["Bearer sk-proj-fake-platform-key", "Bearer not-a-jwt", "Bearer a.bm90LWpzb24.c"] ) - def test_non_chatgpt_bearer_falls_back_to_configured_key(self, bearer): - authenticator = _gateway_authenticator() + def test_non_chatgpt_bearer_falls_back_to_configured_key(self, bearer, tmp_path): + authenticator = Authenticator(auth_file=str(tmp_path / "missing-default.json")) - headers = _config_with(authenticator).validate_environment( + headers = ChatGPTResponsesAPIConfig(authenticator=authenticator).validate_environment( headers={"Authorization": bearer}, model="gpt-5.4", litellm_params=GenericLiteLLMParams(api_key="configured-gateway-key"), ) assert "ChatGPT-Account-Id" not in headers - authenticator.get_access_token.assert_not_called() + assert headers["Authorization"] == "Bearer configured-gateway-key" def test_configured_key_is_used_without_client_credential(self): authenticator = _gateway_authenticator() diff --git a/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py b/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py index 48e55e166e8..2e9e3462bee 100644 --- a/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py +++ b/tests/unit/llms/chatgpt/test_chatgpt_authenticator.py @@ -7,6 +7,8 @@ from typing import Final from unittest.mock import MagicMock, mock_open, patch import pytest +import httpx +import respx import litellm from litellm.constants import DEFAULT_REQUEST_TIMEOUT_SECONDS, HTTP_HANDLER_CONNECT_TIMEOUT_SECONDS @@ -280,3 +282,30 @@ class TestChatGPTMultiAccountAuthenticator: assert relative_key is not None and absolute_key is not None assert get_cached_authenticator(relative_key) is get_cached_authenticator(absolute_key) + + +def test_refresh_keeps_saved_workspace(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr("time.time", lambda: 1700000000) + auth_file: Final = tmp_path / "selected.json" + auth_file.write_text( + json.dumps( + { + "access_token": "expired-example-token", + "refresh_token": "example-refresh-token", + "expires_at": 1, + "account_id": "acct-selected-workspace", + } + ) + ) + token: Final = _make_jwt({"exp": 4102444800, "https://api.openai.com/auth": {"chatgpt_account_id": "acct-default"}}) + authenticator: Final = Authenticator(auth_file=str(auth_file)) + with respx.mock as transport: + transport.post("https://auth.openai.com/oauth/token").mock( + return_value=httpx.Response( + 200, + json={"access_token": token, "id_token": token, "refresh_token": "example-rotated-refresh"}, + ) + ) + assert authenticator.get_access_token() == token + assert authenticator.get_account_id(token) == "acct-selected-workspace" + assert json.loads(auth_file.read_text())["refresh_token"] == "example-rotated-refresh" diff --git a/tests/unit/llms/custom_httpx/test_llm_http_handler.py b/tests/unit/llms/custom_httpx/test_llm_http_handler.py index 80b46b11294..bbcd7f70e63 100644 --- a/tests/unit/llms/custom_httpx/test_llm_http_handler.py +++ b/tests/unit/llms/custom_httpx/test_llm_http_handler.py @@ -4676,51 +4676,6 @@ def test_image_edit_handler_keeps_the_sync_transform(): assert response.data[0].b64_json == "sync" -def test_responses_handler_lets_provider_see_client_extra_headers_before_choosing_credentials(): - """Providers pick between a client credential and their own login inside validate_environment, - so the request's extra_headers must be visible there, and the final headers must not carry - two case variants of the same header.""" - from unittest.mock import MagicMock - from litellm.llms.custom_httpx.http_handler import HTTPHandler - from litellm.llms.custom_httpx.llm_http_handler import BaseLLMHTTPHandler - - provider_config = MagicMock() - provider_config.validate_environment.side_effect = lambda headers, model, litellm_params: { - "Authorization": headers["authorization"].replace("client", "chosen"), - "ChatGPT-Account-Id": headers["chatgpt-account-id"], - "content-type": "application/json", - } - provider_config.get_complete_url.return_value = "https://chatgpt.example.com/responses" - provider_config.transform_responses_api_request.return_value = {"input": "hi"} - provider_config.should_fake_stream.return_value = False - provider_config.sign_request.side_effect = lambda headers, **_: (headers, None) - - mock_client = MagicMock(spec=HTTPHandler) - mock_client.post.return_value = MagicMock() - - BaseLLMHTTPHandler().response_api_handler( - model="gpt-5.4", - input="hi", - responses_api_provider_config=provider_config, - response_api_optional_request_params={"extra_headers": {"originator": "codex_cli_rs"}}, - custom_llm_provider="chatgpt", - litellm_params=GenericLiteLLMParams(), - logging_obj=MagicMock(), - extra_headers={"authorization": "Bearer client-token", "chatgpt-account-id": "acct-alice"}, - client=mock_client, - _is_async=False, - ) - - seen = provider_config.validate_environment.call_args.kwargs["headers"] - assert seen["originator"] == "codex_cli_rs" - assert seen["authorization"] == "Bearer client-token" - - sent = mock_client.post.call_args.kwargs["headers"] - assert [sent[name] for name in sent if name.lower() == "authorization"] == ["Bearer client-token"] - assert [sent[name] for name in sent if name.lower() == "chatgpt-account-id"] == ["acct-alice"] - assert sent["content-type"] == "application/json" - - class _ScriptedClientWebSocket(_FakeClientWebSocket): def __init__(self, messages: list[str], last_event_type: str) -> None: super().__init__()