Structured output makes the model return data in a predictable format that your backend can safely process.

Why is it needed?

Humans can understand this:

The customer is Aman, and his plan is Pro.

A backend usually needs this:

{
  "name": "Aman",
  "plan": "pro"
}

Free-form text can change between requests. The model might add headings, explanations, or different property names, breaking your parser.

Prompting for JSON is not always enough

This instruction helps:

Return JSON only.

But the model may still return:

A structured-output feature lets you define the expected schema.

Example schema

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    plan: { enum: ["free", "pro"] },
    active: { type: "boolean" }
  },
  required: ["name", "plan", "active"],
  additionalProperties: false
};

Conceptual request:

const result = await ai.generate({
  input: "Aman has an active Pro subscription.",
  outputSchema: schema
});

Expected result:

{
  "name": "Aman",
  "plan": "pro",
  "active": true
}

Where it is useful

Still validate on the backend

A valid structure does not guarantee correct information.

For example, this is valid JSON but may be factually wrong:

{
  "total": 5000
}

Validate:

Structured output vs tool calling

Use structured output when you need data. Use tool calling when the model needs to interact with a system.

Common mistakes

Structured output guarantees a shape, not the truth.