Python SDK
- Your script
OpenProgram()sends messages, answers and stops - Workerthe App's, port 18100; streams text, tool calls, approvals and the result
- Conversationstored; shows in every sidebar
The SDK ships with OpenProgram; nothing else to install. It finds this profile's Worker and starts one if none runs.
from openprogram.interfaces.sdk import OpenProgram
with OpenProgram() as op:
result = op.session().run("Summarise README.md")
print(result.text, result.usage)
With no approval handler, run() is unattended: a step that would ask for approval is refused at once, as with openprogram --print.
Stream a reply
import asyncio
from openprogram.interfaces.sdk import AsyncOpenProgram, TextDelta, ToolCall
async def main():
async with AsyncOpenProgram() as op:
turn = await op.session().send("List the open TODOs")
async for event in turn:
if isinstance(event, TextDelta):
print(event.text, end="", flush=True)
elif isinstance(event, ToolCall):
print(f"\n[{event.name}] {event.arguments}")
result = await turn.result()
asyncio.run(main())
| Event | When |
|---|---|
TurnStarted | The Worker took the message; names the execution |
StatusChanged | running, paused (waiting for an answer), cancelling, … |
TextDelta, ThinkingDelta | A piece of the reply, or of visible reasoning |
ToolCall, ToolResult | A tool call and its output, matched by id |
ApprovalRequest, Question | The turn waits for you; see below |
UsageUpdate | The turn's tokens, as it ends; also result.usage |
The blocking client is the same: for event in session.send(...), then turn.result().
Answer approvals
def decide(request):
print(request.tool, request.arguments, request.risk_level)
return input("Allow? [y/N] ") == "y" # or a scope: "session", "always"
with OpenProgram() as op:
op.session().run("Deploy the docs", permission_mode="ask", on_approval=decide)
| Method | Effect |
|---|---|
request.approve(scope="once") | The call runs; request.scopes lists the wider grants the Worker offers |
request.deny(reason) | The call is refused; the reason reaches the model |
question.answer(value), question.decline() | A question from the Agent; on_question= in run() |
An approval answered here is answered in the App too, and the other way round. The first answer counts; a later one raises CommandRejected.
Stop a turn
turn = session.send("Rewrite every test")
...
turn.cancel()
assert turn.result().status == "cancelled" # result.text: what was written before the stop
Conversations, Agents, models
| Call | Returns |
|---|---|
op.session() | A new conversation, created by its first message; agent="reviewer" binds a saved Agent, project= a project |
op.session(id) | An existing one, to continue; .messages() reads it |
op.sessions() | Every conversation, newest first |
op.agents(), op.models() | Saved Agents; models enabled in Settings, as provider/model |
send(text, model=, effort=, permission_mode=) | Overrides for one turn; session.set_model() keeps a model for the conversation |
send(text, response_format=) | A JSON schema; the value is result.structured_output |
Run a Program
with OpenProgram() as op:
run = op.run_program("research", {"topic": "agent sandboxes"})
print(run.result().output)
It runs as the Programs page runs it: a code node in a conversation, in its own process on the Worker.
Errors
| Error | When |
|---|---|
WorkerNotFound | No Worker, and start=False or it did not start |
AuthenticationError | The token was refused: a token from an earlier Worker, or a wrong one |
TurnRefused | The message was not taken, for example the conversation is busy; code says why |
TurnFailed | The turn ran and failed, usually the model; retryable and kind |
ProgramNotFound, ProgramFailed | No such Program entry; the run ended in error |
ConnectionLost | The Worker went away; the turn goes on there, read it with messages() |
All of them are OpenProgramError with a code.
Another computer's Worker
OpenProgram(url="https://studio.example.net:18100", token=token)
The token is one that Worker accepts: its owner token, or the credential of a paired device. On this computer the owner token is read for you. Whoever holds a token acts as you.