Skip to content

Structured JSON output

Ask for JSON with response_format. With a schema and strict: true, ZenithAI checks the answer against your schema before returning it.

from openai import OpenAI
from pydantic import BaseModel
class Invoice(BaseModel):
vendor: str
invoice_number: str
invoice_date: str # DD-MM-YYYY
total_inr: float
client = OpenAI(base_url="https://dummydomain/v1", api_key="YOUR_API_KEY")
reply = client.chat.completions.create(
model="Fast",
messages=[{"role": "user", "content": "Extract the invoice fields:\n" + invoice_text}],
response_format={
"type": "json_schema",
"json_schema": {"name": "Invoice", "schema": Invoice.model_json_schema(), "strict": True},
},
)
invoice = Invoice.model_validate_json(reply.choices[0].message.content)
  • {"type": "json_object"} returns one valid JSON object, with no schema.
  • {"type": "json_schema", "json_schema": {"name": ..., "schema": ..., "strict": true}} follows your schema. name may use letters, digits, _ and -, up to 64 characters.

Schemas use JSON Schema 2020-12. Local $defs/$ref, anyOf/oneOf and nullable fields work, so Pydantic v2 and Zod schemas can be used as they are. Remote or recursive references are refused.

Result Meaning What to do
finish_reason: "length" The JSON was cut off Raise max_tokens or ask for less
502 invalid_structured_output The answer failed the check twice Retry; simplify the schema if it repeats
400 unsupported_schema The schema can’t be used Change the schema; retrying won’t help

A structured answer must finish within 300 seconds. The schema is also part of the prompt, so a very large schema leaves less room for your content.