Python SDK
Official Python client for the LatentKit /v1 API with sync and async support.
Route-based client for Python 3.10+.
Install
pip install latentkitSync usage
import os
from latentkit import LatentKit, LatentKitAPIError
with LatentKit(api_key=os.environ["LATENTKIT_API_KEY"]) as client:
try:
response = client.chat.create(
messages=[{"role": "user", "content": "Say hello from LatentKit."}],
max_tokens=100,
response_profile="balanced",
)
print(response["content"])
except LatentKitAPIError as exc:
print({
"status": exc.status_code,
"code": exc.code,
"request_id": exc.request_id,
"body": exc.body,
})Async usage
import asyncio
import os
from latentkit import AsyncLatentKit
async def main() -> None:
async with AsyncLatentKit(api_key=os.environ["LATENTKIT_API_KEY"]) as client:
response = await client.completions.create(
prompt="Write a one-line description of LatentKit.",
system="Respond in plain English.",
)
print(response["content"])
asyncio.run(main())Client options
| Option | Description |
|---|---|
api_key | Required API key |
base_url | Defaults to https://ai.latentkit.com |
timeout | Default 120.0 seconds |
headers | Extra request headers |
http_client | Custom httpx client |
Route-based requests
Do not pass model, provider, route, or policy. The SDK rejects route-control keys. model in responses is reporting metadata for the winning route, not an input field.
Streaming
with LatentKit(api_key=os.environ["LATENTKIT_API_KEY"]) as client:
for event in client.chat.stream(
messages=[{"role": "user", "content": "Count from one to five."}],
):
if event.event == "error":
raise RuntimeError(event.data)
if event.is_done:
break
print(event.data.get("delta", ""), end="")Supported resources
Same surface as the JavaScript SDK: chat, completions, vision, embeddings, image, speech, transcription, translation, video, queue, and agent sessions.
See also JavaScript SDK and Streaming.
Framework notes
- FastAPI: prefer
AsyncLatentKitinside async routes and validate payloads with Pydantic. - Django: wrap the SDK in a service module or DRF APIView and keep
LATENTKIT_API_KEYin server settings. - Flask: create the client inside request handlers or an app-managed lifecycle so HTTP clients are closed cleanly.