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 OpenAIfrom 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)import OpenAI from "openai";import { z } from "zod";import { zodToJsonSchema } from "zod-to-json-schema";
const Invoice = z.object({ vendor: z.string(), invoice_number: z.string(), invoice_date: z.string(), total_inr: z.number(),});
const client = new OpenAI({ baseURL: "https://dummydomain/v1", apiKey: process.env.API_KEY });const reply = await client.chat.completions.create({ model: "Fast", messages: [{ role: "user", content: `Extract the invoice fields:\n${invoiceText}` }], response_format: { type: "json_schema", json_schema: { name: "Invoice", strict: true, schema: zodToJsonSchema(Invoice, { target: "openAi" }) } },});const invoice = Invoice.parse(JSON.parse(reply.choices[0].message.content));Two modes
Section titled “Two modes”{"type": "json_object"}returns one valid JSON object, with no schema.{"type": "json_schema", "json_schema": {"name": ..., "schema": ..., "strict": true}}follows your schema.namemay 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.
When it fails
Section titled “When it fails”| 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.