Skip to content

fix(openai): build Codex async headers off the event loop in _agenerate - #38129

Merged
Mason Daugherty (mdrxy) merged 1 commit into
masterfrom
mdrxy/openai-codex-cleanup
Jun 13, 2026
Merged

fix(openai): build Codex async headers off the event loop in _agenerate#38129
Mason Daugherty (mdrxy) merged 1 commit into
masterfrom
mdrxy/openai-codex-cleanup

Conversation

@mdrxy

Copy link
Copy Markdown
Member

The Codex _astream path was reworked to build its auth headers from an async-fetched token, but _agenerate was left on the old "prime the cache, then read it back synchronously" approach. That sync read still went through _FileChatGPTOAuthTokenProvider.get_token, which acquires a thread lock and a cross-process file lock on every call — blocking the event loop even when the token is already warm. Both async paths now build headers the same way, so neither touches sync get_token on the loop.

Changes

  • _ChatOpenAICodex._agenerate now fetches the token via aget_token, builds the Codex headers off-loop, and hands them to _get_request_payload through the private _codex_headers kwarg — eliminating the synchronous token read (and its lock acquisition) that previously ran on the event loop inside super()._agenerate.
  • Replaced the duplicated "_codex_headers" string literal across _agenerate, _astream, and _get_request_payload with a _CODEX_HEADERS_KWARG module constant, documenting that the kwarg is popped before the payload reaches the SDK.
  • Documented the deliberate is not None check in _get_request_payload: an explicitly-built empty header dict (accountless token with originator=None) is honored as-is rather than falling back to the blocking sync read.

…ate`

The Codex `_astream` path was reworked to build its auth headers from an async-fetched token, but `_agenerate` was left on the old "prime the cache, then read it back synchronously" approach. That sync read still went through `_FileChatGPTOAuthTokenProvider.get_token`, which acquires a thread lock and a cross-process file lock on every call — blocking the event loop even when the token is already warm. Both async paths now build headers the same way, so neither touches sync `get_token` on the loop.

## Changes
- `_ChatOpenAICodex._agenerate` now fetches the token via `aget_token`, builds the Codex headers off-loop, and hands them to `_get_request_payload` through the private `_codex_headers` kwarg — eliminating the synchronous token read (and its lock acquisition) that previously ran on the event loop inside `super()._agenerate`.
- Replaced the duplicated `"_codex_headers"` string literal across `_agenerate`, `_astream`, and `_get_request_payload` with a `_CODEX_HEADERS_KWARG` module constant, documenting that the kwarg is popped before the payload reaches the SDK.
- Documented the deliberate `is not None` check in `_get_request_payload`: an explicitly-built empty header dict (accountless token with `originator=None`) is honored as-is rather than falling back to the blocking sync read.
@github-actions github-actions Bot added fix For PRs that implement a fix integration PR made that is related to a provider partner package integration internal openai `langchain-openai` package issues & PRs size: S 50-199 LOC labels Jun 13, 2026

@open-swe open-swe Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Open SWE Review: No issues found

Open SWE reviewed this PR and found no potential bugs to report.

Open in WebView Open SWE trace

@mdrxy
Mason Daugherty (mdrxy) merged commit 37b339f into master Jun 13, 2026
89 checks passed
@mdrxy
Mason Daugherty (mdrxy) deleted the mdrxy/openai-codex-cleanup branch June 13, 2026 05:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix For PRs that implement a fix integration PR made that is related to a provider partner package integration internal openai `langchain-openai` package issues & PRs size: S 50-199 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant