JSON Mode

Guarantee valid, structured JSON output from AI models. Perfect for data extraction, automation, and API integrations.

โŒ Without JSON Mode
Here's the extracted data:

Name: John Smith
Email: john@example.com
Company: Acme Corp

Let me know if you need anything else!
โœ… With JSON Mode
{
  "name": "John Smith",
  "email": "john@example.com",
  "company": "Acme Corp"
}

Two Ways to Get JSON

RECOMMENDED

Structured Outputs

Define a JSON Schema and get guaranteed schema-compliant output. Best for production use with strict requirements.

BASIC

JSON Mode

Guarantees valid JSON but without schema validation. Simpler to use when you just need parseable JSON.

Structured Outputs (Recommended)

Define exactly what shape you want the output to be:

Python
from pydantic import BaseModel

# Define your schema with Pydantic
class ContactInfo(BaseModel):
    name: str
    email: str
    company: str | None

# Request structured output
response = client.beta.chat.completions.parse(
    model="mythic-4",
    messages=[
        {"role": "user", "content": "Extract: John Smith from Acme Corp, john@example.com"}
    ],
    response_format=ContactInfo
)

# Access parsed data directly
contact = response.choices[0].message.parsed
print(contact.name)     # "John Smith"
print(contact.email)    # "john@example.com"
print(contact.company)  # "Acme Corp"

๐Ÿ’ก Type Safety

With Pydantic models, you get full IDE autocomplete and type checking. The response is automatically parsed into your model class.

JSON Mode (Basic)

For simpler use cases where you just need valid JSON:

Python
response = client.chat.completions.create(
    model="mythic-4",
    messages=[
        {"role": "system", "content": "Output valid JSON"},
        {"role": "user", "content": "List 3 programming languages with their use cases"}
    ],
    response_format={"type": "json_object"}
)

# Parse the JSON response
import json
data = json.loads(response.choices[0].message.content)
print(data)

โš ๏ธ Important

When using JSON mode, you must instruct the model to produce JSON in your prompt (system or user message). Otherwise, the model may generate endless whitespace.

Comparison

Feature Structured Outputs JSON Mode
Valid JSON โœ“ Always โœ“ Always
Schema validation โœ“ Guaranteed Not guaranteed
Required keys โœ“ Enforced Not enforced
Type safety โœ“ With Pydantic Manual parsing
Setup Define schema Just enable
Best for Production APIs Quick prototypes

Use Cases

๐Ÿ“ง

Email Extraction

Parse names, emails, and details from text

๐Ÿท๏ธ

Classification

Categorize content with structured labels

๐Ÿ“Š

Data Pipelines

Transform unstructured to structured data

๐Ÿ”Œ

API Responses

Generate consistent API payloads

๐Ÿ“

Form Generation

Create structured form data from descriptions

๐Ÿงช

Test Data

Generate mock data matching your schema

JSON Schema with response_format

You can also use JSON Schema directly:

Python
response = client.chat.completions.create(
    model="mythic-4",
    messages=[
        {"role": "user", "content": "Generate a product with name, price, and category"}
    ],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "product",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "name": {"type": "string"},
                    "price": {"type": "number"},
                    "category": {"type": "string"}
                },
                "required": ["name", "price", "category"],
                "additionalProperties": False
            }
        }
    }
)