|
| 1 | +# @opencode-ai/sdk (Python) |
| 2 | + |
| 3 | +Python SDK for the [opencode](https://opencode.ai) HTTP API. Mirrors the |
| 4 | +JavaScript SDK in `packages/sdk/js`. The client is generated from the same |
| 5 | +OpenAPI document used by the JS SDK (`packages/sdk/openapi.json`) so the two |
| 6 | +stay in lockstep. |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +```bash |
| 11 | +pip install opencode-ai |
| 12 | +``` |
| 13 | + |
| 14 | +Requires Python 3.10+. The runtime depends only on `httpx`, `attrs`, and |
| 15 | +`python-dateutil`. |
| 16 | + |
| 17 | +## Quick start |
| 18 | + |
| 19 | +```python |
| 20 | +from opencode_ai import Client, ServerOptions, create_opencode_server |
| 21 | +from opencode_ai.api.default import global_health, session_create, session_prompt |
| 22 | + |
| 23 | +# Spawn a local opencode server (or skip this and connect to an existing one). |
| 24 | +with create_opencode_server(ServerOptions(port=4096)) as server: |
| 25 | + client = Client(base_url=server.url) |
| 26 | + |
| 27 | + health = global_health.sync(client=client) |
| 28 | + print(health.version) |
| 29 | + |
| 30 | + session = session_create.sync(client=client).id |
| 31 | + session_prompt.sync(client=client, id=session, body=...) |
| 32 | +``` |
| 33 | + |
| 34 | +The generated client follows the standard |
| 35 | +[`openapi-python-client`](https://github.com/openapi-generators/openapi-python-client) |
| 36 | +convention: every operation lives in its own module under |
| 37 | +`opencode_ai.api.<tag>` (most operations live under `default`) and exposes |
| 38 | +four entry points: |
| 39 | + |
| 40 | +| function | result | |
| 41 | +| ----------------- | ------------------------------------- | |
| 42 | +| `sync` | parsed model on success, `None` on 4xx with `raise_on_unexpected_status=False` | |
| 43 | +| `sync_detailed` | full `Response[...]` with status, headers, raw bytes | |
| 44 | +| `asyncio` | coroutine variant of `sync` | |
| 45 | +| `asyncio_detailed`| coroutine variant of `sync_detailed` | |
| 46 | + |
| 47 | +## Server helper |
| 48 | + |
| 49 | +`create_opencode_server` spawns the local `opencode serve` process, blocks |
| 50 | +until it prints its listen URL, and returns a context manager that shuts the |
| 51 | +process down cleanly on exit. The async sibling |
| 52 | +`AsyncServerHandle.create()` provides the same behavior for asyncio code. |
| 53 | + |
| 54 | +```python |
| 55 | +import asyncio |
| 56 | +from opencode_ai import AsyncServerHandle, ServerOptions |
| 57 | +from opencode_ai.api.default import global_health |
| 58 | + |
| 59 | +async def main(): |
| 60 | + async with await AsyncServerHandle.create(ServerOptions(port=0)) as server: |
| 61 | + # ... use server.url |
| 62 | + pass |
| 63 | + |
| 64 | +asyncio.run(main()) |
| 65 | +``` |
| 66 | + |
| 67 | +## Authentication |
| 68 | + |
| 69 | +For deployments that require an auth token, pass it via the |
| 70 | +`AuthenticatedClient` re-export: |
| 71 | + |
| 72 | +```python |
| 73 | +from opencode_ai import AuthenticatedClient |
| 74 | + |
| 75 | +client = AuthenticatedClient(base_url=..., token="...") |
| 76 | +``` |
| 77 | + |
| 78 | +## Regeneration |
| 79 | + |
| 80 | +The package source is generated by `openapi-python-client` from |
| 81 | +`packages/sdk/openapi.json`. Hand-written files (`__init__.py`, `server.py`) |
| 82 | +are layered on top from `scripts/_overlay/`. |
| 83 | + |
| 84 | +```bash |
| 85 | +# Regenerate in place after editing the overlay or the OpenAPI spec. |
| 86 | +python packages/sdk/python/scripts/generate.py |
| 87 | + |
| 88 | +# CI runs this in --check mode and fails if the result differs from disk. |
| 89 | +python packages/sdk/python/scripts/generate.py --check |
| 90 | +``` |
| 91 | + |
| 92 | +## Testing |
| 93 | + |
| 94 | +```bash |
| 95 | +pip install -e packages/sdk/python[dev] |
| 96 | +pytest packages/sdk/python/tests |
| 97 | +``` |
| 98 | + |
| 99 | +The smoke suite under `tests/test_smoke.py` is skipped unless an `opencode` |
| 100 | +binary is on `$PATH`. CI provides the binary by building the monorepo. |
| 101 | + |
| 102 | +## License |
| 103 | + |
| 104 | +MIT, matching the rest of the repository. |
0 commit comments