Get machine-readable responses from any AI model. Learn JSON mode, JSON Schema, function calling, and validation patterns for production systems.
By default, AI models return free-form text. Structured output forces the model to return data in a specific format — usually JSON — that your application can parse reliably.
{"sentiment": "positive", "rating": 4, "topics": ["shipping", "quality"]}.
* "Full JSON Schema" means the model validates against your schema. "JSON Mode" means the model outputs valid JSON but doesn't enforce schema constraints.
from openai import OpenAI client = OpenAI(api_key="YOUR_KEY", base_url="https://tokenease.io/v1") response = client.chat.completions.create( model="deepseek-tc", messages=[{ "role": "user", "content": "Extract info: The iPhone 15 costs $799 and has a 6.1-inch screen." }], response_format={"type": "json_object"} ) import json data = json.loads(response.choices[0].message.content) print(data) # {"product": "iPhone 15", "price": 799, "screen_size": "6.1-inch"}
For stricter control, define exactly what fields the model must return. Only GPT-4o and GLM-5 fully enforce schemas.
response = client.chat.completions.create(
model="glm",
messages=[{
"role": "user",
"content": "Review: The hotel was clean but noisy. Staff were friendly."
}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "review_analysis",
"schema": {
"type": "object",
"properties": {
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"]
},
"rating": {"type": "integer", "minimum": 1, "maximum": 5},
"topics": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["sentiment", "rating", "topics"]
}
}
}
)
| Feature | JSON Mode | Function Calling |
|---|---|---|
| Use case | Structured final output | Tool use & multi-step |
| Schema enforcement | Partial | Strong |
| Multi-turn | No | Yes |
| Complexity | Simple | More complex |
| Supported by | Most models | GPT-4o, GLM-5, DeepSeek |
Always validate model output, even with JSON mode. Models can hallucinate fields or return invalid types.
from pydantic import BaseModel, Field import json class ProductInfo(BaseModel): product: str price: float = Field(gt=0) currency: str = Field(default="USD") features: list[str] = Field(default_factory=list) # Validate the model's JSON output raw_json = response.choices[0].message.content data = ProductInfo.model_validate_json(raw_json) print(data.product, data.price) # Type-safe access
response_format: json_object, the model might return {"price": "expensive"} instead of a number. Pydantic catches this before it breaks your application.
response_format. K3, Qwen, and Doubao need explicit instruction.
json_schema on models that only support json_object. The request will fail. Check our support grid above.
json.loads() in try/except and validate with Pydantic.
All models on TokenEase support JSON mode through the OpenAI-compatible API. Key features:
One API key. All models. JSON mode, schema validation, and function calling — all through the same endpoint.
Get Free API Key