Menu: OpenAI Python SDK

Guides

OpenAI Python SDK

Use the official openai package with Scalaix by changing the base URL and key.

Install

pip install openai
export SCALAIX_API_KEY="sx_..."

Chat completions

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://www.scalaix.com/v1",
    api_key=os.environ["SCALAIX_API_KEY"],
)

reply = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are terse."},
        {"role": "user", "content": "Name three uses of a message queue."},
    ],
)
print(reply.choices[0].message.content)
print(reply.usage)

Use any id from Models, "auto" to let Scalaix route, or "default" for the key's default model.

Streaming

stream = client.chat.completions.create(
    model="claude-3-5-haiku-latest",
    messages=[{"role": "user", "content": "Write a limerick about retries."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
    if chunk.usage:
        print("\n", chunk.usage)

Structured output

from pydantic import BaseModel

class Sentiment(BaseModel):
    label: str
    score: float

result = client.beta.chat.completions.parse(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Sentiment of: 'The update fixed everything.'"}],
    response_format=Sentiment,
)
print(result.choices[0].message.parsed)

Reading Scalaix headers

raw = client.chat.completions.with_raw_response.create(
    model="auto",
    messages=[{"role": "user", "content": "Hello"}],
)
print(raw.headers.get("x-scalaix-model"), raw.headers.get("x-scalaix-cache"), raw.headers.get("x-scalaix-fallbacks"))
reply = raw.parse()

Per-request cache control

client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "What's 2 + 2?"}],
    extra_headers={"X-Scalaix-Cache": "ttl=600"},
)

Async

from openai import AsyncOpenAI

client = AsyncOpenAI(base_url="https://www.scalaix.com/v1", api_key=os.environ["SCALAIX_API_KEY"])
reply = await client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hi"}])

Errors raise the SDK's usual exceptions (AuthenticationError for 401, RateLimitError for 429, APIStatusError for the rest). See Errors.