JSON Mode is a Pro feature. Upgrade to access.
Force the model to output valid, parseable JSON. Perfect for building structured data pipelines, APIs, and automated workflows.
Enabling JSON Mode
Set response_format to {"type": "json_object"} in your request:
from openai import OpenAI
client = OpenAI(
base_url = "https://api.llm.kiwi/v1" ,
api_key = "YOUR_API_KEY"
)
response = client.chat.completions.create(
model = "gpt-oss-20b" ,
response_format = { "type" : "json_object" },
messages = [
{ "role" : "system" , "content" : "Extract data as JSON." },
{ "role" : "user" , "content" : "Parse: John Doe, 30 years old, engineer" }
]
)
print (response.choices[ 0 ].message.content)
# {"name": "John Doe", "age": 30, "occupation": "engineer"}
Parameter Type Description response_formatobject Set to {"type": "json_object"} messagesarray Must include “JSON” in system or user message
You must mention “JSON” in your prompt (system or user message). Otherwise, the request may fail or produce unexpected results.
Example Use Cases
You are a data extraction assistant. Always respond with valid JSON matching this schema:
{
"entities" : [{ "name" : "string" , "type" : "string" }],
"sentiment" : "positive|negative|neutral" ,
"summary" : "string"
}
API Response Generation
response = client.chat.completions.create(
model = "gpt-oss-20b" ,
response_format = { "type" : "json_object" },
messages = [
{
"role" : "system" ,
"content" : """Generate API response JSON with structure:
{"status": "success|error", "data": {...}, "message": "string"}"""
},
{ "role" : "user" , "content" : "Create a response for user signup success" }
]
)
Structured Analysis
response = client.chat.completions.create(
model = "gpt-oss-20b" ,
response_format = { "type" : "json_object" },
messages = [
{
"role" : "system" ,
"content" : "Analyze text and return JSON with keys: topics, keywords, reading_level"
},
{ "role" : "user" , "content" : "Analyze this article..." }
]
)
Model Compatibility
Model JSON Mode Support pro✅ Full support fast✅ Full support default⚠️ Best effort
Error Handling
If JSON parsing fails, wrap your response handling:
import json
try :
data = json.loads(response.choices[ 0 ].message.content)
except json.JSONDecodeError as e:
# Handle invalid JSON
print ( f "Failed to parse: { e } " )
Best Practices
Define Schema in Prompt : Clearly specify the expected JSON structure
Use System Messages : Put JSON requirements in the system role
Validate Output : Always validate parsed JSON against your schema
Handle Edge Cases : Account for optional fields and type variations
Need More Structure? For even stricter schemas, use Function Calling with typed parameters.