Docs/AI SDK

AI

Python SDK

Last updated May 28, 2026

Official Python client for Cencori. Supports sync calls, async calls, typed responses, and streaming.

Installation

Codetext
pip install cencori

Basic Usage (Synchronous)

The standard client is blocking and best for scripts or simple applications.

Codetext
from cencori import Cencori
 
client = Cencori()  # Reads os.environ["CENCORI_API_KEY"]
 
response = client.ai.chat(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello world"}]
)
 
print(response.content)

Async Usage (Async/Await)

For high-performance web applications (FastAPI, Django, etc.), use the async methods on the same client.

Codetext
import asyncio
from cencori import Cencori
 
async def main():
    client = Cencori()
    
    response = await client.ai.async_chat(
        model="claude-sonnet-4.5",
        messages=[{"role": "user", "content": "Tell me a joke"}]
    )
    
    print(response.content)
 
if __name__ == "__main__":
    asyncio.run(main())

Streaming Responses

Streaming allows you to process the response chunk-by-chunk, which is critical for real-time AI experiences.

Codetext
stream = client.ai.chat_stream(
    model="gemini-2.5-flash",
    messages=[{"role": "user", "content": "Write a long story..."}],
)
 
for chunk in stream:
    if chunk.delta:
        print(chunk.delta, end="")

Text Completions

Codetext
response = client.ai.completions(
    prompt="Write a haiku about production logs",
    model="gpt-4o",
)
 
print(response.content)

Typing & Pydantic Support

The SDK returns typed dataclasses for responses and errors.

Codetext
from cencori.types import ChatResponse
 
# Functions can explicitly accept Cencori types
def process_response(response: ChatResponse):
    print(f"Usage: {response.usage.total_tokens}")
 
# Exceptions also use standard types
from cencori import CencoriError, RateLimitError
 
try:
    client.ai.chat(messages=[{"role": "user", "content": "..."}])
except RateLimitError:
    print("Too many requests!")
except CencoriError as e:
    print(f"Cencori error: {e}")

Advanced Configuration

You can override the base URL or request timeout.

Codetext
client = Cencori(
    api_key="csk_...",
    base_url="https://cencori.com",
    timeout=20.0,
)