The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: answer get_api_base for github_copilot and chatgpt without running the login flow (#42602)
devin-ai-integration[bot]ca95fc2bd418
diff --git a/litellm/litellm_core_utils/llm_response_utils/get_api_base.py b/litellm/litellm_core_utils/llm_response_utils/get_api_base.pyindex 26e79fa0ea8..3815ea91b51 100644--- a/litellm/litellm_core_utils/llm_response_utils/get_api_base.py+++ b/litellm/litellm_core_utils/llm_response_utils/get_api_base.py@@ -3,10 +3,30 @@ from typing import Finalimport litellmfrom litellm import verbose_logger-from ...litellm_core_utils.get_llm_provider_logic import get_llm_provider+from ...litellm_core_utils.get_llm_provider_logic import (+ declared_authenticating_provider,+ get_llm_provider,+)from ...types.router import LiteLLM_Params+def _api_base_without_login(provider: str) -> str | None:+ if provider == "github_copilot":+ return litellm.GithubCopilotConfig().api_base_without_login()+ if provider == "chatgpt":+ return litellm.ChatGPTConfig().api_base_without_login()+ return None+++def _provider_default_api_base(model: str, custom_llm_provider: str | None, stream: bool) -> str | None:+ if custom_llm_provider == "gemini":+ action: Final = "streamGenerateContent" if stream else "generateContent"+ return f"https://generativelanguage.googleapis.com/v1beta/models/{model}:{action}"+ if custom_llm_provider == "openai":+ return "https://api.openai.com"+ return None++def get_api_base(model: str, optional_params: dict | LiteLLM_Params) -> str | None:"""Returns the api base used for calling the model.@@ -42,6 +62,9 @@ def get_api_base(model: str, optional_params: dict | LiteLLM_Params) -> str | Noif litellm.model_alias_map and model in litellm.model_alias_map:model = litellm.model_alias_map[model]+ declared: Final = declared_authenticating_provider(model, _optional_params.custom_llm_provider)+ if declared is not None:+ return _api_base_without_login(declared)try:(model,@@ -83,16 +106,4 @@ def get_api_base(model: str, optional_params: dict | LiteLLM_Params) -> str | No_api_base = f"{_optional_params.vertex_location}-aiplatform.googleapis.com/v1/projects/{_optional_params.vertex_project}/locations/{_optional_params.vertex_location}/publishers/google/models/{model}:generateContent"return _api_base- if custom_llm_provider is None:- return None-- if custom_llm_provider == "gemini":- if stream:- _api_base = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:streamGenerateContent"- else:- _api_base = f"https://generativelanguage.googleapis.com/v1beta/models/{model}:generateContent"- return _api_base- elif custom_llm_provider == "openai":- _api_base = "https://api.openai.com"- return _api_base- return None+ return _provider_default_api_base(model, custom_llm_provider, stream)diff --git a/litellm/llms/chatgpt/chat/transformation.py b/litellm/llms/chatgpt/chat/transformation.pyindex e35408b0829..1b110704c8b 100644--- a/litellm/llms/chatgpt/chat/transformation.py+++ b/litellm/llms/chatgpt/chat/transformation.py@@ -23,6 +23,9 @@ class ChatGPTConfig(OpenAIConfig):super().__init__()self.authenticator = Authenticator()+ def api_base_without_login(self) -> str:+ return self.authenticator.get_api_base()+def _get_openai_compatible_provider_info(self,model: str,@@ -30,7 +33,7 @@ class ChatGPTConfig(OpenAIConfig):api_key: str | None,custom_llm_provider: str,) -> tuple[str | None, str | None, str]:- dynamic_api_base: Final = self.authenticator.get_api_base()+ dynamic_api_base: Final = self.api_base_without_login()try:dynamic_api_key: Final = self.authenticator.get_access_token()except GetAccessTokenError as e:diff --git a/litellm/llms/github_copilot/chat/transformation.py b/litellm/llms/github_copilot/chat/transformation.pyindex 8634b374f1b..169b9a037a5 100644--- a/litellm/llms/github_copilot/chat/transformation.py+++ b/litellm/llms/github_copilot/chat/transformation.py@@ -31,6 +31,14 @@ class GithubCopilotConfig(OpenAIConfig):super().__init__()self.authenticator = Authenticator()+ def api_base_without_login(self, api_base: str | None = None) -> str:+ return (+ api_base+ or self.authenticator.get_api_base()+ or os.getenv("GITHUB_COPILOT_API_BASE")+ or DEFAULT_GITHUB_COPILOT_API_BASE+ )+def _get_openai_compatible_provider_info(self,model: str,@@ -38,12 +46,7 @@ class GithubCopilotConfig(OpenAIConfig):api_key: str | None,custom_llm_provider: str,) -> tuple[str | None, str | None, str]:- dynamic_api_base: Final = (- api_base- or self.authenticator.get_api_base()- or os.getenv("GITHUB_COPILOT_API_BASE")- or DEFAULT_GITHUB_COPILOT_API_BASE- )+ dynamic_api_base: Final = self.api_base_without_login(api_base)try:dynamic_api_key: Final = self.authenticator.get_api_key()except GetAPIKeyError as e:diff --git a/tests/test_litellm/litellm_core_utils/llm_response_utils/test_get_api_base.py b/tests/test_litellm/litellm_core_utils/llm_response_utils/test_get_api_base.pynew file mode 100644index 00000000000..63977c30270--- /dev/null+++ b/tests/test_litellm/litellm_core_utils/llm_response_utils/test_get_api_base.py@@ -0,0 +1,93 @@+import json++import pytest++import litellm+from litellm.litellm_core_utils.llm_response_utils import get_api_base as get_api_base_module+from litellm.llms.chatgpt.common_utils import CHATGPT_API_BASE+from litellm.llms.github_copilot.common_utils import DEFAULT_GITHUB_COPILOT_API_BASE+++def isolated_token_dirs(tmp_path, monkeypatch):+ monkeypatch.setenv("GITHUB_COPILOT_TOKEN_DIR", str(tmp_path / "github_copilot"))+ monkeypatch.setenv("CHATGPT_TOKEN_DIR", str(tmp_path / "chatgpt"))+ monkeypatch.delenv("GITHUB_COPILOT_API_BASE", raising=False)+ monkeypatch.delenv("CHATGPT_API_BASE", raising=False)+ monkeypatch.delenv("OPENAI_CHATGPT_API_BASE", raising=False)+ return tmp_path+++def resolution_lookups(monkeypatch):+ lookups: list = []++ def _record(*args, **kwargs):+ lookups.append((args, kwargs))+ raise RuntimeError("provider resolution must not run for an authenticating provider")++ monkeypatch.setattr(get_api_base_module, "get_llm_provider", _record)+ return lookups+++class TestDeclaredAuthenticatingProvider:+ """get_llm_provider runs the OAuth device flow for github_copilot and chatgpt, and get_api_base+ runs on every response's hidden params and on every mapped exception, so it must answer from+ the declaration without resolving. The recorder appends before raising, and get_api_base+ swallows resolver errors, so an empty list proves the lookup never ran."""++ @pytest.mark.parametrize(+ "model, custom_llm_provider, expected",+ [+ ("github_copilot/gpt-4o", None, DEFAULT_GITHUB_COPILOT_API_BASE),+ ("gpt-4o", "github_copilot", DEFAULT_GITHUB_COPILOT_API_BASE),+ ("chatgpt/gpt-5", None, CHATGPT_API_BASE),+ ("gpt-5", "chatgpt", CHATGPT_API_BASE),+ ],+ )+ def test_answers_without_resolving(+ self, model, custom_llm_provider, expected, isolated_token_dirs, resolution_lookups+ ):+ api_base = litellm.get_api_base(model=model, optional_params={"custom_llm_provider": custom_llm_provider})++ assert resolution_lookups == []+ assert api_base == expected++ def test_copilot_keeps_the_enterprise_endpoint_from_disk(self, isolated_token_dirs, resolution_lookups):+ token_dir = isolated_token_dirs / "github_copilot"+ token_dir.mkdir()+ (token_dir / "api-key.json").write_text(+ json.dumps({"endpoints": {"api": "https://api.enterprise.githubcopilot.com"}})+ )++ api_base = litellm.get_api_base(model="github_copilot/gpt-4o", optional_params={})++ assert resolution_lookups == []+ assert api_base == "https://api.enterprise.githubcopilot.com"++ def test_explicit_api_base_still_wins(self, isolated_token_dirs, resolution_lookups):+ api_base = litellm.get_api_base(+ model="github_copilot/gpt-4o", optional_params={"api_base": "https://copilot.example/v1"}+ )++ assert resolution_lookups == []+ assert api_base == "https://copilot.example/v1"++ def test_other_providers_still_resolve(self, isolated_token_dirs, resolution_lookups):+ litellm.get_api_base(model="openai/gpt-4o", optional_params={})++ assert len(resolution_lookups) == 1+++ "model, expected",+ [+ ("gemini/gemini-2.5-pro", "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-pro:generateContent"),+ ("openai/gpt-4o", "https://api.openai.com"),+ ],+)+def test_providers_with_a_fixed_base_still_get_it(model, expected, monkeypatch):+ for env in ("GEMINI_API_BASE", "OPENAI_API_BASE", "OPENAI_BASE_URL"):+ monkeypatch.delenv(env, raising=False)++ assert litellm.get_api_base(model=model, optional_params={}) == expecteddiff --git a/tests/test_litellm/rerank_api/test_main.py b/tests/test_litellm/rerank_api/test_main.pyindex aca0c970dd5..f56673fec30 100644--- a/tests/test_litellm/rerank_api/test_main.py+++ b/tests/test_litellm/rerank_api/test_main.py@@ -239,7 +239,6 @@ async def test_arerank_error_is_mapped_to_litellm_exception(respx_mock: respx.Mo@pytest.mark.asyncio[email protected](300)async def test_arerank_declared_authenticating_provider_skips_resolution(monkeypatch):"""Regression for the event-loop hazard in arerank's provider pre-resolution:get_llm_provider runs the blocking OAuth device flow for github_copilot/chatgpt,@@ -257,6 +256,9 @@ async def test_arerank_declared_authenticating_provider_skips_resolution(monkeypraise BaseLLMException(status_code=401, message='{"error":"bad key"}')monkeypatch.setattr(litellm, "get_llm_provider", record_resolution)+ monkeypatch.setattr(+ "litellm.litellm_core_utils.llm_response_utils.get_api_base.get_llm_provider", record_resolution+ )monkeypatch.setattr("litellm.rerank_api.main.rerank", rerank_raises_provider_error)with pytest.raises(litellm.AuthenticationError) as exc_info: