Clatter LabsHelp Center

How do I get JSON back from SuperCosmos in Python?

How-toUpdated

The documented way to get structured data is tool calling: define a function whose parameters are your schema and read function.arguments. For simple cases, asking for JSON in the prompt and validating it works too.

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.gateway.clatterlabs.xyz/v1",
    api_key=os.environ["CLATTER_API_KEY"],
    default_headers={"X-Clatter-Account": os.environ["CLATTER_ACCOUNT_ID"]},
)
import json

r = client.chat.completions.create(
    model="supercosmos-flash",
    messages=[
        {"role": "system", "content": "Reply with only a JSON object: {\"name\": string, \"age\": number}. No other text."},
        {"role": "user", "content": "Sara is 31."},
    ],
    temperature=0,
)
try:
    data = json.loads(r.choices[0].message.content)
except json.JSONDecodeError:
    data = None  # retry or fall back
print(data)

response_format isn't listed among the documented parameters, so don't rely on it; tool calling is the documented route to a schema.

The client setup above (base URL, key and the X-Clatter-Account header) is the same in every Python example; see Set up the Python SDK. Full parameter list: Create a chat completion.

Still need help?#

We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Related articles

Still need help?

Tried the steps above and still stuck? Email us. We usually reply within the first hour, and always within 24 hours. A full support team is with you.

Infrastructure questions, or permanent deletion of your data
infrastructure@clatterlabs.xyz
For deleting your whole account or the data of one service.