Prerequisites
- Python >= v3.11
Getting started
Get started quickly with the Python Quickstart.
restate_sdk[serde]
dependency to your Python project to start developing Restate services.
Basic Services
Basic Services group related handlers and expose them as callable endpoints:- Initialize the Service and specify the service name (here
MyService
). - Annotate each handler with
@my_service.handler()
. Optionally, you can override the handler name (heremyHandler
). - Each handler can then be called at
<RESTATE_INGRESS>/myService/myHandler
- Handlers take the
restate.Context
as the first argument. - Handlers can take one optional JSON-serializable input and must return an optional output. These can be of any primitive type,
TypedDict
or Pydantic model (or see custom serialization for advanced types). - Finally, initialize the app, bind the service(s) to it, and serve it. Look at the serving docs to learn more about serving your app.
Virtual Objects
Virtual Objects are services that are stateful and key-addressable — each object instance has a unique ID and persistent state.- Initialize a
restate.VirtualObject
and specify the object’s name (hereMyVirtualObject
). - Each instance is identified by a key (accessible via
ctx.key()
). - Virtual Objects can have exclusive and shared handlers.
- Exclusive handlers receive an
ObjectContext
, allowing read/write access to object state. - Shared handlers have the annotation
kind="shared"
and receive anObjectSharedContext
.
Workflows
Workflows are long-lived processes with a defined lifecycle. They run once per key and are ideal for orchestrating multi-step operations, which require external interaction via signals and queries.- Initialize a
restate.Workflow
and specify its name (hereMyWorkflow
). - Every workflow must include a
run
handler:- This is the main orchestration entry point and is annotated with
@my_workflow.main()
. - It runs exactly once per workflow execution and uses the
WorkflowContext
- Resubmission of the same workflow will fail with “Previously accepted”. The invocation ID can be found in the request header
x-restate-id
. - Use
ctx.key()
to access the workflow’s unique ID
- This is the main orchestration entry point and is annotated with
- Additional handlers are annotated with
@my_workflow.handler()
. They must use theWorkflowSharedContext
and can signal or query the workflow. They can run concurrently with therun
handler and until the retention time expires.