Restate Python SDK

0.17.1 · active · verified Thu Apr 16

The `restate-sdk` is a Python SDK for Restate, a platform designed for building resilient applications using distributed durable async/await. It enables developers to implement durable backend services, virtual objects, and AI agents that can withstand failures and manage state reliably. The library is actively maintained with frequent minor version releases, integrating new features and improvements often.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart defines a basic Restate service named 'MyService' with a single asynchronous handler 'greet'. Handlers receive a `restate.Context` object for durable operations and can process input to produce an output. To execute this service, it needs to be run within a Restate runtime environment, typically by using the `restate serve` CLI command after defining the service.

import restate

my_service = restate.Service("MyService")

@my_service.handler("greet")
async def greet(ctx: restate.Context, name: str) -> str:
    # Use ctx.run to wrap any non-deterministic operations or external calls
    # For this simple example, we'll just return a greeting.
    return f"Hello {name}!"

# To run the service, typically you'd run this file with 'restate serve'
# or deploy it to a Restate runtime. For local testing, you can use:
# (This part is not runnable without a Restate runtime running)
# app = restate.app([my_service])
# app.run() # This would start an HTTP server

# Example of how to call this service (requires a running Restate server)
async def call_service_example():
    # This client creation is for an ingress client (v0.12.0+)
    async with restate.create_client("http://localhost:8080") as client:
        # Assuming 'MyService' is registered and 'greet' handler is available
        result = await client.object_call(my_service, key="unique-key", handler_name="greet", arg="World")
        print(f"Service call result: {result}")

if __name__ == "__main__":
    # For a full local setup and run, refer to Restate's Python Quickstart documentation.
    # The provided code snippet defines a service, but doesn't run it as a standalone app
    # without a Restate server or the 'restate serve' command.
    import asyncio
    # asyncio.run(call_service_example()) # Uncomment to try calling (requires Restate server)
    print("Service 'MyService' with handler 'greet' defined. To run, use 'restate serve'.")

view raw JSON →