Skip to content

fix(codex): preserve proxy env when creating OpenAI client#12008

Closed
ikumiyo wants to merge 1 commit into
NousResearch:mainfrom
ikumiyo:fix-codex-proxy-transport
Closed

fix(codex): preserve proxy env when creating OpenAI client#12008
ikumiyo wants to merge 1 commit into
NousResearch:mainfrom
ikumiyo:fix-codex-proxy-transport

Conversation

@ikumiyo

@ikumiyo ikumiyo commented Apr 18, 2026

Copy link
Copy Markdown

Summary

Fixes a regression where Codex requests can time out for users who rely on http_proxy / https_proxy.

_create_openai_client() injects a custom httpx.Client with HTTPTransport(socket_options=...) for TCP keepalives. When proxy environment variables are configured, passing a custom transport
bypasses httpx's default environment proxy handling. This causes Codex requests to https://chatgpt.com/backend-api/codex to attempt direct connection instead of using the configured proxy.

Root Cause

With proxy env vars configured, the default httpx client can reach the Codex endpoint through the proxy, but the custom transport path times out:

default_httpx status=401 elapsed=1.07s
custom_transport_keepalive ERROR ConnectTimeout elapsed=20.03s

## Fix

Skip the keepalive http_client injection when any proxy environment variable is present:

- HTTP_PROXY
- HTTPS_PROXY
- ALL_PROXY
- http_proxy
- https_proxy
- all_proxy

This preserves default OpenAI SDK / httpx proxy behavior for proxied environments, while keeping the keepalive transport for non-proxy environments.

## Tests

Added a regression test to ensure _create_openai_client() does not inject a custom http_client when proxy env vars are set.

Tested locally:

HERMES_HOME=/tmp/hermes-test-home python -m pytest \
  tests/run_agent/test_create_openai_client_kwargs_isolation.py \
  tests/run_agent/test_create_openai_client_reuse.py -q

4 passed

Note: scripts/run_tests.sh could not run in my local venv because pip is missing:

/venv/bin/python: No module named pip

@teknium1

Copy link
Copy Markdown
Contributor

Thanks @ikumiyo — the diagnosis here was correct: the custom httpx transport from #11277 suppresses env-proxy handling.

Fixed on main via #12657 using an alternative approach that forwards the proxy env explicitly to httpx.Client(proxy=...) while keeping the keepalive transport in place. Skipping the custom transport entirely (as here) would silently opt proxy users out of the #10324 dead-connection detection, which we wanted to avoid.

Closing as superseded. Appreciate the detailed test case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants