Skip to main content

OpenCode Client

A Python client for interacting with the OpenCode server, enabling programmatic access to AI-powered coding sessions with file operations and conversation history.

Installation

pip install opencode-client

Quick Start

import asyncio
from opencode_client import OpenCodeClient


async def main():
    # Initialize client
    client = OpenCodeClient(
        base_url="http://localhost:8080",
        model_provider="anthropic",
        model="claude-sonnet-4",
        timeout=600,
    )

    # Create a session
    session = await client.create_current_session(title="My Coding Session")

    # Send a message
    response = await client.send_message(
        "Write a hello world function in Python"
    )
    print(response.to_string())


asyncio.run(main())

Features

  • Session Management: Create, list, update, and delete coding sessions
  • Message Handling: Send text and file-based messages with system prompts
  • File Operations: Search files by name or content, read directories, check file status
  • Chat History: Automatic tracking of conversation history

Usage Examples

Session Management

# Create a new session
session = await client.create_current_session(title="Debug Session")

# List all sessions
sessions = await client.list_sessions()

# Update session title
await client.update_session(title="New Title")

# Abort running session
await client.abort_session()

# Delete session
await client.delete_session()

Sending Messages

# Simple text message
response = await client.send_message("Explain async/await in Python")

# Message with system prompt
response = await client.send_message(
    "Write tests for this function",
    system_message="You are an expert in pytest",
)

# Message with file attachment
response = await client.send_message(
    "Review this code", file="path/to/script.py"
)

# Message with multiple files
response = await client.send_message(
    "Compare these implementations", file=["version1.py", "version2.py"]
)

# Message with directory of files
reponse = await client.send_message(
    "What is the content of the `src/client` directory?",
    directory="./src/client",
)

# Message with URL
response = await client.send_message(
    "Analyze this code", file="https://example.com/code.py"
)

File Operations

# Search files by name
files = await client.search_file_by_name("README")

# Search files by content
matches = await client.search_file_by_text("def main")

# Read directory contents
file_info = await client.read_directory_files("/path/to/dir")

# Get file status
status = await client.get_files_status()

Chat History

# Access structured chat history
for message in client.chat_history:
    print(message)

# Access string representation of chat history
for msg_string in client.string_chat_history:
    print(msg_string)

Configuration

Client Parameters

  • base_url: API endpoint URL
  • model_provider: Provider name (e.g., "anthropic", "openai")
  • model: Model identifier (e.g., "claude-sonnet-4")
  • timeout: Request timeout in seconds (default: 600)

Advanced Usage

Working with Multiple Sessions

# Create parent session
parent = await client.create_current_session(title="Parent Session")

# Create child session
child = await client.create_current_session(
    title="Child Session", parent_id=parent.id
)

# Send message to specific session
response = await client.send_message(
    "Help with this bug", session_id=parent.id
)

Error Handling

try:
    response = await client.send_message("Your question")
except ValueError as e:
    print(f"Invalid input: {e}")
except httpx.HTTPError as e:
    print(f"API error: {e}")

Contributing

We welcome contributions! Please read our Contributing Guide to get started.

License

This project is licensed under the MIT License.

Metadata

Release files for opencode-client 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opencode-client 0.1.1
File Size Uploaded Provenance Actions
opencode_client-0.1.1.tar.gz 24.9 kB Without provenance Details for opencode_client-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opencode-client 0.1.1
File Interpreter ABI Platform Actions
opencode_client-0.1.1-py3-none-any.whl Python 3 none any Details for opencode_client-0.1.1-py3-none-any.whl

Total release size: 28.2 kB

Release files / opencode_client-0.1.1.tar.gz

Download URL opencode_client-0.1.1.tar.gz
Size 24.9 kB
Tags Source
SHA-256 checksum
How to use checksums
74a23417ad111bc78d1ce3fe2b13d931d35fcfafbdb4f177ecdb942b125ac7d8
BLAKE2b-256 checksum
How to use checksums
4479bac40e07ef8061dd7cd0bfc2d9b0e5fa8eaeffc01fe9aa6d8284913e6976
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.22

Release files / opencode_client-0.1.1-py3-none-any.whl

Download URL opencode_client-0.1.1-py3-none-any.whl
Size 3.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
14bd7335faaac5f67dc9801ebceb0989f7047bd83f3ba40d3390b6200671dcb4
BLAKE2b-256 checksum
How to use checksums
3e5e3a344982907363acf0ff98c3c2c839038796c8677541ced14c178fa4be16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.22

Security for opencode-client 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page