Official Python SDK for OC Agents - Run AI agents programmatically
Project description
OC Agents SDK
Official Python SDK for OC Agents - Run AI agents programmatically.
Installation
pip install opencomputer-agents-sdk
Quick Start
import asyncio
from oc_agents import OCAgents, RunOptions
async def main():
client = OCAgents(api_key='flt_xxx')
await client.connect()
# Run a task and wait for the result
result = await client.agents.run('agent-id', RunOptions(
prompt='Analyze the sales data and provide a summary'
))
print(result.output) # Structured output (if agent has output_schema)
print(result.result) # Raw text output
await client.disconnect()
asyncio.run(main())
Features
- Real-time streaming - Get live output as the agent works
- Task control - Cancel long-running tasks at any time
- Structured output - Define schemas for typed responses
- Auto-reconnect - Handles connection drops gracefully
- Async/await - Native Python async support
Usage
Simple: Run and Wait
async with OCAgents(api_key='flt_xxx') as client:
result = await client.agents.run('agent-id', RunOptions(
prompt='Analyze this data',
timeout=300.0, # 5 minutes max
))
if result.output:
# Structured output (if agent has output_schema)
print(result.output['summary'])
print(result.output['confidence'])
Advanced: Stream Events
from oc_agents import OCAgents, SubmitOptions
async def main():
client = OCAgents(api_key='flt_xxx')
await client.connect()
task = await client.agents.submit('agent-id', SubmitOptions(
prompt='Long running analysis task'
))
# Listen to real-time events
task.on('stdout', lambda data: print(data, end=''))
task.on('tool_start', lambda tool, _: print(f'Using tool: {tool}'))
task.on('tool_end', lambda tool, _, duration: print(f'{tool} completed in {duration}ms'))
task.on('status', lambda status: print(f'Status: {status}'))
# Cancel if needed
# await asyncio.sleep(60)
# await task.cancel()
# Wait for final result
try:
result = await task.result()
print('Completed:', result.output)
except TaskCancelledError:
print('Task was cancelled')
await client.disconnect()
asyncio.run(main())
List Agents
agents = await client.agents.list()
for agent in agents:
print(f"{agent.name} ({agent.id})")
print(f" Type: {agent.type}")
print(f" Provider: {agent.provider}")
Context Manager
async with OCAgents(api_key='flt_xxx') as client:
result = await client.agents.run('agent-id', RunOptions(prompt='Hello'))
print(result.output)
# Automatically disconnects when done
Error Handling
from oc_agents import (
OCAgents,
OCError,
TaskCancelledError,
TaskFailedError,
TaskTimeoutError,
AuthenticationError,
)
try:
result = await client.agents.run('agent-id', RunOptions(prompt='...'))
except TaskCancelledError:
print('Task was cancelled')
except TaskFailedError as e:
print(f'Task failed: {e.task_error}')
except TaskTimeoutError as e:
print(f'Task timed out after {e.timeout}s')
except AuthenticationError:
print('Invalid API key')
except OCError as e:
print(f'OC Agents error: {e} (code: {e.code})')
Configuration
client = OCAgents(
api_key='flt_xxx', # Required: Your API key
base_url='https://api.opencomputer.dev', # Optional: API base URL
timeout=600.0, # Optional: Default timeout (10 min)
)
API Reference
OCAgents
Main client class.
__init__(api_key, base_url, timeout)- Create a new clientconnect()- Connect to OC Agents (required before using)disconnect()- Disconnect from OC Agentsis_connected()- Check connection statusagents- Access agents resource
AgentsResource
Resource for interacting with agents.
list()- List all agentsget(agent_id)- Get a specific agentrun(agent_id, options)- Run a task and waitsubmit(agent_id, options)- Submit a task (non-blocking)
TaskHandle
Handle for managing a submitted task.
id- Task IDagent_id- Agent IDon(event, handler)- Listen to events ('stdout', 'stderr', 'tool_start', 'tool_end', 'status')cancel()- Cancel the taskresult()- Wait for the resultget_status()- Get current statusis_finished()- Check if task is donestream()- Iterate over events (sync generator)
Types
RunOptions- Options for running a taskSubmitOptions- Options for submitting a taskTaskResult- Result of a completed taskAgent- Agent informationTaskStatus- Task status enum
Requirements
- Python 3.9+
- websockets
- httpx
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file opencomputer_agents_sdk-0.1.0.tar.gz.
File metadata
- Download URL: opencomputer_agents_sdk-0.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6470899e0b5f1b71be9308f1d2169bdd5f3bbdc5e5b68de9d870d9793188164
|
|
| MD5 |
0dae0b3ee512fdc2ecd7e1490f991c08
|
|
| BLAKE2b-256 |
8d60b39c0ca8e96d3b27b930fe3bde8bec9d756b370b75c01fa8cbd716fc6a2e
|
File details
Details for the file opencomputer_agents_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: opencomputer_agents_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d9c8fa01288577fe395b1a6e0765f3a95c11048bef84fe7b5274b68ad477925
|
|
| MD5 |
179a6623d2931662c0d3e7dc14e0e19c
|
|
| BLAKE2b-256 |
8718c23bde17124ebb77e5960e18ddaff8288f6926b2f91897755463de079d5b
|