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

Uploaded CPython 3.12+Windows ARM64

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

Uploaded CPython 3.12+Windows x86-64

codex_python-1.131.0-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.0-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.0-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.0-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.0-cp312-abi3-macosx_11_0_arm64.whl (82.5 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

codex_python-1.131.0-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.0.tar.gz.

File metadata

  • Download URL: codex_python-1.131.0.tar.gz
  • Upload date:
  • Size: 88.6 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.0.tar.gz
Algorithm Hash digest
SHA256 d426599f8a451b9e59f1031f08df6365d57786417f14e8719f1943b059850226
MD5 36c224ded8fe2592f4ba6cd62c4e38f6
BLAKE2b-256 1fa1dadba63bb2cfd05a5048a9c1700f4c1689af411b8d33383d08cfecd7a681

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 e20971a308d7facccec59e7a0da08621aff230530fbe58672eb6e8913f5ba9fc
MD5 c6c102341efe74636bbf37db052ee6c0
BLAKE2b-256 f57553fd8f6a58859759a812ede221de0538dba60fa1720810064a940b479e7e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 63607a6afe69812b528ef461553654de0e8fc6a3e79d9311b3024a53cfd722e1
MD5 1639b0c7a98d66a0cd5b3b7842d9652c
BLAKE2b-256 801f71c8e399fb47826edbe2aeff21adccfea3d5d67136c049453f662c9a39fe

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0d89592438dbbcf55e10fe316c4dd7983ef6282bde5c3782fca6e611b5ae7194
MD5 59144999426bc0141ff5535c067553b4
BLAKE2b-256 67c0a1c0d3c0735623d33ecfc69cc6e9c7e1c49a443ee8d377f9fe598b40212c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 5223882819beefda070095dcffb0e044f7da1a286a85e3d6ac0c4f2b29860891
MD5 8b9476b2dbe5b134fb52838f4978a7ac
BLAKE2b-256 595735186c1546e4ef3ec6b0e5ea0c28c710f7db1ff72a82464178bd2d299123

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bc0341972b4974cdfc80f7b2c8ccadb74de7c520822a72f585172fdb7e360c74
MD5 4fe6137fdf56c8ffd30202c87c37c8b7
BLAKE2b-256 422a1fbc1773dbbbcd81ff21b6d8eeb6cf08217b4103d06491a694a084b721e0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 be932de37ae9cbb4201248de3862773c37a50412751b10833355f17a9a12dc5a
MD5 57536c836bb1ef231fcf10d39ab1ea95
BLAKE2b-256 184f43b11d0b6ca63e891bd6b0511e0863f91f2613162039f3807802caf74bbd

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d829214e3f5a6e3bb8bf4752731800d9975d21ebf2d731b4a1dc9c0fde893280
MD5 ca378bdd029fa9539d60a8629744a8fc
BLAKE2b-256 4c62c8c0f31aef342335f9f6accfdd1066c764716e94efcc863b2af8b21a1f3a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for codex_python-1.131.0-cp312-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4b4b391afa85dfb04d909fd2cac6fc3b4359b0c5b00aefc26c14b0f9504a2a1f
MD5 9ba85a6b6a9cbb08f0d02a71f2e57d4d
BLAKE2b-256 1aca64f66878651b3122888229e6354cf230c72cc6ba1385e2e0851d71645e5a

See more details on using hashes here.

Provenance

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