Skip to content
Try Hanzo
Language: English (United States)

Use Hanzo AI with Python

Use Hanzo AI in Python with the official OpenAI SDK (fully compatible), the Anthropic SDK, or via HTTP with httpx/requests. Zero lock-in, standard interfaces.

Get API key — Free
OpenAI SDK (recommended)python
pip install openai
 
from openai import OpenAI
 
client = OpenAI(
    base_url="https://api.hanzo.ai/v1",
    api_key="your-hanzo-api-key",
)
 
response = client.chat.completions.create(
    model="zen4-pro",
    messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
2 of 11 lines point at Hanzo

httpx (raw HTTP)

python

2 of 10 lines point at Hanzo
httpx (raw HTTP)python
import httpx, json
 
response = httpx.post(
    "https://api.hanzo.ai/v1/chat/completions",
    headers={"Authorization": "Bearer your-hanzo-api-key"},
    json={
        "model": "zen4-pro",
        "messages": [{"role": "user", "content": "Hello!"}],
    },
)
print(response.json()["choices"][0]["message"]["content"])

Async with asyncio

python

2 of 13 lines point at Hanzo
Async with asynciopython
import asyncio
from openai import AsyncOpenAI
 
client = AsyncOpenAI(
    base_url="https://api.hanzo.ai/v1",
    api_key="your-hanzo-api-key",
)
 
async def main():
    response = await client.chat.completions.create(
        model="zen4-pro",
        messages=[{"role": "user", "content": "Hello async!"}],
    )
    print(response.choices[0].message.content)
 
asyncio.run(main())

Environment setup

python

3 of 11 lines point at Hanzo
Environment setuppython
# .env
HANZO_API_KEY=your-hanzo-api-key
 
# Load in code
from dotenv import load_dotenv
import os
 
load_dotenv()
 
from openai import OpenAI
client = OpenAI(
    base_url="https://api.hanzo.ai/v1",
    api_key=os.environ["HANZO_API_KEY"],
)

Created by Python Software Foundation

License: PSF License · View source on GitHub →

Hanzo AI is OpenAI-compatible, so existing Python code works with zero refactoring. We deeply appreciate the Python Software Foundation team for building and maintaining this open-source project.

Ready to get started?

Create a free account and get your API key. The Free plan includes free model requests and Kai decisions, no card.