Python SDK
Start sessions, stream records, and invoke agents from Python.
The Python SDK is a client for the /v1 API with session handles, streaming, pagination, and typed errors.
Install
Python 3.10 or newer.
Start a session
run returns a handle while work continues. wait polls until the session is idle or closed and returns how the prompt's message ended (completed, failed, stopped, or cancelled), or None when the session has no message. A timeout stops waiting and leaves the session running.
Use sessions.handle(session_id) to attach to an existing session.
Continue a conversation
Leave conversation.interactive enabled to accept messages:
send returns the message, and wait then waits for it to be answered. The same message key is accepted once per session. A message sent while the agent is working is answered after the one it is on. handle.stop() ends the message the agent is on with stopped and cancels queued ones. It fails with 409 when the session is idle or closed.
Start an agent session
Start an agent session as written:
Send any field of a session start to replace the agent's value for that session:
Pass None to remove the agent's value so its default applies. Leave an argument out to keep the agent's value.
For an agent with an input schema, pass input:
Stream a session
See Session events for every modeled event and complete JSON examples.
Use AsyncEllipsis for asynchronous requests and streaming. Complete records persist; partial text deltas are live-only.
Read results
records returns everything that happened in a session, oldest first. That includes the messages sent to it, the platform's own events, and the agent's output. Pass message_id to keep one message's records. SDK page iterators fetch every page. For manual pagination, use items, has_more, and next_cursor.
Errors
The client defaults to a 60-second request timeout and 2 retries for transport errors, 429, 502, 503, and 504. Configure timeout and max_retries when creating it. Each session start and message gets its own idempotency_key unless you pass one, so a retry never starts a second session or sends a message twice.