Skip to main content

Python SDK Overview

The Statehouse Python SDK provides a clean, Pythonic interface for interacting with the Statehouse daemon. All gRPC and protobuf details are hidden from users.

Installation​

pip install statehouse

Or install from source:

cd python
pip install -e .

Quick Start​

from statehouse import Statehouse

# Connect to the daemon
client = Statehouse(url="localhost:50051")

# Write state in a transaction
with client.begin_transaction() as tx:
tx.write(
agent_id="my-agent",
key="memory",
value={"fact": "Paris is the capital of France"},
)

# Read state
result = client.get_state(agent_id="my-agent", key="memory")
print(result.value) # {"fact": "Paris is the capital of France"}

Design Principles​

The SDK follows these principles:

  1. No gRPC exposure: Users never see protobufs, stubs, or gRPC types
  2. Pythonic API: Uses dataclasses, context managers, and iterators
  3. Type safety: Full type hints for IDE support and static analysis
  4. Minimal dependencies: Only requires grpcio and protobuf

Core Classes​

ClassPurpose
StatehouseMain client for connecting to the daemon
TransactionContext for staging writes and deletes
StateResultResult of read operations
ReplayEventEvent from the replay stream

Exceptions​

All SDK exceptions inherit from StatehouseError:

ExceptionWhen raised
StatehouseErrorBase exception for all SDK errors
TransactionErrorTransaction lifecycle errors
NotFoundErrorRequested resource not found
ConnectionErrorCannot connect to daemon

Next Steps​