Skip to Content
unii.docs
Sdks

Python SDK

A Python client for building with the Unii API.

Overview

The Unii Python SDK provides a type-hinted client for the Unii API. It supports chat completions, streaming, knowledge bases, and more.

Important

The SDK API surface shown on this page is illustrative. Verify the actual method signatures and types against the latest SDK documentation before using them in production.

Installation

Install the SDK using pip:

pip install unii

Requires Python 3.9 or later.

Initialize the client

Import the client and authenticate with your API key:

init.py
import os
from unii import UniiClient

client = UniiClient(
    api_key=os.environ["UNII_API_KEY"]
)

The SDK reads UNII_API_KEY from the environment automatically if you don't pass an explicit api_key.

Create a chat completion

chat.py
response = client.chat.completions.create(
    model="unii-latest",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Explain RAG in one sentence."},
    ],
    temperature=0.7,
    max_tokens=256,
)

print(response.choices[0].message.content)

Streaming responses

stream.py
stream = client.chat.completions.create(
    model="unii-latest",
    messages=[
        {"role": "user", "content": "Explain RAG in detail."},
    ],
    stream=True,
)

for chunk in stream:
    content = chunk.choices[0].delta.content or ""
    print(content, end="", flush=True)

Error handling

errors.py
from unii import UniiApiError

try:
    response = client.chat.completions.create(
        model="unii-latest",
        messages=[{"role": "user", "content": "Hello"}],
    )
except UniiApiError as e:
    print(f"API error: {e.status_code} - {e.message}")

Methods reference

Key client methods:

ParameterTypeDescription
chat.completions.create()method
Create a chat completion. Returns a completion object or an async iterator when stream=True.
knowledge_bases.create()method
Create a new knowledge base with a name and optional description.
verify_auth()method
Verify that the current API key is valid and active.

Next steps

On this page