codex-auth-helper
codex-auth-helper turns an existing local Codex auth session into either:
- a
pydantic-aiResponses model - a LangChain
ChatOpenAImodel pinned to the OpenAI Responses API
It reads ~/.codex/auth.json, refreshes access tokens when needed, builds
Codex-specific OpenAI clients for the Responses endpoint, and returns either a
ready-to-use CodexResponsesModel or a LangChain chat model.
What It Does
- Reads tokens from
~/.codex/auth.json - Derives
ChatGPT-Account-Idfrom the auth file or token claims - Refreshes expired access tokens with
https://auth.openai.com/oauth/token - Writes refreshed tokens back to the auth file with private, atomic file replacement
- Builds an OpenAI-compatible client pointed at
https://chatgpt.com/backend-api/codex - Returns a
pydantic-airesponses model that already applies the Codex backend requirements - Returns a LangChain
ChatOpenAImodel configured for the Responses API
The helper enforces two backend-specific behaviors for you:
openai_store=False- an SSE Responses request even when
pydantic-aicalls the non-streamedrequest()path - incremental delta delivery when callers use the Pydantic AI or LangChain streaming APIs
What It Does Not Do
- It does not log you into Codex
- It does not create
~/.codex/auth.json - It does not provide generic Chat Completions wiring
- It does not replace
pydantic-ai; it only provides a model/client factory
Install
For the latest stable release:
uv add codex-auth-helper
pip install codex-auth-helper
For LangChain usage:
uv add "codex-auth-helper[langchain]"
pip install "codex-auth-helper[langchain]"
You also need an existing Codex auth session on the same machine:
~/.codex/auth.json
If you have not logged in yet:
codex login
Quick Start
from codex_auth_helper import create_codex_responses_model
from pydantic_ai import Agent
model = create_codex_responses_model(
"gpt-5.4",
instructions="You are a helpful coding assistant.",
)
agent = Agent(model)
result = agent.run_sync("Naber")
print(result.output)
LangChain Quick Start
from codex_auth_helper import create_codex_chat_openai
from langchain.agents import create_agent
graph = create_agent(
model=create_codex_chat_openai(
"gpt-5.4",
instructions="You are a helpful coding assistant.",
),
tools=[],
name="codex-graph",
)
The LangChain helper returns langchain_openai.ChatOpenAI configured to:
- use the Codex Responses endpoint
- reuse local Codex auth state
- keep
use_responses_api=True - default to
output_version="responses/v1" - require
instructions=and pass it through to the Responses request
instructions is mandatory for create_codex_chat_openai(...). The helper does not provide an
implicit system prompt for the LangChain path; callers must pass the behavior they want explicitly.
The same rule applies to create_codex_responses_model(...) on the Pydantic path. Pass the Codex
system behavior to the helper directly instead of relying on a separate agent-level instruction just
to seed the model.
Streaming
CodexResponsesModel.request_stream() forwards Responses API deltas as they arrive; it does not
wait for the completed response before yielding text. Consume it through the ordinary Pydantic AI
agent streaming surface:
import asyncio
from codex_auth_helper import create_codex_responses_model
from pydantic_ai import Agent
async def main() -> None:
agent = Agent(
create_codex_responses_model(
"gpt-5.4",
instructions="You are a concise coding assistant.",
)
)
async with agent.run_stream("Explain this repository in three sentences.") as result:
async for delta in result.stream_text(delta=True):
print(delta, end="", flush=True)
asyncio.run(main())
The LangChain factory enables model streaming by default. astream() therefore yields
AIMessageChunk values incrementally:
import asyncio
from codex_auth_helper import create_codex_chat_openai
async def main() -> None:
model = create_codex_chat_openai(
"gpt-5.4",
instructions="You are a concise coding assistant.",
)
async for chunk in model.astream("Explain this repository in three sentences."):
print(chunk.text, end="", flush=True)
asyncio.run(main())
Pass streaming=False to create_codex_chat_openai(...) only when a LangChain consumer explicitly
requires the non-streaming model path. This option does not disable the Codex backend's required SSE
transport for Pydantic AI request() calls.
Custom Auth Path
If you want to read a different auth file, pass a custom config:
from pathlib import Path
from codex_auth_helper import CodexAuthConfig, create_codex_responses_model
config = CodexAuthConfig(auth_path=Path("/tmp/codex-auth.json"))
model = create_codex_responses_model(
"gpt-5.4",
config=config,
instructions="You are a helpful coding assistant.",
)
Auth State Safety
The auth state file contains credentials and should be treated as private host state.
When refreshed tokens are written back, CodexAuthStore uses a private temp file, fsync, atomic
replace, and POSIX 0600 permissions for the final file. If replace fails, the previous auth file is
left intact and the temp file is cleaned up.
Keep the parent directory private and do not copy auth state into logs, examples, test fixtures, or container images.
Passing Extra OpenAI Responses Settings
Additional OpenAIResponsesModelSettings can still be passed through. The helper
keeps openai_store=False unless you explicitly override the model after
construction.
from codex_auth_helper import create_codex_responses_model
model = create_codex_responses_model(
"gpt-5.4",
instructions="You are a helpful coding assistant.",
settings={
"openai_reasoning_summary": "concise",
},
)
Lower-Level Client Factory
If you only want the authenticated OpenAI client, use create_codex_async_openai(...):
from codex_auth_helper import create_codex_async_openai
client = create_codex_async_openai()
This returns CodexAsyncOpenAI, a subclass of openai.AsyncOpenAI.
If you need the sync OpenAI client, use create_codex_openai(...).
Public API
from codex_auth_helper import (
CodexAsyncOpenAI,
CodexAuthConfig,
CodexAuthState,
CodexOpenAI,
CodexAuthStore,
CodexResponsesModel,
CodexTokenManager,
create_codex_async_openai,
create_codex_chat_openai,
create_codex_openai,
create_codex_responses_model,
)
Errors
Typical failure modes:
Codex auth file was not found ...The machine is not logged into Codex yet.Codex auth file ... does not contain valid JSONThe auth file is corrupt or partially written.ModelHTTPError ... Store must be set to falseMeans you are not using the helper-backed model instance.ModelHTTPError ... Stream must be set to trueMeans you are not usingCodexResponsesModel.
Package Notes
This package is intentionally small and focused:
- auth file parsing
- token refresh
- private, atomic auth state writes
- Codex-specific OpenAI client wiring
pydantic-airesponses model factory- LangChain Responses-model factory
Documentation
Release files for codex-auth-helper 1.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| codex_auth_helper-1.7.0.tar.gz | 15.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| codex_auth_helper-1.7.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.7 kB
Release files / codex_auth_helper-1.7.0.tar.gz
| Download URL | codex_auth_helper-1.7.0.tar.gz |
|---|---|
| Size | 15.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7c7b820d1dd1e8f883fa6b96fcc846329df7bc9bffc42a044d3931b0b85aa7f8
|
|
BLAKE2b-256 checksum How to use checksums |
7147081d0eaa1a2975d7e3878b71f5400d935a11bc08afd474582c9978f7cbd4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / codex_auth_helper-1.7.0-py3-none-any.whl
| Download URL | codex_auth_helper-1.7.0-py3-none-any.whl |
|---|---|
| Size | 17.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6c2a6572dbb9cfa01b97d93f3f90defca7fef35c92b265bd6640e7af3720ad64
|
|
BLAKE2b-256 checksum How to use checksums |
4a87815c8d228af5af7a1a43ed743ae25933081409a31b0f33e7eb8d7583812f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|