JSON mode
Force the model to return valid JSON by setting response_format:
from openai import OpenAI
client = OpenAI(
base_url="https://kymaapi.com/v1",
api_key="ky-your-api-key"
)
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[{"role": "user", "content": "List 3 programming languages with name and year. Return JSON."}],
response_format={"type": "json_object"}
)
import json
data = json.loads(response.choices[0].message.content)
print(data)
# [{"name": "Python", "year": 1991}, {"name": "JavaScript", "year": 1995}, ...]
JSON Schema (strict mode)
Define the exact shape of the response:
response = client.chat.completions.create(
model="qwen-3.6-plus",
messages=[{"role": "user", "content": "List 2 colors"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "colors",
"schema": {
"type": "object",
"properties": {
"colors": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["colors"]
}
}
}
)
data = json.loads(response.choices[0].message.content)
print(data)
# {"colors": ["red", "blue"]}
The response will always match your schema. No parsing errors.
Which models support structured outputs?
| Model | json_object | json_schema |
|---|
| Qwen 3 32B | ✅ | ✅ |
| Qwen 3 Coder | ✅ | ✅ |
| Qwen 3.6 Plus | ✅ | ✅ |
| Llama 3.3 70B | ✅ | ✅ |
| Kimi K2.5 | ✅ | ✅ |
| GPT-OSS 120B | ✅ | ✅ |
| Gemini 2.5 Flash | ✅ | ✅ |
| Gemini 3 Flash | ✅ | ✅ |
| Gemma 4 31B | ✅ | ⚠️ Limited |
If a model doesn’t support json_schema, use json_object mode and describe the format in your prompt. It works with all models.
With Pydantic (Python)
from pydantic import BaseModel
from openai import OpenAI
class MovieReview(BaseModel):
title: str
rating: float
summary: str
client = OpenAI(base_url="https://kymaapi.com/v1", api_key="ky-your-api-key")
response = client.chat.completions.create(
model="qwen-3-32b",
messages=[{"role": "user", "content": "Review the movie Inception"}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "movie_review",
"schema": MovieReview.model_json_schema()
}
}
)
review = MovieReview.model_validate_json(response.choices[0].message.content)
print(f"{review.title}: {review.rating}/10")
With TypeScript/Zod
import OpenAI from 'openai';
import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
const ColorSchema = z.object({
colors: z.array(z.string())
});
const client = new OpenAI({
baseURL: 'https://kymaapi.com/v1',
apiKey: 'ky-your-api-key'
});
const response = await client.chat.completions.create({
model: 'qwen-3.6-plus',
messages: [{ role: 'user', content: 'List 3 colors' }],
response_format: {
type: 'json_schema',
json_schema: {
name: 'colors',
schema: zodToJsonSchema(ColorSchema)
}
}
});
const data = ColorSchema.parse(JSON.parse(response.choices[0].message.content));