Overview
The Kelly AI SDK is built entirely on async/await syntax usingaiohttp for HTTP requests. All API methods are asynchronous and must be awaited.
Basic usage
To use the SDK, you need to create an instance ofKellyAPI and call its methods within an async function:
All API methods in the SDK return coroutines that must be awaited. Forgetting to use
await will return a coroutine object instead of the actual result.Session management
The SDK automatically managesaiohttp.ClientSession instances. By default, a new session is created for each request:
Custom session (recommended)
For better performance when making multiple requests, you can provide your ownaiohttp.ClientSession:
Making multiple concurrent requests
You can useasyncio.gather() to make multiple API calls concurrently:
Timeout configuration
The SDK uses a default timeout of 60 seconds for API requests. This is handled internally in the_fetch and _post_json methods:
View full implementation
View full implementation
The timeout is applied to all HTTP requests in the SDK. If a request exceeds the timeout, a
TimeoutError will be raised. See the error handling guide for more information.From /home/daytona/workspace/source/kellyapi/api.py:31-53Running in different contexts
- Standalone script
- Jupyter Notebook
- Existing event loop
Best practices
Always use async/await
Always use async/await
All SDK methods are asynchronous and must be awaited. Never call them without
await:Reuse sessions for multiple requests
Reuse sessions for multiple requests
Creating a new session for each request adds overhead. Reuse sessions when making multiple API calls:
Handle timeouts appropriately
Handle timeouts appropriately
Long-running operations like image generation may take time. Consider increasing timeouts or implementing retry logic for critical operations.
Use asyncio.gather for concurrent requests
Use asyncio.gather for concurrent requests
When you need results from multiple independent API calls, use
asyncio.gather() to run them concurrently rather than sequentially.Next steps
Error handling
Learn how to handle errors and exceptions
Working with images
Handle image data with base64 encoding