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.131.1.tar.gz (89.1 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.131.1-cp312-abi3-win_arm64.whl (80.7 MB view details)

Uploaded CPython 3.12+Windows ARM64

codex_python-1.131.1-cp312-abi3-win_amd64.whl (87.3 MB view details)

Uploaded CPython 3.12+Windows x86-64

codex_python-1.131.1-cp312-abi3-musllinux_1_2_x86_64.whl (88.0 MB view details)

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

codex_python-1.131.1-cp312-abi3-musllinux_1_2_aarch64.whl (82.0 MB view details)

Uploaded CPython 3.12+musllinux: musl 1.2+ ARM64

codex_python-1.131.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (87.8 MB view details)

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

codex_python-1.131.1-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (81.8 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ ARM64

codex_python-1.131.1-cp312-abi3-macosx_11_0_arm64.whl (82.5 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

codex_python-1.131.1-cp312-abi3-macosx_10_12_x86_64.whl (91.8 MB view details)

Uploaded CPython 3.12+macOS 10.12+ x86-64

File details

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

File metadata

  • Download URL: codex_python-1.131.1.tar.gz
  • Upload date:
  • Size: 89.1 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.131.1.tar.gz
Algorithm Hash digest
SHA256 e2a4c270da6668cee7140bd78c1e354dc6edc7009a2bec51e4b677a35d5117c2
MD5 7489627ab9ebe49850ce13b43cdbe7c2
BLAKE2b-256 4c1dd8c39467983b9a8af4f3694d09f2778c25cdfa708db163225fadbf73ca6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1.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.131.1-cp312-abi3-win_arm64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 bfa3cfd2ba5d458961a6d557f8e9bb1323e83d5f2901379d0982f0bb59cdb132
MD5 d0dec5fce17f78cbae69f596169fbcb1
BLAKE2b-256 648ac77808822ec5760feec15d086cb0358a720b74775d54d019aaca98dcb61d

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 9c91e28cc839d3efb941539607ae2a13e315cff79cb5ed395c73d081816e0275
MD5 0a35c0c5147ec6901d41e610a543428e
BLAKE2b-256 b6cf8841c5b89d54e8f7d2b3a700d4a6deb044498e6e33c23bb5311b6f387d8c

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 2783544e0aa20a4fc58275332f7087f59475f00407cb4a04e947c3b39b926e5f
MD5 e6f5a40d936263dacc7ffb29142da326
BLAKE2b-256 4724f98c3dbe079223f7ac8986e9e1c116fb7f5226fc4f97f7ef82af0c38d4cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 72a803103550f9c93550b0ac7601295fa7d3f4d8be1c9a16886a77543565220c
MD5 f23f990381bfd5fccbd09d8d6c84dce3
BLAKE2b-256 3c6ecfe9870335e3b5f754f4a2ca7612da6de51fc37263a2f37f03d6c2656f67

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ba65944470b8f5f5d7d6c601caac5d16b4bdb045f3f2e21793833f55e2aa4ce3
MD5 e22f20f4384e9ff9910bde739c8d4436
BLAKE2b-256 c35494d4cf421f8b5827dd581ae2b75f3da17b435c06c4ca4ac6298c60010d78

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 314b8fdf1ef3da6492d805c3529e2c6a378d1c0533fcd272486d9097bdf28184
MD5 65cb4cd9021386534c349f27e2754f69
BLAKE2b-256 e74da21bea7434ff015fb1263695ce0f3c21dd91870bde011154fe8a5a815fc4

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ac29b0c678606986740d04c06d03f8fa4cbbdfe6d05b477c23d61403a7dd3ccd
MD5 2aacc051badb1110bce9de8d82c2d63c
BLAKE2b-256 8fd009b92c96ac525c8d01d1402b11d16b9249b6e3ee7f3c64868f8f39889d83

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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.131.1-cp312-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for codex_python-1.131.1-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f4577f92bbd90681cbab156cffedeeb3e764801cb8df4024b840030a8c8930a0
MD5 c0333033727a322309ab5a043cc6cbe6
BLAKE2b-256 598a38f70265f4cd373b58bc45c2575ee796fa7fb2aedfe03892f824ce3a51cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for codex_python-1.131.1-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