"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(providers): an explicit header survives its ambient twin under openai 2.x and 3.x (#1573)
Haozhe Wu218041a43250
diff --git a/agent/src/providers/llm.py b/agent/src/providers/llm.pyindex bf6d436dd..b3ba54316 100644--- a/agent/src/providers/llm.py+++ b/agent/src/providers/llm.py@@ -287,6 +287,7 @@ if ChatOpenAI is not None:_vibe_provider: Optional[str] = PrivateAttr(default=None)_vibe_api_key: str = PrivateAttr(default="")_vibe_ambient_header_names: tuple[str, ...] = PrivateAttr(default=())+ _vibe_explicit_twin_headers: dict[str, Any] = PrivateAttr(default_factory=dict)_vibe_has_explicit_authorization: bool = PrivateAttr(default=False)_vibe_owned_http_clients: tuple[Any, ...] = PrivateAttr(default=())_vibe_fallback_session_id: str = PrivateAttr(default="")@@ -318,9 +319,23 @@ if ChatOpenAI is not None:# scheduled research, CLI) every request from this adapter still# shares a stable conversation identity.self._vibe_fallback_session_id = uuid.uuid4().hex+ # Every ambient spelling is omitted per request, and an explicit+ # header sharing its name in another case is put back after the+ # omits (see _provider_scoped_extra_headers). Excluding the twin+ # from the omits instead is right only under openai 3.x's+ # case-insensitive merge; openai 2.x merges with a plain dict and+ # would send the ambient ``user-agent`` beside ``User-Agent`` (#1573).self._vibe_ambient_header_names = tuple(- name for name in ambient_names if name.lower() not in explicit_names_lower+ name for name in ambient_names if name not in explicit_names)+ ambient_names_lower = {name.lower() for name in self._vibe_ambient_header_names}+ self._vibe_explicit_twin_headers = {+ str(name): value+ for name, value in (+ explicit_headers.items() if isinstance(explicit_headers, Mapping) else ()+ )+ if str(name).lower() in ambient_names_lower+ }self._vibe_has_explicit_authorization = ("authorization" in explicit_names_lower)@@ -356,6 +371,10 @@ if ChatOpenAI is not None:ambient_authorization = (ambient_authorization or name.lower() == "authorization")+ # openai 3.x applies an omit case-insensitively, so the omit for an+ # ambient ``user-agent`` also took the provider's ``User-Agent``.+ # Inserted after the omits, the explicit value is the one that stays.+ overrides.update(self._vibe_explicit_twin_headers)# OPENAI_CUSTOM_HEADERS can override the SDK's normal Bearer header.# Remove every captured spelling, then restore the selected providerdiff --git a/agent/tests/test_provider_header_isolation.py b/agent/tests/test_provider_header_isolation.pyindex adb108f55..2ca223102 100644--- a/agent/tests/test_provider_header_isolation.py+++ b/agent/tests/test_provider_header_isolation.py@@ -156,6 +156,52 @@ def test_openrouter_async_stream_ignores_non_ascii_ambient_headers() -> None:assert seen["authorization"] == "Bearer sk-or-test"+ ChatOpenAIWithReasoning is None,+ reason="langchain-openai is not installed",+)+def test_an_explicit_header_wins_over_its_ambient_twin_on_the_wire() -> None:+ """One header, the explicit value, whatever spelling the ambient env used (#1573).++ openai 2.x merges OPENAI_CUSTOM_HEADERS under default_headers with a plain+ dict (an ambient ``user-agent`` beside an explicit ``User-Agent`` sent both);+ openai 3.x merges case-insensitively and applies a per-request omit the same+ way (an omit for the ambient spelling stripped the explicit header). The+ repo's lock pins 2.x and CI installs 3.x, so both must hold.+ """+ seen: list[httpx.Headers] = []++ def handler(request: httpx.Request) -> httpx.Response:+ seen.append(request.headers)+ return _stream_response("ok")++ env = {"OPENAI_CUSTOM_HEADERS": "user-agent: ambient\nX-Other: ambient"}+ with patch.dict(os.environ, env, clear=True):+ with httpx.Client(transport=httpx.MockTransport(handler)) as client:+ llm = ChatOpenAIWithReasoning(+ model="kimi-for-coding",+ api_key="sk-test",+ base_url="https://relay.invalid/v1",+ default_headers={"User-Agent": "provider-value"},+ http_client=client,+ vibe_provider="kimi-coding",+ vibe_api_key="sk-test",+ )+ list(llm.stream("hello"))++ assert seen[0].get_list("user-agent") == ["provider-value"]+ assert "x-other" not in seen[0]+ # The wire above is checked against whichever openai CI installed; the+ # overrides are checked here so both merge styles are pinned on any version:+ # the ambient spelling is omitted (2.x), and the explicit value comes after+ # that omit (3.x drops every spelling of a name when it meets the omit).+ overrides = llm._provider_scoped_extra_headers()+ names = list(overrides)+ assert type(overrides["user-agent"]).__name__ == "Omit"+ assert overrides["User-Agent"] == "provider-value"+ assert names.index("User-Agent") > names.index("user-agent")++def test_build_rejects_non_ascii_openrouter_api_key_before_transport() -> None:"""A malformed Bearer credential should name its setting without leaking it."""import src.providers.llm as llm_mod