Python Versions
Python 3.8+ with full type support and async capabilities
Web Frameworks
Django, Flask, FastAPI, and other WSGI/ASGI frameworks
Async Support
Full asyncio support with httpx and optional aiohttp backends
Data Science
Jupyter Notebooks, pandas integration, and ML workflows
Quickstart
1
Get your API key
Retrieve your API key from your account page.
2
Install the SDK
Install the SDK using your preferred package manager:
3
Initialize the client
Create a new client instance with your API key. If
api_key
is not specified, the SDK will look for the WITHPI_API_KEY
environment variable.4
Make your first scoring request
Issue a request to Pi Scorer:You should receive a response with the scores Pi Scorer assigned to the generation.
Async Usage
The SDK provides full async support withAsyncPiClient
. Simply import AsyncPiClient
instead of PiClient
and use await
with each API call.
By default, the async client uses httpx for HTTP requests. However, for improved concurrency performance you may also use aiohttp as the HTTP backend.
Type Safety
The SDK includes comprehensive type definitions using TypedDicts for requests and Pydantic models for responses:Configuration Options
Client Initialization
Configure the client with various options for your specific use case:Timeouts
Configure request timeouts globally or per request:Retries
The SDK automatically retries certain errors with exponential backoff:- Connection errors
- 408 Request Timeout
- 409 Conflict
- 429 Rate Limit
- 500+ Internal Server errors
Error Handling
In the event of errors, SDK raises subclasses ofAPIConnectionError
:
Error Types
Status Code | Error Type | Description |
---|---|---|
400 | BadRequestError | Invalid request parameters |
401 | AuthenticationError | Invalid or missing API key |
403 | PermissionDeniedError | Insufficient permissions |
404 | NotFoundError | Resource not found |
422 | UnprocessableEntityError | Request validation failed |
429 | RateLimitError | Rate limit exceeded |
500+ | InternalServerError | Server-side errors |
N/A | APIConnectionError | Network connectivity issues |
Requests that time out throw an
APITimeoutError
and are automatically retried according to your retry configuration.Advanced Usage
Accessing Raw Response Data
Access the underlying response object for headers and other metadata:Custom and Undocumented Endpoints
You can make requests to any endpoint, including undocumented ones:HTTP Client Customization
Customize the underlying HTTP client for proxies, transports, and advanced functionality:Resource Management
When making a one-off request, you can use a context manager to ensure the underlying HTTP client and its open connections are closed properly:Logging and Debugging
Logging can be enabled by setting thePI_CLIENT_LOG
environment variable:
__version__
:
Handling Null vs Missing Fields
In an API response, a field may be explicitly null, or missing entirely; in either case, its value is None in this library. You can differentiate the two cases with.model_fields_set
: