Skip to main content

One async Python API for Claude Code, Codex, and ACP coding agents

Project description

unio for Python

Async Python SDK for using Claude Code, Codex, and ACP-native coding agents through one API. Python 3.11 or newer is required.

The Python SDK follows the shared unio behavior specification. Its package and release versions are independent from the Go SDK.

Install

Python 3.11+ is supported. After the first python-v0.1.0 release, install the SDK and separately install and authenticate the CLI you intend to drive:

python -m pip install unio-py
codex --version  # or claude/kimi/opencode --version

Runtime discovery matches the support matrix. Creating an Agent checks that the executable exists; authentication and provider errors can surface on the first operation.

The PyPI distribution is named unio-py; the import package remains unio.

For development from this repository:

cd python
python -m venv .venv
. .venv/bin/activate
python -m pip install -e '.[dev]'

Basic usage

import asyncio

import unio


async def main() -> None:
    async with unio.Agent(unio.Codex, cwd="/path/to/repo") as agent:
        session = agent.new_session()
        result = await session.run(unio.UserMessage("Explain this repository"))
        print(result.text)


asyncio.run(main())

Agent owns its child process and sessions, so prefer async with or call await agent.close().

Stream output

async with unio.Agent(unio.Claude, cwd=".") as agent:
    session = agent.new_session()
    stream = await session.stream(unio.UserMessage("Review this repository"))
    async for event in stream:
        if event.kind in {unio.EventKind.TEXT, unio.EventKind.THINKING}:
            print(event.text, end="")
        elif event.kind is unio.EventKind.TOOL_CALL:
            print(f"tool={event.tool} input={event.tool_input!r}")
    result = await stream.result()

Always fully consume a manual stream or call await stream.result(). A Session remains running until its terminal event is consumed.

Approvals and interruption

result = await session.run(unio.UserMessage("Apply the change"))
while result.blocked is not None:
    if result.blocked.options:
        for option in result.blocked.options:
            print(f"{option.value}: {option.label}")
        selected = await asyncio.to_thread(input, "Choose an option value: ")
        user_input = unio.OptionSelection(selected)
    else:
        reply = await asyncio.to_thread(input, f"{result.blocked.message}\nReply: ")
        user_input = unio.UserMessage(reply)
    result = await session.run(user_input)

run and stream accept the same UserInput union. On an idle Session, UserMessage starts a turn. On a blocked Session, use OptionSelection for an advertised option or UserMessage when the runtime requests free-form input. Use await session.interrupt() to stop a running or blocked turn. Confirmed interruption sets result.interrupted; an idle interrupt is a no-op.

Only one turn may run on a Session at a time; separate Sessions owned by one Agent may run concurrently. Cancelling a task that is consuming a Stream asks the runtime to interrupt that turn. Agent.close() closes every Session and can raise an ExceptionGroup if one or more Session cleanups fail. Consume stream events promptly: the internal queue is bounded and may drop intermediate events for a slow consumer while preserving terminal events.

Resume and inspect sessions

items = await agent.list_sessions(limit=20)
if not items:
    raise RuntimeError("No persisted sessions")
session = await agent.get_session(items[0].id)
result = await session.run(unio.UserMessage("Continue the previous work"))

raw = await session.raw()
statistics = await session.token_statistics()

Listing defaults to the Agent working directory. Pass all_workspaces=True to remove that filter. Raw data can contain prompts, code, commands, outputs, paths, and credentials; review and redact it before logging or transmitting it.

Configuration

agent = unio.Agent(
    unio.OpenCode,
    cwd="/path/to/repo",
    model="provider/model",
    system_prompt="Keep changes minimal.",
    extra_args=("--some-runtime-flag",),
    env={"RUNTIME_SETTING": "value"},
)

cwd selects a working directory; it is not a sandbox. Codex app-server arguments are fixed, so extra_args does not affect Codex. The child CLI inherits the current OS user's file, process, environment, and network access; approval behavior varies by runtime. env overrides inherited variables with the same name. Model and system-prompt validation normally occurs when the runtime starts. An invalid Agent kind raises ValueError; runtime failures use AgentError. Use a least-privilege environment for untrusted repositories and see the security policy.

Errors and support

Catch unio.AgentError and branch on error.kind, not message text. See the shared error guide and runtime support matrix. The SDK is fully typed and ships py.typed.

Runnable examples are in python/examples/. For local quality gates and release steps, see the contribution guide.

Authenticated Codex, TraeX, and OpenCode E2E tests are opt-in because they can consume tokens:

UNIO_RUN_REAL_E2E=1 pytest -s tests/e2e_real

Run one runtime directly with:

UNIO_RUN_REAL_E2E=1 pytest -s tests/e2e_real/test_codex.py
UNIO_RUN_REAL_E2E=1 pytest -s tests/e2e_real/test_acp.py -k traex
UNIO_RUN_REAL_E2E=1 pytest -s tests/e2e_real/test_acp.py -k opencode

The OpenCode test defaults to deepseek/deepseek-v4-flash. Override it with UNIO_E2E_OPENCODE_MODEL=provider/model when another authenticated model is available locally.

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

unio_py-0.1.0.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

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

unio_py-0.1.0-py3-none-any.whl (32.7 kB view details)

Uploaded Python 3

File details

Details for the file unio_py-0.1.0.tar.gz.

File metadata

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

File hashes

Hashes for unio_py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f87eb93f93be410a31b36cd1405ac468f91378c089fd638e2e2eb0681dac4398
MD5 6a4d45fd03adb51364e781e26c6cac02
BLAKE2b-256 fe935e2af04aec3664712dfd4ad5eeccc40d697c841c2d4dcd2d54565971713e

See more details on using hashes here.

Provenance

The following attestation bundles were made for unio_py-0.1.0.tar.gz:

Publisher: python-release.yml on Fullstop000/unio

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

File details

Details for the file unio_py-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: unio_py-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 32.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for unio_py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ddcd4487ec13a3cf128f57d5383b861dabea19b75e6ad11e0ede123935399348
MD5 e46cef323e4e0fa0951bcb5e9f20278f
BLAKE2b-256 b8f6aff5a389da72022eb2552954dc7643a55d7497ab704230701b18145c3714

See more details on using hashes here.

Provenance

The following attestation bundles were made for unio_py-0.1.0-py3-none-any.whl:

Publisher: python-release.yml on Fullstop000/unio

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