Extensibility: Python SDK
Relevant source files
The following files were used as context for generating this wiki page:The OpenCode Python SDK provides a convenient way to interact with the OpenCode API, enabling programmatic control over coding agent sessions. It is designed to work with the client/server architecture of OpenCode, allowing developers to build custom tools and integrations. The SDK handles communication, session management, and response streaming.
This document covers the installation, configuration, and usage of the Python SDK, focusing on core concepts like sessions, messaging, and file management. For more details on the overall project, see the main documentation.
Sources: packages/sdk/python/README.md, README.md
Installation
The SDK can be installed using pip.
pip install opencode-ai
Sources: packages/sdk/python/README.md
Core Components
The primary entry point for the SDK is the OpenCodeAI client class. This class manages authentication and communication with the OpenCode server.
Client Initialization
To begin, you must instantiate the OpenCodeAI client. Configuration can be provided through constructor arguments or environment variables.
The diagram below illustrates the client initialization flow.
The client can be configured with the following parameters:
| Parameter | Type | Environment Variable | Default Value | Description |
|---|---|---|---|---|
api_key | str | OPENCODE_API_KEY | None | Your OpenCode API key. This is required. |
base_url | str | OPENCODE_BASE_URL | http://127.0.0.1:3000/v1 | The base URL for the OpenCode API server. |
timeout | float | None | 600.0 | The timeout in seconds for API requests. |
Sources: packages/sdk/python/src/opencode_ai/client.py:100-117
Here is a basic example of initializing the client:
# packages/sdk/python/examples/basic_usage.py:4-6
import os
from opencode_ai import OpenCodeAI
# It is recommended to set the API key via the OPENCODE_API_KEY environment variable.
# You can also pass it directly to the client constructor.
client = OpenCodeAI(
# api_key="YOUR_API_KEY"
)
Sources: packages/sdk/python/examples/basic_usage.py:4-10
Sessions
Sessions are the primary context for interacting with the OpenCode agent. All conversations and file operations happen within a session. The SDK provides a sessions property on the client to manage them.
The Session object contains methods for interacting with the agent, such as chat and chat_stream.
The diagram below shows the class structure for the client and session.
Creating a Session
A new session can be created using the client.sessions.create() method. You can optionally associate it with a project_id.
# packages/sdk/python/examples/basic_usage.py:12
session = client.sessions.create()
print(f"Created session: {session.id}")
Sources: packages/sdk/python/examples/basic_usage.py:12, packages/sdk/python/src/opencode_ai/client.py:162-177
Interacting with the Agent
Once a session is created, you can send messages to the agent using the chat or chat_stream methods on the Session object.
The following sequence diagram shows the flow of a standard chat interaction.
Standard Chat
The chat() method sends a message and waits for the complete response from the agent. It returns a Message object.
# packages/sdk/python/examples/basic_usage.py:14-15
response = session.chat(message="Hello, who are you?")
print(f"Agent: {response.content}")
Sources: packages/sdk/python/examples/basic_usage.py:14-15, packages/sdk/python/src/opencode_ai/client.py:200-218
Streaming Chat (SSE)
For real-time interactions, the chat_stream() method is recommended. It uses Server-Sent Events (SSE) to stream the agent's response as it's being generated. This method returns an iterator of Message objects.
# packages/sdk/python/examples/basic_usage.py:18-22
print("Agent (streaming): ", end="", flush=True)
stream = session.chat_stream(message="Tell me a short story.")
for chunk in stream:
if chunk.content:
print(chunk.content, end="", flush=True)
This feature is a key part of the SDK's usage, as highlighted in its documentation structure.
Sources: packages/sdk/python/examples/basic_usage.py:18-22, packages/sdk/python/src/opencode_ai/client.py:220-239, packages/sdk/python/mkdocs.yml:23
Documentation and Further Usage
The official documentation for the Python SDK provides more in-depth information on advanced usage patterns. The documentation is structured into several key areas:
- Configuration: Detailed client setup.
- Sessions: In-depth session management.
- Files & Projects: Managing files within a session context.
- Streaming (SSE): Advanced streaming techniques.
- Generation: Using generation capabilities.
- Testing: How to test applications using the SDK.
Sources: packages/sdk/python/mkdocs.yml:18-26