Key Features
- Modern Python: Python 3.8+ with full type hints
- Sync and Async: Both
Client(sync) andAsyncClient(async) clients - httpx Powered: Industry-standard HTTP client with automatic retries
- Dataclass Models: Clean, typed data structures
- uv-Ready: Includes
pyproject.tomlfor modern Python packaging - SSE Streaming: First-class Server-Sent Events support
Quick Start
Step 1: Define Your Contract
Step 2: Generate the SDK
Step 3: Install and Use
Generated Code Structure
pyproject.toml
init.py
Client Configuration
Creating a Client
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
api_key | str | None | None | Authentication token |
base_url | str | None | Service default | API base URL |
timeout | float | None | None (no timeout) | Request timeout in seconds |
max_retries | int | 2 | Maximum retry attempts |
default_headers | Dict[str, str] | None | None | Headers for all requests |
Modifying Client Options
Create a new client with modified options:Closing the Client
Always close the client when done:Async Client
The SDK includes a fully async client for use withasyncio:
Async Context Manager
Type System
Type Mapping Reference
| Contract Type | Python Type |
|---|---|
string | str |
bool | bool |
int, int8-int64 | int |
uint, uint8-uint64 | int |
float32, float64 | float |
time.Time | datetime |
json.RawMessage | object |
any | object |
[]T | List[T] |
map[string]T | Dict[str, T] |
Dataclass Types
Contract struct types generate Python dataclasses:Optional and Nullable Fields
| Contract Definition | Python Type |
|---|---|
| Required field | T |
optional: true | Optional[T] |
nullable: true | Optional[T] |
| Both optional and nullable | Optional[T] |
Enum Fields
Enum fields use literal type hints in the docstring:List and Dict Types
Resources and Methods
Resource Pattern
Each contract resource becomes a property on the client:Method Signatures
Methods use Python naming conventions (snake_case):| Contract Method | Python Signature |
|---|---|
Create | create(**kwargs) -> Todo |
List | list() -> ListOutput |
Get | get(id: str) -> Todo |
Delete | delete(id: str) -> None |
Calling Methods
Methods accept keyword arguments matching the input type fields:Streaming (SSE)
For methods with streaming support, the SDK provides iterator-based consumption:Sync Streaming
Async Streaming
Collecting All Events
Error Handling
Error Types
The SDK defines three error types:Handling Errors
Automatic Retries
The client automatically retries failed requests (except for 4xx errors):- Network errors
- 5xx server errors
- Timeout errors
Advanced Usage
Custom Headers
Authentication Modes
The SDK supports different authentication modes based on your service configuration:Timeout Configuration
Using with Existing httpx Client
The generated SDK uses httpx internally. For advanced use cases, you can access the underlying client:Complete Example
Server
Client Usage
Async Example
Installation and Distribution
Installing Locally
Publishing to PyPI
Installing from Git
See Also
- Overview - Introduction to SDK generation
- Go - Generate Go clients
- TypeScript - Generate TypeScript clients
- Types - Contract type system
- REST Transport - Serve your API over REST