Skip to content

vunknown for OpenCode server 2.1.2

Drive OpenCode from Python, fully typed.

All 142 operations of the OpenCode server API, sync and async, with pydantic models for every request, response and event. Generated from the same contract as the official TypeScript client.

pip install opencode-python-sdk

PyPI Python CI

from opencode import OpencodeServer

with OpencodeServer(cwd="/path/to/repo") as server:  # (1)!
    client = server.client()
    session = client.session.create(title="Review")
    client.session.prompt(session.id, text="Summarize the open TODOs")
    client.session.wait(session.id)

    for message in client.message.list(session.id):  # (2)!
        print(message.type)
  1. Starts opencode serve on a free port with a random password, and stops it on exit.
  2. Iterating a page fetches the following pages as needed.
import asyncio
from opencode import AsyncOpencode


async def main() -> None:
    async with AsyncOpencode("http://127.0.0.1:4096") as client:
        session = await client.session.create(title="Review")
        await client.session.prompt(session.id, text="Hello")

        async for message in client.message.list(session.id):
            print(message.type)


asyncio.run(main())  # (1)!
  1. Works with asyncio and trio.

01

Complete

Every operation of the OpenCode 2.1 API, on Opencode and AsyncOpencode, with 96 typed event classes.

02

Generated from the source

The contract is exported from OpenCode's own Effect HttpApi, so names and shapes match the TypeScript client.

03

Safe retries

Only retries when a request cannot have changed server state. A prompt is never sent twice.

04

Forward compatible

New fields, enum values, union variants and events from newer servers are kept, not rejected.

05

Typed errors

Every error the server declares is its own exception class with a typed body.

06

Batteries included

Launch opencode serve, paginate lists, and stream server-sent events with one call each.

Where to next

Unofficial

This package is community-maintained and not affiliated with or endorsed by the OpenCode team. If the OpenCode maintainers want to take it over, the repository and the PyPI name will be transferred.