fix(api): ignore inherited v1 base URL for Codex (#1718)

This commit is contained in:
de1ty
2026-05-26 16:55:23 +08:00
committed by GitHub
parent eaf3048f2c
commit 41a2ccabf8
2 changed files with 38 additions and 2 deletions
@@ -110,9 +110,15 @@ class CodexLLM(LLMInterface):
"Or use a different provider (openai, anthropic, gemini) with API keys."
) from e
# Use ChatGPT backend API endpoint
if not self.base_url:
# Use ChatGPT backend API endpoint. Codex auth is tied to
# chatgpt.com/backend-api, not the OpenAI-compatible base URL used by
# other providers. Deployments often set a global LLM_BASE_URL for an
# OpenAI-compatible proxy; ignore that inherited value unless the user
# explicitly provides a Codex backend URL.
if not self.base_url or self.base_url.rstrip("/").endswith("/v1"):
self.base_url = "https://chatgpt.com/backend-api"
else:
self.base_url = self.base_url.rstrip("/")
# Normalize model name (strip openai/ prefix if present)
if self.model.startswith("openai/"):
@@ -0,0 +1,30 @@
"""Tests for Codex provider base URL handling."""
from unittest.mock import patch
from hindsight_api.engine.providers.codex_llm import CodexLLM
def _make(base_url: str) -> CodexLLM:
with (
patch.object(CodexLLM, "_load_codex_auth", return_value=("at", "acct")),
patch.object(CodexLLM, "_load_codex_refresh_token", return_value="rt"),
):
return CodexLLM(
provider="openai-codex",
api_key="ignored",
base_url=base_url,
model="gpt-5.4-mini",
)
def test_codex_uses_chatgpt_backend_when_base_url_empty():
assert _make("").base_url == "https://chatgpt.com/backend-api"
def test_codex_ignores_inherited_openai_compatible_v1_base_url():
assert _make("https://newapi.example.com/v1").base_url == "https://chatgpt.com/backend-api"
def test_codex_preserves_explicit_codex_backend_base_url_without_trailing_slash():
assert _make("https://chatgpt.example.com/backend-api/").base_url == "https://chatgpt.example.com/backend-api"