Drop-in OpenAI SDK patch for ChatGPT Codex OAuth authentication
Project description
codex-auth
Drop-in OAuth for the OpenAI Python SDK — use the ChatGPT Codex API with your Pro/Plus account instead of an API key. Obviously this is for personal usage users, not for production or so.
Install
pip install codex-auth
Usage
import codex_auth
from openai import OpenAI
client = OpenAI() # no API key needed
response = client.responses.create(
model="gpt-5.1-codex-mini",
input="Write a one-sentence bedtime story about a unicorn.",
)
print(response.output_text)
A browser window opens on first run for OAuth. Tokens are cached in
~/.codex-auth/auth.json and refreshed automatically.
Both streaming and non-streaming calls work — the library handles the Codex endpoint's streaming requirement transparently.
Streaming
stream = client.responses.create(
model="gpt-5.1-codex-mini",
input="Write a hello-world in Rust.",
stream=True,
)
for event in stream:
if event.type == "response.completed":
print(event.response.output_text)
chat.completions compatibility
Existing code using chat.completions works too — requests are converted
to the Responses API format automatically:
response = client.chat.completions.create(
model="gpt-5.1-codex-mini",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
Explicit client
If you prefer not to monkey-patch:
from codex_auth import CodexClient
client = CodexClient() # browser / device auth
client = CodexClient(token="…") # existing token
Async variant:
from codex_auth import AsyncCodexClient
client = AsyncCodexClient()
Disable auto-patch
export CODEX_AUTH_NO_PATCH=1
Or in code:
import codex_auth
codex_auth.init(auto_patch=False)
How it works
A custom httpx transport intercepts OpenAI SDK requests to:
- Rewrite URLs to the Codex backend (
chatgpt.com/backend-api/codex/responses) - Convert
chat.completionspayloads to the Responses API format - Buffer SSE responses for non-streaming callers
- Inject OAuth bearer tokens and refresh them transparently
Browser-based PKCE auth is used on desktop; device-code flow on headless/SSH.
Token storage
~/.codex-auth/auth.json (mode 0600). Override with CODEX_AUTH_TOKEN env var.
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 codex_auth-0.1.1.tar.gz.
File metadata
- Download URL: codex_auth-0.1.1.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87cf2031569b26c0819ee2bc6320562fee15eac197b3c5871fe1ee7f24f4140d
|
|
| MD5 |
73f3d43c70d6b48ebf7d4500c31d1e74
|
|
| BLAKE2b-256 |
1999b7b2230b21ea2402d60b54bd116a88cc5d5a70d436187c2088f116330152
|
File details
Details for the file codex_auth-0.1.1-py3-none-any.whl.
File metadata
- Download URL: codex_auth-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a90708b60fc9388479d39248f8aa97fc7cd53c22fc79a2b17b598901a97d003
|
|
| MD5 |
75220fc39add610b49ca69024f4a673d
|
|
| BLAKE2b-256 |
66184e50a18c247fa447b47cf971c1946e0101e4f6d285d638d189e067eab995
|