Skip to content

Python

Our API Reference has generic examples in every supported language, and we strive to make the experience of each SDK very similar. However, there are some things specific to the Python SDK that we want to call out here.

All of the generated Python code includes type hints, which should help your editor get the most out of the code.

Python client SDK code uses async/await. You’ll need to use asyncio to run its methods.

The Python client supports async context managers for automatic resource cleanup:

async with Client(store_id=12345, region="us-west-2") as client:
# Your database operations
item = await client.put(my_movie)
result = await client.get(Movie, key_path("/movie-{id}", id=movie_id))
# Client is automatically closed when exiting the context

In general though, we recommend that you create one long-lived client and manually call await client.close() when you are done with it:

client = Client(store_id=12345, region="us-west-2")
try:
# Your database operations
item = await client.put(my_movie)
result = await client.get(Movie, key_path("/movie-{id}", id=movie_id))
finally:
await client.close() # Important: clean up resources

The key_path function can be used to format an ID value (especially a UUID) correctly to include in a key path:

from statelydb import key_path
kp = key_path("/movie-{id}/actor-{actor_id}",
id=result.id, actor_id=actor_id)

Many client APIs return a list of items, but you want to know exactly what type each item is.

if isinstance(item, Movie):
print(f"[Movie] title: {item.title}")
elif isinstance(item, Character):
print(f"[Character] name: {item.name}")

UUIDs are represented as the uuid.UUID type.