Skip to content

Python Examples

Install the SDK

pip install studiolm

Chat completion

import studiolm

client = studiolm.Client(api_key="sk-...")

response = client.chat.completions.create(
    model="gemma-3-12b-it-qat",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Summarise the theory of relativity in 3 bullet points."},
    ],
)
print(response["choices"][0]["message"]["content"])

Streaming

for chunk in client.chat.completions.create(
    model="gemma-3-12b-it-qat",
    messages=[{"role": "user", "content": "Tell me a short story."}],
    stream=True,
):
    print(chunk["choices"][0].get("delta", {}).get("content", ""), end="", flush=True)
print()

Image generation

image = client.generate(
    "A futuristic cityscape at night, neon lights, cinematic",
    style="vivid",
    size="1216x832",
)
image.save("city.png")

Image-to-image

image = client.generate(
    "Transform into Studio Ghibli style",
    reference_image_url="https://example.com/photo.jpg",
    denoising_strength=0.65,
)
image.save("ghibli.png")
response = client.chat.completions.create(
    model="gemma-3-27b-it-qat",
    messages=[{"role": "user", "content": "What are the latest AI releases this week?"}],
    web_search="auto",
)
print(response["choices"][0]["message"]["content"])

Multimodal (image in chat)

import base64

with open("photo.jpg", "rb") as f:
    b64 = base64.b64encode(f.read()).decode()

response = client.chat.completions.create(
    model="gemma-3-12b-it-qat",
    messages=[{
        "role": "user",
        "content": [
            {"type": "text", "text": "Describe this image."},
            {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}},
        ],
    }],
)
print(response["choices"][0]["message"]["content"])

List models

for m in client.models.list():
    print(m["type"], m["id"])

Without the SDK (requests)

import requests

response = requests.post(
    "https://api.studiolm.dev/v1/chat/completions",
    headers={"Authorization": "Bearer sk-..."},
    json={
        "model": "gemma-3-12b-it-qat",
        "messages": [{"role": "user", "content": "Hello!"}],
    },
)
print(response.json()["choices"][0]["message"]["content"])