Run
Usectx.run to safely wrap any non-deterministic operation, like HTTP calls or database responses, and have Restate store its result in the execution log.
ctx.run, you cannot use the Restate context (e.g., ctx.get, ctx.sleep, or nested ctx.run).
Serialization
Serialization
By default, the SDK serializes the journal entry with the
json library.
Alternatively, you can specify a Pydantic model or custom serializer.Error handling and retry policies
Error handling and retry policies
Failures in
ctx.run are treated the same as any other handler error. Restate will retry it unless configured otherwise or unless a TerminalError is thrown.You can customize how ctx.run retries via:- You can limit retries by time or count
- When the policy is exhausted, a
TerminalErroris thrown - See the Error Handling Guide and the Sagas Guide for patterns like compensation
Increasing timeouts
Increasing timeouts
If Restate doesn’t receive new journal entries from a service for more than one minute (by default), it will automatically abort the invocation and retry it.However, some business logic can take longer to complete—for example, an LLM call that takes up to 3 minutes to respond.In such cases, you can adjust the service’s abort timeout and inactivity timeout settings to accommodate longer execution times.For more information, see the error handling guide.
Deterministic randoms
When you do non-deterministic operations, like generating UUIDs or random numbers, you must ensure that the results are deterministic on replay. For example, to generate stable UUIDs for things like idempotency keys:UUIDs
To generate stable UUIDs for things like idempotency keys:Random numbers
To generate a deterministic float between0 and 1:
Math.random() but is deterministically replayable.