> ## Documentation Index
> Fetch the complete documentation index at: https://docs.prisminference.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Structured outputs

> Return valid JSON or enforce a JSON Schema with Chat Completions.

Use `response_format` when your application needs machine-readable output.
Prism supports JSON mode and JSON Schema on `POST /v1/chat/completions`.

## JSON mode

JSON mode guarantees valid JSON but does not enforce a specific shape.

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const response = await client.chat.completions.create({
  model: "prism-glm53",
  messages: [
    {
      role: "user",
      content:
        "Respond in JSON with the bug category and a one-sentence explanation: ...",
    },
  ],
  response_format: { type: "json_object" },
});

const result = JSON.parse(response.choices[0].message.content ?? "{}");
```

Tell the model to return JSON in the prompt. Without that instruction, the
model may spend the token budget generating whitespace.

## JSON Schema

Use strict JSON Schema when downstream code requires known fields:

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const response = await client.chat.completions.create({
  model: "prism-glm53",
  messages: [
    {
      role: "user",
      content: "Classify this pull request and summarize the primary risk: ...",
    },
  ],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "pull_request_review",
      strict: true,
      schema: {
        type: "object",
        properties: {
          risk: {
            type: "string",
            enum: ["low", "medium", "high"],
          },
          summary: { type: "string" },
          requires_human_review: { type: "boolean" },
        },
        required: ["risk", "summary", "requires_human_review"],
        additionalProperties: false,
      },
    },
  },
});
```

Example content:

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "risk": "high",
  "summary": "The migration drops a populated column before backfilling its replacement.",
  "requires_human_review": true
}
```

## Validate in your application

Schema-constrained generation reduces invalid responses, but it does not replace
application validation or authorization.

```typescript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import { z } from "zod";

const PullRequestReview = z.object({
  risk: z.enum(["low", "medium", "high"]),
  summary: z.string(),
  requires_human_review: z.boolean(),
});

const content = response.choices[0].message.content;
if (content == null) {
  throw new Error("Prism returned no structured content");
}

const review = PullRequestReview.parse(JSON.parse(content));
```

Keep schemas small and explicit. Deep nesting, large enums, and ambiguous field
descriptions make the model's job harder.

<Note>
  Tool schemas and response schemas solve different problems. A tool schema
  constrains a function call. `response_format` constrains the assistant's final
  response.
</Note>
