AI API Structured Output & JSON Mode

Get machine-readable responses from any AI model. Learn JSON mode, JSON Schema, function calling, and validation patterns for production systems.

What Is Structured Output?

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.

Use case: Instead of asking "Summarize this review" and getting prose, ask for a JSON with fields like {"sentiment": "positive", "rating": 4, "topics": ["shipping", "quality"]}.

JSON Mode Support by Model

GLM-5

Full JSON Schema

GPT-4o

response_format + Schema

DeepSeek V4

JSON Mode + Schema

K3

JSON Mode (no Schema)

Qwen-Plus

JSON Mode (no Schema)

Doubao Pro

JSON Mode (no Schema)

* "Full JSON Schema" means the model validates against your schema. "JSON Mode" means the model outputs valid JSON but doesn't enforce schema constraints.

Basic JSON Mode Example

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"}

JSON Schema (Advanced)

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"]
            }
        }
    }
)

Function Calling vs JSON Mode

FeatureJSON ModeFunction Calling
Use caseStructured final outputTool use & multi-step
Schema enforcementPartialStrong
Multi-turnNoYes
ComplexitySimpleMore complex
Supported byMost modelsGPT-4o, GLM-5, DeepSeek

Pydantic Validation Pattern

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
Why validate? Even with response_format: json_object, the model might return {"price": "expensive"} instead of a number. Pydantic catches this before it breaks your application.

Common Structured Output Mistakes

Mistake #1: Not including "Respond in JSON" in the prompt when using models that don't support response_format. K3, Qwen, and Doubao need explicit instruction.
Mistake #2: Using json_schema on models that only support json_object. The request will fail. Check our support grid above.
Mistake #3: Trusting the model to return valid JSON 100% of the time. Always wrap json.loads() in try/except and validate with Pydantic.
Mistake #4: Sending extremely complex schemas (>10 nested levels). Simpler schemas = better adherence. Break complex structures into multiple API calls.

Structured Output at TokenEase

All models on TokenEase support JSON mode through the OpenAI-compatible API. Key features:

Build Structured AI Applications

One API key. All models. JSON mode, schema validation, and function calling — all through the same endpoint.

Get Free API Key