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

# Return structured JSON

> Constrain model output to a schema for application use.

<Frame>
  <img src="https://mintcdn.com/openserv/wCXyJJTiOX6drWSJ/images/tutorials/structured-outputs.webp?fit=max&auto=format&n=wCXyJJTiOX6drWSJ&q=85&s=19eeed95aa19f8c6c06be7c078d007d5" alt="Free-form response particles being constrained into a structured object" width="1536" height="1024" data-path="images/tutorials/structured-outputs.webp" />
</Frame>

Use a JSON schema when your application consumes the response.

## Why structured outputs matter

Prompt instructions alone do not guarantee valid JSON. A schema makes the expected fields, types, required values, and allowed enums explicit. SERV forwards the schema to a compatible upstream provider and preserves the structured response format.

```js theme={null}
const response = await client.chat.completions.create({
  model: "gpt-5.4-mini",
  messages: [
    { role: "system", content: "Extract the support request into the supplied schema." },
    { role: "user", content: "My invoice has the wrong company name." },
  ],
  response_format: {
    type: "json_schema",
    json_schema: {
      name: "support_request",
      strict: true,
      schema: {
        type: "object",
        properties: {
          category: { type: "string", enum: ["billing", "technical", "account"] },
          summary: { type: "string" },
        },
        required: ["category", "summary"],
        additionalProperties: false,
      },
    },
  },
});

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

Validate the result at your application boundary. Handle refusals, truncation, invalid JSON, and invalid field values separately.

Keep the schema small and use enums wherever the valid values are known. If the provider cannot honor the requested format, fail visibly rather than silently treating prose as valid application data.
