Skip to main content

Codex auth helpers for pydantic-ai OpenAI Responses models.

Project description

codex-auth-helper

codex-auth-helper turns an existing local Codex auth session into a pydantic-ai model.

It reads ~/.codex/auth.json, refreshes access tokens when needed, builds a custom AsyncOpenAI client for the Codex Responses endpoint, and returns a ready-to-use CodexResponsesModel.

What It Does

  • Reads tokens from ~/.codex/auth.json
  • Derives ChatGPT-Account-Id from 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
  • Builds an OpenAI-compatible client pointed at https://chatgpt.com/backend-api/codex
  • Returns a pydantic-ai responses model that already applies the Codex backend requirements

The helper enforces two backend-specific behaviors for you:

  • openai_store=False
  • streamed responses even when pydantic-ai calls the non-streamed request() path

What It Does Not Do

  • It does not log you into Codex
  • It does not create ~/.codex/auth.json
  • It does not support pydantic_ai.models.openai.OpenAIChatModel or Chat Completions flows
  • It does not replace pydantic-ai; it only provides a model/client factory

Install

uv pip install codex-auth-helper

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")
agent = Agent(model, instructions="You are a helpful coding assistant.")

result = agent.run_sync("Naber")
print(result.output)

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", config=config)

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",
    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.

Public API

from codex_auth_helper import (
    CodexAsyncOpenAI,
    CodexAuthConfig,
    CodexAuthState,
    CodexAuthStore,
    CodexResponsesModel,
    CodexTokenManager,
    create_codex_async_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 JSON The auth file is corrupt or partially written.
  • ModelHTTPError ... Store must be set to false Means you are not using the helper-backed model instance.
  • ModelHTTPError ... Stream must be set to true Means you are not using CodexResponsesModel.

Package Notes

This package is intentionally small and focused:

  • auth file parsing
  • token refresh
  • Codex-specific OpenAI client wiring
  • pydantic-ai responses model factory

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codex_auth_helper-0.4.1.tar.gz (8.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codex_auth_helper-0.4.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file codex_auth_helper-0.4.1.tar.gz.

File metadata

  • Download URL: codex_auth_helper-0.4.1.tar.gz
  • Upload date:
  • Size: 8.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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}

File hashes

Hashes for codex_auth_helper-0.4.1.tar.gz
Algorithm Hash digest
SHA256 6c86a454202ee929aaa8135f73963c59e4a4a04ccea800e3df4b82f5e8690b5a
MD5 5cfebac2311aed641902c662898af996
BLAKE2b-256 442f873755a485abfce6c3cbbf91548ffb3e10feab923a12e9a70b02aafd61d5

See more details on using hashes here.

File details

Details for the file codex_auth_helper-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: codex_auth_helper-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","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}

File hashes

Hashes for codex_auth_helper-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ffede001a01fb58295fd4f5f2de1890221fba29567a06433a9c8207c1c9fa1b3
MD5 bbf42aec5509d17c478f3d118fa646e4
BLAKE2b-256 1fc631e474b886e4cffe35dbf4c64e37741ba5ed54e1a1eef6e10e56d2320541

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page