Skip to main content

Install

Requires Python 3.11 or later. The Terse CLI also requires Node.js.
Define actors in src/actor.py:

Actor definitions

Extend Actor directly. Use typed synchronous def methods. Use defaults or factories for fields. Prefix helper methods with _. Use concrete public types. JSON primitives, typed collections, unions, and Pydantic models work. Any and untyped collections fail contract generation.

Backend calls

Run from a Python application with pyproject.toml:
Generated calls are synchronous. Use a worker thread from an async web framework. The namespace exposes actors.Counter.Stub, actors.Counter.State, message models, and method argument and result types. Set TERSE_ACTOR_URL and TERSE_API_KEY on the calling backend for hosted actors. Load .env explicitly; uv run --env-file .env can do that.

Browser access

Authenticate the user. Check access to the actor ID. Derive metadata on your backend. Issue a grant:
Return the URL with Cache-Control: no-store. The browser opens a WebSocket. It sends JSON messages such as {"by": 1}.

Socket handlers

Broadcast filters accept except_ids, tags, and tag_match="all" or "any".

Streaming and subscriptions

Use the provider’s synchronous stream inside the handler. Broadcast each output update. Save the completed result before returning. Browser clients receive state snapshots and state_update messages for emitted fields. Replace snapshots. Merge updates. Remove fields listed in removed. Python clients can subscribe to complete typed state:
Subscription callbacks run on a background thread. Errors stop the subscription and reach on_error. For application messages, use counter.connect(actors.Counter.Metadata(user_id="authorized-user")) as a context manager. Call connection.send(...). Iterate over the connection. Handle StateSnapshot and StateUpdate alongside application messages. Broadcasts have no replay. Reconnect and restore saved state. See the quickstart.

Failures and concurrency

Ordinary calls serialize. Failed calls roll back persisted fields. External writes and broadcasts can already have happened. ActorInvocationError with code outcome_unknown means the call may have executed. Reconcile before retrying. @reentrant allows concurrent worker threads. It disables failure rollback across the actor class. Protect shared mutations with an ephemeral lock. See Concepts.