# Structured output

> Get replies as JSON that matches a schema you define.

This is not available on this model yet. The model object's `capabilities.structured_output` turns true when it is.

## What it is for

When a program reads the reply, free text is hard to use. Give `response_format` a JSON schema and the reply comes back as JSON in that shape.

## Ask for a schema

```bash
curl https://antarya.ai/v1/chat \
  -H "Authorization: Bearer $ANTARYA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "user", "content": "Summarise this contract'"'"'s termination clause."}
    ],
    "response_format": {
      "type": "json_schema",
      "schema": {
        "type": "object",
        "properties": {
          "notice_period": {"type": "string"},
          "who_can_end_it": {"type": "string"},
          "summary": {"type": "string"}
        },
        "required": ["notice_period", "who_can_end_it", "summary"]
      }
    }
  }'
```

```python
import json
from antarya import Antarya

client = Antarya()

reply = client.chat.create(
    messages=[{"role": "user", "content": "Summarise this contract's termination clause."}],
    response_format={
        "type": "json_schema",
        "schema": {
            "type": "object",
            "properties": {
                "notice_period": {"type": "string"},
                "who_can_end_it": {"type": "string"},
                "summary": {"type": "string"},
            },
            "required": ["notice_period", "who_can_end_it", "summary"],
        },
    },
)
clause = json.loads(reply.content[0].text)
print(clause["notice_period"])
```

```typescript
import Antarya from "antarya";

const client = new Antarya();

const reply = await client.chat.create({
  messages: [{ role: "user", content: "Summarise this contract's termination clause." }],
  response_format: {
    type: "json_schema",
    schema: {
      type: "object",
      properties: {
        notice_period: { type: "string" },
        who_can_end_it: { type: "string" },
        summary: { type: "string" },
      },
      required: ["notice_period", "who_can_end_it", "summary"],
    },
  },
});
const clause = JSON.parse(reply.content[0]?.text ?? "{}");
console.log(clause.notice_period);
```

Output:

```
{
  "id": "req_05",
  "content": [
    {"type": "text", "text": "{\"notice_period\": \"30 days\", \"who_can_end_it\": \"Either party\", \"summary\": \"Either side can end the agreement with written notice.\"}"}
  ],
  "stop_reason": "end_turn",
  "usage": {"input_tokens": 96, "output_tokens": 44}
}
```

The JSON is the text of the first content part. Parse it as you would any JSON string.

## Parameters

- `response_format` (object, optional): Asks for the reply as JSON in a shape you give.
  - `response_format.type` (text, required): Always "json_schema".
  - `response_format.schema` (object, required): A JSON schema for the reply.

## Limits

If the reply stops early because it reached `max_output_tokens`, the JSON may be cut off. Read `stop_reason` before you parse: a reply that ended with `max_output_tokens` is not complete JSON.

| Limit | Value |
|---|---|
| Supported schema keywords | type, properties, required, items, enum, description, additionalProperties |

## Next steps

- [Tool use](https://antarya.ai/docs/guides/tool-use.md): Let Antarya call your functions.
- [System instructions](https://antarya.ai/docs/guides/system-instructions.md): Set the tone and role for every reply.
- [Python SDK](https://antarya.ai/docs/sdk/python.md): Parse replies in Python.
