Skip to main content

Python SDK for Codex CLI with bundled platform binaries

Project description

codex-python

Python SDK for Codex with bundled codex binaries inside platform wheels.

This package exposes two supported APIs:

  • Codex: a simple, local convenience interface backed by a private stdio app-server session
  • AppServerClient: a richer app-server client for thread management, streaming events, approvals, and typed protocol access

Canonical import paths:

  • use from codex import ... for the high-level Codex facade
  • use from codex.app_server import ... for the raw app-server client and app-server option types

Install

pip install codex-python

Which API should I use?

Codex

Use Codex when you want the smallest surface area for local automation:

  • one private local app-server session per Codex instance
  • stateless run*() convenience (fresh internal thread per call)
  • stateful thread workflows when needed via start_thread() / resume_thread()
  • simple request/response usage
  • optional streaming over the exec event stream
  • structured output via TurnOptions(output_schema=...)

AppServerClient

Use AppServerClient when you want a deeper integration:

  • persistent app-server connection
  • thread objects and turn streams
  • protocol-native notifications
  • server-driven requests such as tool callbacks and approvals
  • typed protocol models and raw JSON-RPC access when needed

Quickstart: Codex

from codex import Codex, ThreadStartOptions

client = Codex()

# Simplest one-shot call.
summary = client.run_text("Diagnose the failing tests and propose a fix")
print(summary)

# One-shot call with thread-scoped defaults for that run's fresh internal thread.
summary = client.run_text(
    "Diagnose the failing tests in this repo",
    thread_options=ThreadStartOptions(
        cwd="/repo",
        model="gpt-5",
    ),
)
print(summary)

Use thread_options= on run(), run_text(), run_json(), and run_model() when you want to set defaults on the fresh internal thread created for that one-shot call. Use start_thread() / resume_thread() when later runs should share context.

More Codex examples: docs/exec_api.md

Quickstart: AppServerClient

from codex.app_server import AppServerClient, AppServerClientInfo, AppServerInitializeOptions

initialize_options = AppServerInitializeOptions(
    client_info=AppServerClientInfo(
        name="my_integration",
        title="My Integration",
        version="0.1.0",
    )
)

with AppServerClient.connect_stdio(initialize_options=initialize_options) as client:
    thread = client.start_thread()
    summary = thread.run_text("Briefly summarize this repository's purpose.")
    print(summary)

More app-server examples: docs/app_server.md For websocket transport, install the optional extra: pip install "codex-python[websocket]".

Dynamic tools

Decorator-driven dynamic tools are available on both SDK surfaces.

Codex

from codex import Codex, dynamic_tool


@dynamic_tool
def lookup_ticket(id: str) -> str:
    """Look up a support ticket by id."""
    return f"Ticket {id}: Login requests time out in eu-west-1."


client = Codex()
summary = client.run_text(
    "Use the lookup_ticket dynamic tool for ticket 123 and summarize the result.",
    tools=[lookup_ticket],
)
print(summary)

AppServerClient

For a complete app-server example using the same decorator-driven flow, see examples/app_server_dynamic_tool.py.

Structured output

Codex

from codex import Codex, TurnOptions

schema = {
    "type": "object",
    "properties": {"summary": {"type": "string"}},
    "required": ["summary"],
    "additionalProperties": False,
}

client = Codex()
payload = client.run_json("Summarize repository status", TurnOptions(output_schema=schema))
print(payload["summary"])

AppServerClient

from pydantic import BaseModel

from codex.app_server import AppServerClient, AppServerTurnOptions


class Summary(BaseModel):
    summary: str


with AppServerClient.connect_stdio() as client:
    thread = client.start_thread()
    result = thread.run_model(
        "Summarize repository status",
        Summary,
    )
    print(result.summary)

run_model() uses Summary both as the validation model and, by default, as the output schema sent to Codex. If you want JSON back without validation, you can also pass the model class directly to output_schema, for example thread.run_json(..., AppServerTurnOptions(output_schema=Summary)).

Streaming

Codex stream

from codex import Codex
from codex.protocol import types as protocol

client = Codex()
stream = client.run("Investigate this bug")
for event in stream:
    if isinstance(event, protocol.ItemAgentMessageDeltaNotification):
        print(event.params.delta, end="", flush=True)

print()

Codex.run*() starts a fresh internal thread for each call. Use start_thread() or resume_thread() when you want later runs to share context.

High-level Codex helpers raise ThreadRunError on failed or interrupted terminal turns and preserve the final turn metadata on the exception for debugging and UI handling.

App-server stream

from codex.app_server import AppServerClient
from codex.protocol import types as protocol

with AppServerClient.connect_stdio() as client:
    thread = client.start_thread()
    stream = thread.run("Investigate this bug")

    for event in stream:
        if isinstance(event, protocol.ItemAgentMessageDeltaNotification):
            print(event.params.delta, end="", flush=True)

    print()

Advanced app-server usage, including typed stable RPC domains such as client.models and the raw client.rpc fallback: docs/app_server_advanced.md

Examples

Bundled binary behavior

