Skip to main content

Documentation Index

Fetch the complete documentation index at: https://runinfra.ai/docs/llms.txt

Use this file to discover all available pages before exploring further.

What this does. Forces the model’s response to match a JSON Schema. The model cannot emit invalid JSON, missing fields, or extra keys. When to use it. Any downstream consumer that needs a known shape: database inserts, form-filling, tool arguments, data extraction, classification with fixed enums.

Minimal code

from openai import OpenAI
from pydantic import BaseModel

client = OpenAI(
    base_url="https://api.runinfra.ai/v1",
    api_key="YOUR_RUNINFRA_API_KEY",
)

class Receipt(BaseModel):
    merchant: str
    total_usd: float
    date: str
    items: list[str]

response = client.beta.chat.completions.parse(
    model="default",
    messages=[
        {"role": "system", "content": "Extract the receipt fields."},
        {"role": "user", "content": "I bought coffee and a muffin at Blue Bottle on 2026-04-20 for $12.50"},
    ],
    response_format=Receipt,
)

receipt: Receipt = response.choices[0].message.parsed
print(receipt.merchant, receipt.total_usd)

What to tune

ParameterEffect
response_format: { type: "json_object" }Looser mode. Any valid JSON, no schema enforcement. Use for free-form JSON
response_format.json_schema.strict: trueReject any response that doesn’t fit the schema (default for Pydantic / Zod helpers)
temperature: 0Best for deterministic extractions; structured output handles non-zero too

Common mistakes

  • Using free-form prompts like “respond as JSON” without response_format. The model can still emit trailing commentary or markdown fences. Always pass response_format.
  • Schemas that allow additionalProperties: true. Strict mode requires additionalProperties: false. The Pydantic / Zod helpers set this for you; raw JSON Schema users must add it.
  • Nested anyOf without discriminators. Use enums or discriminated unions; unbounded anyOf confuses the constrained decoder.
  • Expecting enums to be case-insensitive. Schema enums are exact-match. "Paris" won’t satisfy enum: ["paris", "berlin"].

Next steps

Tool calling

Structured args for tool invocations.

Streaming

Stream JSON that parses as it arrives.

RAG

Structured extraction over retrieved context.

API reference

Full response_format contract.