Skip to main content

openai-codex-auth

A standalone Python client for the ChatGPT Codex backend. It reads the Codex CLI's ChatGPT login, refreshes credentials, sends HTTP or WebSocket requests, and reconstructs completed Responses output from streaming events.

The core has no DSPy or LiteLLM dependency. Its runtime dependencies are Requests for credential refresh, HTTPX for HTTP, and WebSockets for WebSocket transport. dspy-codex-auth imports this client to provide an optional DSPy adapter.

Install and login

uv add openai-codex-auth
codex login

Choose "Sign in with ChatGPT". This package requires a file-backed login at ~/.codex/auth.json, or an explicit auth-file path. It does not read the OS credential store. See Codex authentication. No Pi installation or plugin is needed.

Synchronous and asynchronous calls

from openai_codex_auth import CodexClient

client = CodexClient()
response = client.create(
    model="gpt-5.5",
    input="Explain what a Python generator does in one sentence.",
    reasoning={"effort": "low", "summary": "auto"},
)
print(response.output_text)
print(response.usage)

Use the same client with await client.acreate(...) in asynchronous code:

response = await client.acreate(
    model="gpt-5.5",
    input="What is an async iterator?",
    transport="http",
)

Use bare model IDs such as gpt-5.5. input accepts a string, encoded as a user message, or a list of native Responses input items. Pass instructions through instructions= and provider fields such as reasoning, text, and tools directly. The client returns function calls; your application executes tools and supplies results as input for subsequent calls. It does not provide a tool-execution loop.

CodexResponse exposes output_text, output, model, usage, and the actual transport used. Its response dictionary preserves provider metadata and the reconstructed output, including reasoning summaries and function calls. Both methods consume the stream and return the completed response.

Transport and request behavior

response = client.create(
    model="gpt-5.5",
    input="Hello",
    transport="auto",
    timeout=300.0,
    connect_timeout=10.0,
    idle_timeout=300.0,
    max_retries=3,
)
  • transport="auto" tries HTTP and switches to WebSocket only for the exact structured HTTP 404 model-not-found error for the requested model. "http" and "websocket" select a single transport.
  • HTTP timeout defaults to 300 seconds. WebSocket connection timeout defaults to 10 seconds and its per-event idle timeout to 300 seconds. HTTP also accepts an httpx.Timeout for phase-specific deadlines, including explicit None values to disable particular HTTP deadlines.
  • max_retries=3 allows three additional attempts across both transports for recoverable network failures, HTTP/backend 429/5xx responses, and completions without meaningful output. Auto selection may additionally make an HTTP routing probe before switching to WebSocket. Set max_retries=0 to disable retries. Delays start at 0.5 seconds and double up to 8 seconds. Malformed or unfinished streams and other backend failures raise errors.
  • Empty output and reasoning-only output without an answer are retried; refusals and other native output items are returned to the caller.
  • The backend requires stream=True and store=False; the client enforces both. It removes max_output_tokens, which this backend rejects, and maps service_tier="fast" to "priority". There is no client output-token cap.
  • The client reconstructs missing output from streamed text, reasoning, and tool-call events. A stream ending before completion raises an error.
  • usage is an empty dictionary when the provider omits usage; it does not imply zero tokens were used.

Native provider input and options are distinct from DSPy's chat-message and LM keyword interface. Use the DSPy adapter for codex/... model names, messages, response_format, and reasoning_effort.

Credentials and custom clients

client = CodexClient(auth="/path/to/codex-auth.json", user_agent="my-app/1.0")

Credentials are read for each request and refreshed when necessary. Refreshes take an exclusive file lock. Missing or non-ChatGPT credentials raise CodexAuthError with login instructions. CodexClient also accepts an explicit bearer credential through api_key= and its account through account_id=; these are Codex subscription credentials.

Existing auth-only helpers remain available:

from openai_codex_auth import CodexAuth, codex_headers, getauthtoken

auth = CodexAuth()
token = auth.token()
headers = codex_headers(token, account_id=auth.account_id())

HTTP requests use the core's originator by default; WebSocket handshakes use codex_cli_rs. Both use the configured User-Agent. Bearer credentials stay in authentication headers, outside WebSocket request frames.

Set authentication, originator, and User-Agent through their explicit client options. Conflicting entries in headers= are ignored in favor of those options and the required protocol headers. HTTP errors expose their status and credential-redacted provider payload through CodexHTTPError.status_code and CodexHTTPError.body.

Development and release

uv sync --locked --dev
uv run pytest
uv run ruff check .
uv build --no-sources

Tests use synthetic credentials and mocked transports. See RELEASING.md for publishing instructions and THIRD_PARTY_NOTICES.md for attribution.

Metadata

Release files for openai-codex-auth 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openai-codex-auth 0.2.0
File Size Uploaded Provenance Actions
openai_codex_auth-0.2.0.tar.gz 17.1 kB Without provenance Details for openai_codex_auth-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openai-codex-auth 0.2.0
File Interpreter ABI Platform Actions
openai_codex_auth-0.2.0-py3-none-any.whl Python 3 none any Details for openai_codex_auth-0.2.0-py3-none-any.whl

Total release size: 38.5 kB

Release files / openai_codex_auth-0.2.0.tar.gz

Download URL openai_codex_auth-0.2.0.tar.gz
Size 17.1 kB
Tags Source
SHA-256 checksum
How to use checksums
1745fc619d94b1c35cc463dbb7e16b442b3960b334e8634c7e1fdb1b3c5b77bc
BLAKE2b-256 checksum
How to use checksums
d306cea1be1a7cfbe4456ae4bec2fba64cd60e94c0302a37b18e72766a9f5efc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Release files / openai_codex_auth-0.2.0-py3-none-any.whl

Download URL openai_codex_auth-0.2.0-py3-none-any.whl
Size 21.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c35c950a0fd542cbdd08db2597d98ca16a53da0e57bd4279972e99d45707d25f
BLAKE2b-256 checksum
How to use checksums
1a8b79308de7f32da433752f3af7446e7cbc2e99b39f2792db37bbcff897133e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.0

Security for openai-codex-auth 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page