Use ChatGPT Plus/Pro Codex (OAuth) as a LangChain/LangGraph chat model
Project description
langchain-codex-oauth
Use the Codex models included with a ChatGPT Plus/Pro subscription inside LangChain/LangGraph.
This project authenticates locally via OpenAI OAuth (no OPENAI_API_KEY) and talks to the ChatGPT/Codex backend.
What this is
- A dev-friendly adapter that makes Codex feel like a native-ish LangChain chat model.
- Local OAuth login (
langchain-codex-oauth auth login) storing credentials under~/.langchain-codex-oauth/. - Streaming support via SSE (
.stream()yields chunks as they arrive). - Async support via
.ainvoke()/.astream(). - Tool calling via
.bind_tools(...)(useful for LangGraph agents). - Streaming tool call chunks (
tool_call_chunks) when available. - Stop sequences (
stop=[...]) for invoke/stream (best-effort). - Response and usage metadata (
response_metadata,usage_metadata) when available.
What this is not
- Not for sharing accounts/subscriptions.
- Not intended for production or multi-user hosting.
Stability (v1.0)
- The Python API (
ChatCodexOAuth, CLI commands, and message/tool wiring) is considered stable. - The upstream backend is a ChatGPT consumer endpoint and may change without notice. If that happens, new releases may be required to restore compatibility.
- Recommendation: pin a minor version in real projects (e.g.
langchain-codex-oauth>=1.0,<1.1) and upgrade when needed.
Requirements
- Python
>=3.10 - Active ChatGPT Plus/Pro subscription with Codex access
Install
python -m pip install langchain-codex-oauth
Authenticate (one time)
langchain-codex-oauth auth login
# If port 1455 is busy or you’re on a remote machine:
langchain-codex-oauth auth login --manual
Quickstart
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_codex_oauth import ChatCodexOAuth
model = ChatCodexOAuth(model="gpt-5.2-codex")
messages = [
SystemMessage(content="You are a concise assistant."),
HumanMessage(content="Say hello and give a one-line coding tip."),
]
for chunk in model.stream(messages):
print(chunk.content, end="", flush=True)
Or run the examples (see examples/README.md for details):
python examples/langchain/hello.py
python examples/langchain/tools.py
python examples/langchain/async_hello.py
python examples/langchain/chatopenai_compatibility.py
python examples/langchain/flags_and_params.py
python examples/langchain/usage_and_metadata.py
python examples/langchain/tool_call_chunks.py
# Optional (requires installing langgraph in a separate venv)
python examples/langgraph/system_prompt_drift.py --mode strict
Configuration knobs (ChatOpenAI-like)
ChatCodexOAuth supports a small set of common ChatOpenAI knobs:
timeout(seconds) andmax_retriestemperatureandmax_tokens(best-effort passthrough)stop=[...]for.invoke/.stream/.ainvoke/.astream(best-effort)
Additional Codex-specific knob:
system_prompt_mode("strict"default,"default","disabled") to reduce system-prompt drift on the consumer backend.
RAG / Embeddings
ChatCodexOAuth is a chat model, not an embedding model.
- For RAG, you can pair it with local embeddings (e.g. Ollama
mxbai-embed-large) or OpenAI embeddings. - The critical rule is: the embedding model used to index documents must match the embedding model used to embed queries.
- See
examples/langchain/rag_chroma_ab.pyfor an A/B harness (Chroma + persisted local DB).
Notes
- The Codex backend requires validated
instructions. By default the library uses cached prompts, attempts GitHub fetch if missing, and falls back to bundled prompts (override withLANGCHAIN_CODEX_OAUTH_INSTRUCTIONS_MODE). - The adapter aims to be OpenAI-like: tool schemas from
convert_to_openai_tool(...)andwith_structured_output(...)are supported. - If you hit ChatGPT usage limits, the library normalizes some backend “usage limit” errors to HTTP 429 semantics.
- Usage counts and finish reasons are best-effort (the backend is not a stable public API).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file langchain_codex_oauth-1.0.0.tar.gz.
File metadata
- Download URL: langchain_codex_oauth-1.0.0.tar.gz
- Upload date:
- Size: 33.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
89b0af6e9731d6ebf61d1846696b20695f6f75405b5c52f9de1d82ec4187e2ce
|
|
| MD5 |
ecdff1cb7a559f55973753791f2e0fb2
|
|
| BLAKE2b-256 |
3760085af1147ffd1430b326bf778acc98f02497059527130783cd41e78c3752
|
File details
Details for the file langchain_codex_oauth-1.0.0-py3-none-any.whl.
File metadata
- Download URL: langchain_codex_oauth-1.0.0-py3-none-any.whl
- Upload date:
- Size: 32.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7c81bbb8df62ef08231afab88bc5ed3781a6641d441896580a6f7c5183f2f2a
|
|
| MD5 |
d7ee1471d737fe9184ee4067b806b2c2
|
|
| BLAKE2b-256 |
91d1b4113b7383a1580f6dd11efaaadd2295b0654fa264344ed89e8bf63c852d
|