Skip to main content

Python SDK for Codex app-server

Project description

Codex Agent SDK for Python

Python SDK for Codex app-server. This SDK wraps the codex app-server CLI via a subprocess and speaks JSON-RPC over stdio (newline-delimited JSON).

Installation

pip install codex-agent-sdk

Using uv:

uv pip install codex-agent-sdk

Quick Start

The simplest way to use the SDK:

import anyio
from codex_agent_sdk import run

async def main() -> None:
    async for chunk in run("Hello Codex!"):
        print(chunk, end="", flush=True)

anyio.run(main)

Using the Agent Class

import anyio
from codex_agent_sdk import Agent

async def main() -> None:
    agent = Agent(model="gpt-5.2-codex", auto_approve=True)

    # Stream text
    async for chunk in agent.run("Explain Python decorators"):
        print(chunk, end="", flush=True)

anyio.run(main)

Get Full Response

# Get full text
text = await agent.run("What is 2+2?").text()
print(text)

# Get full response with metadata
response = await agent.run("Hello").response()
print(response.text)
print(response.turn.id)

Builder Pattern

Configure agents with a fluent API:

agent = (
    Agent()
    .model("gpt-5.2-codex")
    .cwd("/path/to/project")
    .effort("medium")
    .auto_approve_commands()
    .auto_approve_file_changes()
)

Decorator-Based Handlers

Register approval handlers using decorators:

from codex_agent_sdk import Agent, CommandApproval, FileChangeApproval

agent = Agent()

@agent.on_command_approval
async def handle_command(cmd: CommandApproval) -> str:
    print(f"Approving: {cmd.command}")
    return "accept"  # or "reject"

@agent.on_file_change
async def handle_file_change(change: FileChangeApproval) -> str:
    print(f"File change: {change.path}")
    return "accept"

async for chunk in agent.run("List files in current directory"):
    print(chunk, end="")

Multi-Turn Conversations

Maintain conversation context across multiple messages:

async with agent.conversation() as conv:
    response = await conv.send("Hi, my name is Alice")
    print(response.text)

    response = await conv.send("What's my name?")
    print(response.text)  # Agent remembers "Alice"

Typed Events

Access rich event objects with pattern matching:

from codex_agent_sdk import (
    Agent,
    MessageDelta,
    TurnCompleted,
    ExecStarted,
    ExecCompleted,
)

async for event in agent.run("Run ls -la").events():
    match event:
        case MessageDelta(delta=text):
            print(text, end="")
        case ExecStarted(command=cmd):
            print(f"\n> Running: {cmd}")
        case ExecCompleted(exit_code=code):
            print(f"\n> Exit code: {code}")
        case TurnCompleted(status=status):
            print(f"\nCompleted: {status}")

Real-World Example

Analyze a local project:

uv run python examples/real_world_project_analysis.py --project /path/to/project

Or set environment variables:

CODEX_PROJECT=/path/to/project \
CODEX_MODEL=gpt-5.2-codex \
CODEX_EFFORT=medium \
uv run python examples/real_world_project_analysis.py

Schema Validation

Optionally validate messages against JSON schemas:

pip install codex-agent-sdk[schema]

CLI tools for schema management:

# Validate SDK <-> installed Codex CLI compatibility
codex-agent-sdk validate

# Generate schema to a directory
codex-agent-sdk schema generate --out ./codex-schema

# Detect breaking schema changes
codex-agent-sdk schema diff --baseline ./codex-schema
codex-agent-sdk schema check-breaking --baseline ./codex-schema

Logging

Enable debug logs:

import logging

logging.getLogger("codex_agent_sdk").setLevel(logging.DEBUG)
logging.basicConfig(level=logging.INFO)

Development

# Create dev environment
uv sync --all-extras

# Run tests
uv run pytest -q

# Run integration tests (requires Codex CLI auth)
CODEX_INTEGRATION=1 uv run pytest -q

Contributing

See docs/CONTRIBUTING.md.

License

MIT

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_agent_sdk-0.0.1.tar.gz (25.9 kB view details)

Uploaded Source

Built Distribution

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

codex_agent_sdk-0.0.1-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file codex_agent_sdk-0.0.1.tar.gz.

File metadata

  • Download URL: codex_agent_sdk-0.0.1.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for codex_agent_sdk-0.0.1.tar.gz
Algorithm Hash digest
SHA256 8a758b599e35b2398cc1abc4b255c2518f7c76650df9138076819dee6f65c0eb
MD5 d58ab76f619f989a7fc38a5f4430112d
BLAKE2b-256 abefe1719f28ff5e92f13c0ac32b5ebc772d18eed50fd4b285a96d9d2dcd68fc

See more details on using hashes here.

File details

Details for the file codex_agent_sdk-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: codex_agent_sdk-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.29 {"installer":{"name":"uv","version":"0.9.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for codex_agent_sdk-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b543173eacd65fcf366fbbe28072c0de7c06318378e1b81e717e9c4b44083d5e
MD5 a9adfeb95aa57273891f915c8fa3d8f2
BLAKE2b-256 86f36e6327800438a5141bee60ceb2bce282f6c315cbc503bee78d31a3b15a6d

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