From 003352ff079fe74ba7b5da5d9f3cc0d289306710 Mon Sep 17 00:00:00 2001 From: mateo Date: Sat, 19 Sep 2026 06:09:18 +0000 Subject: [PATCH] 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