Documentation Index
Fetch the complete documentation index at: https://langchain-5e9cc07a-preview-mdrxyo-1779336777-59064a9.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
ChatOpenAICodex is a ChatOpenAI variant that authenticates with a ChatGPT OAuth session and routes requests to the ChatGPT Codex backend.
Use ChatOpenAICodex when you want to call Codex models through ChatGPT subscription authentication. For standard OpenAI API-key authentication, use ChatOpenAI instead.
Overview
Integration details
| Class | Package | Serializable | JS/TS Support | Downloads | Latest Version |
|---|---|---|---|---|---|
ChatOpenAICodex | langchain-openai | ❌ | ❌ |
Model features
| Tool calling | Structured output | Image input | Audio input | Video input | Token-level streaming | Native async | Token usage | Logprobs |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ |
Setup
To access Codex models withChatOpenAICodex, install the langchain-openai integration package and complete a ChatGPT OAuth sign-in.
Installation
One-time login
Run the browser-based ChatGPT OAuth flow:ChatOpenAICodex reads this file by default, so you usually only need to log in once.
Instantiation
After login, instantiate the model:instructions is the system prompt.The ChatGPT Codex backend requires a top-level instructions field on every request and will return 400 Instructions are required if it is missing. ChatOpenAICodex ships a generic fallback so quick experiments work, but you should almost always set your own — either at construction time (as above) or per call via model.invoke(..., instructions=...).A SystemMessage in the input list is not the same thing: it becomes a system-role entry in the input array, while instructions is a separate top-level field. Prefer instructions for the Codex backend.Invocation
Async usage
Headless login
For remote servers or environments without a browser, use the device-code flow:Custom token store
By default, tokens are stored in~/.langchain/chatgpt-auth.json.
To use a different path, pass store_path during login and pass the returned provider to the model:
Behavior differences from ChatOpenAI
ChatOpenAICodex is a specialized ChatOpenAI variant with Codex-specific defaults:
- Uses the ChatGPT Codex backend by default:
https://chatgpt.com/backend-api/codex - Authenticates with ChatGPT OAuth tokens instead of an OpenAI API key.
- Forces Responses API behavior with
use_responses_api=Trueandoutput_version="responses/v1". - Requires a top-level
instructions(system prompt) on every request and injects a generic fallback if you do not provide one. - Adds ChatGPT account headers automatically when available.
- Stores OAuth tokens separately from Codex CLI and VS Code authentication to avoid invalidating those sessions.
ChatOpenAI instead:
Custom originator header
ChatOpenAICodex sends an originator: langchain request header so OpenAI telemetry attributes calls to this package. Downstream consumers (e.g., a framework or product built on top of ChatOpenAICodex) can override the value to identify themselves instead.
Set it on the constructor:
LANGCHAIN_CODEX_ORIGINATOR environment variable to tag every instance in a deployment without code changes:
- Constructor / kwarg value.
- The
LANGCHAIN_CODEX_ORIGINATORenv var, if set and non-empty. - The package default (
"langchain").
include_originator_header=False. To override on a single call only, use extra_headers={"originator": "..."} — caller-supplied headers always win over the model’s configured value.
Troubleshooting
BadRequestError: Instructions are required
The Codex backend rejects requests without a top-level instructions field. ChatOpenAICodex injects a generic fallback, so this error usually means a caller explicitly passed instructions="" or instructions=None. Set a non-empty system prompt:
No ChatGPT OAuth token found
Run login first:Browser login cannot bind to the default port
login_chatgpt() uses localhost port 1455 by default. If that port is unavailable, pass another one:
Refresh token is invalid
If token refresh fails withinvalid_grant, re-run login:
Running on a server
Use the device-code flow:Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

