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

# Structured Output

> Generate consistent JSON using JSON Schema (or Pydantic models in Python).

Maitai supports JSON Schema-based structured output. In Python, you can also pass a Pydantic model and parse the result via `message.parsed`.

<CodeGroup>
  ```python Python (Pydantic) theme={null}
  import maitai

  from pydantic import BaseModel

  class CalendarEvent(BaseModel):
      name: str
      date: str
      participants: list[str]

  maitai_client = maitai.Maitai()
  completion = maitai_client.chat.completions.create(
      application="DEMO_APP",
      intent="CALENDAR",
      model="llama3.1-8b",
      messages=[
          {"role": "system", "content": "Extract the event information."},
          {"role": "user", "content": "AI learning seminar, October 20."},
      ],
      response_format=CalendarEvent,
  )

  event: CalendarEvent = completion.choices[0].message.parsed
  ```
</CodeGroup>

## JSON Schema example

<CodeGroup>
  ```python Python theme={null}
  import maitai

  schema = {
      "name": "athlete_schema",
      "strict": True,
      "schema": {
          "type": "object",
          "properties": {
              "athletes": {
                  "type": "array",
                  "items": {
                      "type": "object",
                      "properties": {
                          "sport": {"type": "string"},
                          "name": {"type": "string"},
                      },
                      "required": ["sport", "name"],
                      "additionalProperties": False,
                  },
              }
          },
          "required": ["athletes"],
          "additionalProperties": False,
      },
  }

  maitai_client = maitai.Maitai()

  response = maitai_client.chat.completions.create(
      session_id="YOUR_SESSION_ID",
      intent="FACTS",
      application="DEMO_APP",
      model="claude-3-5-sonnet-20240620",
      messages=[{"role": "user", "content": "Give me a list of 5 famous athletes and their sport"}],
      response_format={"type": "json_schema", "json_schema": schema}
  )

  data = response.choices[0].message.parsed
  ```

  ```javascript Node theme={null}
  import Maitai from "maitai";

  const maitai = new Maitai();

  const schema = {
    name: "athlete_schema",
    strict: true,
    schema: {
      type: "object",
      properties: {
        athletes: {
          type: "array",
          items: {
            type: "object",
            properties: {
              sport: { type: "string" },
              name: { type: "string" },
            },
            required: ["sport", "name"],
            additionalProperties: false,
          },
        },
      },
      required: ["athletes"],
      additionalProperties: false,
    },
  };

  const response = await maitai.chat.completions.create({
    session_id: "YOUR_SESSION_ID",
    intent: "FACTS",
    application: "DEMO_APP",
    model: "claude-3-5-sonnet-20240620",
    messages: [
      {
        role: "user",
        content: "Give me a list of 5 famous athletes and their sport",
      },
    ],
    response_format: { type: "json_schema", json_schema: schema },
  });

  const data = response.choices[0].message.parsed;
  ```
</CodeGroup>