By default, the SDK resolves the bundled binary at:

codex/vendor/<target-triple>/codex/{codex|codex.exe}

If the bundled binary is not present, for example in a source checkout, the SDK falls back to codex on PATH.

You can override the executable path with:

  • CodexOptions(codex_path_override=...)
  • codex.app_server.AppServerProcessOptions(codex_path_override=...)

Development

make lint
make test

make test emits a terminal coverage report, writes coverage.xml, and enforces the repository coverage gate.

If you want to test vendored-binary behavior locally, fetch binaries into codex/vendor:

python scripts/fetch_codex_binary.py --target-triple x86_64-unknown-linux-musl

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_python-1.140.0.tar.gz (94.4 kB view details)

Uploaded Source

Built Distributions

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

codex_python-1.140.0-cp312-abi3-win_arm64.whl (100.2 MB view details)

Uploaded CPython 3.12+Windows ARM64

codex_python-1.140.0-cp312-abi3-win_amd64.whl (87.0 MB view details)

Uploaded CPython 3.12+Windows x86-64

codex_python-1.140.0-cp312-abi3-musllinux_1_2_x86_64.whl (102.6 MB view details)

Uploaded CPython 3.12+musllinux: musl 1.2+ x86-64

codex_python-1.140.0-cp312-abi3-musllinux_1_2_aarch64.whl (96.1 MB view details)

Uploaded CPython 3.12+musllinux: musl 1.2+ ARM64

codex_python-1.140.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (102.4 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

codex_python-1.140.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (95.9 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

codex_python-1.140.0-cp312-abi3-macosx_11_0_arm64.whl (92.7 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

codex_python-1.140.0-cp312-abi3-macosx_10_12_x86_64.whl (99.8 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

Details for the file codex_python-1.140.0.tar.gz.

File metadata

  • Download URL: codex_python-1.140.0.tar.gz
  • Upload date:
  • Size: 94.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for codex_python-1.140.0.tar.gz
Algorithm Hash digest
SHA256 2f04cab3caec57f2d8e341dae4660aa58df46b1c11d5a888c7b04bf41a988d3c
MD5 64021ee4c8b21871d2b51917f57c66b6
BLAKE2b-256 292dbd3a6e368ce5976ad267f720dc90528d34c6b2f31c5ec54f62864d0440ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0.tar.gz:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 989571f7117d51f971baa7689db1e3e59e0846d50c7289b3ee448617560d5d0a
MD5 229062b71c2ef043a72a2409944a3225
BLAKE2b-256 e91b1d0913cace1bc54cc92ca7c4d04077065808587cc1cac265cad49a70d460

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-win_arm64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 0fae7921afa466c767d58546920e1f26ad7d0c258a57ed9530019572d4237bc2
MD5 c2458683cefda5575b4e686aeefc721b
BLAKE2b-256 2eff78748d45219a95dfa1189481fe955e37b015d36f1b046eba07f52b0eb35e

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-win_amd64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2d61f2ff30868bbddd0f367e001c668082346662e7b857c43c64829864e11b8a
MD5 dbbff69b2c37af45928ef96c5a034d37
BLAKE2b-256 c4aa788231d9ed2540bd86539f0d9c31d05115cd787e26681788698bb4a94610

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-musllinux_1_2_x86_64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 8496a2848a4dca0e1210f2d065ae2ab23900b14919b1a74c01f25123d0aeade6
MD5 b8154ec09faa290c0e61fcd4c7921ae2
BLAKE2b-256 dd94ac25107f00b879cfacf4113d7aec157216c0f082f6b5c63d6765ad0ca3a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-musllinux_1_2_aarch64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a5401509c420c065e86f0cf0bbf6f8cf4331d2723fa42a1af5e48814a4dbc7d3
MD5 1a7935b780e12e9085ac2ab82c99421d
BLAKE2b-256 8af85493e8b19b1cc69d3025446ae2297d929fe35714b9ae2980f332c262a126

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 cdecf800a35998786d3fdc4ed80a4cd50abdf2d06ba7865f0dc6d496b6035710
MD5 ce33b9ee493bf15eb01415ed7b5d392d
BLAKE2b-256 578f55b852457c917c6a457428725d84f636bc4f4d19cb1c0d5eb83bfb919aa5

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ed41dd94015a87b50dde8a3585b433216d7746a4fd57294b4751b9b1f10d699c
MD5 edfc33d621d7f143cf6c7c5bab3e6bfc
BLAKE2b-256 aa09a29144f43d2c7434261b07e46ea3549b4d7fd4a3313b96d453b8c1796457

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file codex_python-1.140.0-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for codex_python-1.140.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 2d98c34541e5113410b3e38cef232c31c4da582c7f6602eec7ec19fdc4f97d1a
MD5 b3bc79f45762fc898e1926808013b6e6
BLAKE2b-256 46ef37cda1ac27c21b12eb16cfaa50fb202b1489c8956286174a43fa645a5b06

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.140.0-cp312-abi3-macosx_10_12_x86_64.whl:

Publisher: release-published.yml on gersmann/codex-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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