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
This SDK launches codex app-server via the Codex CLI, so make sure codex is installed and
available on your PATH.
pip install codex-agent-sdk
Using uv:
uv add codex-agent-sdk
Or, install into the current environment:
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
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_agent_sdk-0.0.2.tar.gz.
File metadata
- Download URL: codex_agent_sdk-0.0.2.tar.gz
- Upload date:
- Size: 26.3 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4a9c65f543c1c15aa20090e812e4b894b2e2ff5957d708ec7c3e8b9a9df8745c
|
|
| MD5 |
538e04a881bab4df25e224d5181848f7
|
|
| BLAKE2b-256 |
fe5f2f7cf0467ca359f792dce00a45041373b81784dd3b9ac205aa0bf21f7119
|
File details
Details for the file codex_agent_sdk-0.0.2-py3-none-any.whl.
File metadata
- Download URL: codex_agent_sdk-0.0.2-py3-none-any.whl
- Upload date:
- Size: 24.8 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3616b4036df5ed79825cc54fbcf3530bd9848f670360ab8a205a3eda75bb60ee
|
|
| MD5 |
90ea678547cfc8200514329e69d88ad8
|
|
| BLAKE2b-256 |
ac74251570251ed16ac5585d98b5c2fd4054ddc4fdb017d28cc3c18da0f73868
|