BetaLenzon is in beta — the Free tier is 100% free while we're in beta (bring your own Anthropic key, public repos).See plans →
Lenzon

langchain-ai/langchain PR #37569 — OAuth-powered Codex access — PR #37569

langchain-ai/langchain · pull request #37569 ·

Loading…

Transcript

PlainEnglish

Welcome! Today we're walking through pull request thirty-seven thousand five sixty-nine, which adds a new way to authenticate to OpenAI's Codex backend using your ChatGPT subscription instead of an API key.

PlainEnglish

Let's frame what this pull request is pursuing. First, it enables ChatGPT subscription authentication for the Codex backend, so users can skip API keys entirely. Second, it implements a full OAuth two-point-oh PKCE flow with secure token storage on disk. On top of that, it adapts the ChatOpenAI class to meet the Codex backend's specific wire-level requirements, and it transparently lifts SystemMessage content into a top-level instructions parameter. Importantly, the existing API-key flow for ChatOpenAI remains completely unchanged.

PlainEnglish

Here's the problem this pull request solves. Before, the Codex backend at chatgpt dot com was only accessible with a ChatGPT subscription, and there was no way to integrate it with LangChain. The existing API-key flow doesn't work with the chatgpt dot com endpoints. After this change, we use OAuth bearer tokens to authenticate with subscription credentials. A new ChatOpenAICodex class provides a drop-in integration, and tokens are stored securely at the dedicated path tilde slash dot langchain slash chatgpt dash auth dot json.

Architecture

Let's dive into the changes. The new ChatOpenAICodex class extends ChatOpenAI and forces three wire-level requirements to match what the Codex backend expects: use responses API is true, store is false, and streaming is true. It also lifts SystemMessage content into the instructions parameter, because Codex rejects system messages in the chat input. The token provider injects Authorization and ChatGPT Account ID headers on every request.

Architecture

Here's how the OAuth flow works. The user calls login underscore chatgpt. LangChain generates a PKCE pair for security, then opens the authorization URL in the browser. The user authenticates on ChatGPT, which redirects back with an authorization code. LangChain exchanges that code plus the PKCE verifier for access and refresh tokens, then stores them securely at tilde slash dot langchain. This entire flow is self-contained in the new chatgpt underscore oauth module.

Architecture

Token storage is handled by FileChatGPTOAuthTokenProvider. It defaults to storing tokens at dot langchain slash chatgpt dash auth dot json in the user's home directory. The get token method reads from disk, checks if the token will expire within five minutes, and if so, refreshes it synchronously before returning. Writes are atomic with mode zero-oh-six-hundred, so only the file owner can read the token. This prevents accidental leaks through file permissions.

Architecture

One of the trickiest adaptations is how ChatOpenAICodex handles SystemMessage entries. The Codex backend rejects system messages in the chat input, so we automatically lift the SystemMessage content into a top-level instructions parameter and remove it from the messages list. This happens transparently inside the request payload method, so calling code can still pass SystemMessage instances without breaking. The lift logic only fires when the probe detects SystemMessage might be present, keeping the happy path fast.

PlainEnglish

After this pull request lands, LangChain users with ChatGPT Plus, Pro, Team, or Enterprise subscriptions can authenticate to the Codex backend using OAuth. ChatOpenAICodex becomes a drop-in alternative to ChatOpenAI. Tokens refresh automatically when they're within five minutes of expiring. Integration tests are backed by VCR cassettes with all OAuth secrets scrubbed, so CI can replay them safely. And importantly, the existing API-key flow for ChatOpenAI is completely untouched.

CodeQuality

Now let's look at the risks the analysis surfaced. We've plotted seven concerns on an impact versus likelihood matrix. The top-left quadrant holds lower-probability but higher-impact issues, like the file lock fallback on unsupported platforms and the lossy decode scrub bypass in test cassettes. The bottom-right shows more frequent but lower-impact concerns, like thread pool overhead on async token refresh. Let's deep-dive a couple of the more significant ones.

CodeQuality

Here's the file lock fallback concern. The underscore file underscore lock context manager tries fcntl on Unix and msvcrt on Windows. But if both imports fail or the OS doesn't support locking, it falls back to a no-op with just a warning. On those platforms, concurrent writes from multiple processes can race and corrupt the token file. The fallback is silent after the first warning, so users on unsupported platforms might not realize token storage isn't safe for multi-process use.

CodeQuality

The second concern is in the cassette scrubber. When a response body is malformed bytes and decode fails, the code falls back to lossy decode with errors equals replace. It then scrubs the lossy text and checks if the scrubbed version matches the original lossy text. If they match, it assumes no secrets were found and returns the lossy decode. But if the original body had a JWT that was mangled by the lossy decode, the regex might not match it, and the malformed JWT could leak through. The final JWT regex pass at the end is a backstop, but it's not guaranteed to catch every mangled pattern.

PlainEnglish

That wraps up our walkthrough. This pull request delivers a self-contained OAuth integration that unlocks Codex backend access for ChatGPT subscribers. It includes automatic token refresh and secure file-backed storage. We've noted several items to keep an eye on, particularly around file locking on unsupported platforms and the cassette scrub fallback for malformed responses. The OAuth flow lives in chatgpt underscore oauth dot py with PKCE and refresh-token rotation. SystemMessage lift logic is in codex dot py, and token storage defaults to tilde slash dot langchain slash chatgpt dash auth dot json with mode zero-six-hundred. Thanks for reviewing!

How this was made

Lenzon read langchain-ai/langchain at pull request #37569 and generated this walkthrough automatically. The narration above is the transcript of what it says.

Explain a pull request from your own repo

Point Lenzon at a repo or a pull request and get a narrated walkthrough like this one.

Try it