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

# Structured Outputs

> Generate JSON outputs conforming to a schema using Friendli Structured Outputs. Works on all chat-capable models with response_format support.

Friendli offers structured outputs capability with two core guarantees:

* **Model-agnostic**: Supported on **all** chat‑capable models on Friendli.
* **High schema fidelity**: Generates outputs that reliably conform to your provided schemas.

## What Is Structured Outputs

Structured Outputs ensures LLMs return predictable, machine‑readable results (e.g., JSON) instead of free‑form text. This is essential for workflows that require validation or downstream automation.

## Structured Outputs with Friendli

* **Schema‑aligned generation**: High‑accuracy adherence to your JSON Schema.
* **Flexible modes**: Choose strict or loose JSON mode, or apply regex constraints as needed.
* **OpenAI compatible**: Use standard `response_format` options with OpenAI SDKs.

### Structured Outputs Parameters

| Type          | Description                                                         | Name at OpenAI                                                                                |
| ------------- | ------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| `json_schema` | The model returns a JSON object that conforms to the given schema.  | [Structured Outputs](https://platform.openai.com/docs/guides/structured-outputs#introduction) |
| `json_object` | The model can return any JSON object.                               | [JSON mode](https://platform.openai.com/docs/guides/structured-outputs#json-mode)             |
| `regex`       | The model returns a string that conforms to the given regex schema. | N/A                                                                                           |

### Supported JSON Schemas

We support **all seven standard JSON schema types** (`null`, `boolean`, `number`, `integer`, `string`, `object`, `array`). The supported JSON schema keywords are listed below.

<Tip>Using unsupported or unexpected JSON schema keywords may result in them being ignored, triggering an error, or causing undefined behavior.</Tip>

#### Type-Specific Keywords

* `integer`
  * `exclusiveMinimum`, `exclusiveMaximum`, `minimum`, `maximum` (Note: these are not supported in `number`)
* `string`
  * `pattern`
  * `format`
    * Supported values: `uuid`, `date-time`, `date`, `time`, `uri`
* `object`
  * `properties`
  * `additionalProperties` is ignored, and is always set to `False`.
  * `required`: We support both required and optional properties, but have these limitations:
    * The sequence of the properties is fixed.
    * The first property should be `required`. If not, the first required property is moved to the first position.
* `array`
  * `items`
  * `minItems`: We support only `0` or `1` for `minItems`.

#### Constant Values and Enumerated Values

`const` and `enum` only support constant values of `null`, `boolean`, `number`, and `string`.

#### Schema Composition

We support only `anyOf` for [schema composition](https://json-schema.org/understanding-json-schema/reference/combining).

#### Referencing Subschemas

We only support referencing (`$ref`) to "internal" subschemas. These subschemas must be defined within `$defs`, and the value of `$ref` must be a valid URI pointing to a subschema.

#### Annotations

JSON schema annotations such as `title` or `description` are accepted but ignored.

## Simple Example

<Tabs>
  <Tab title="json_schema">
    This example provides a step-by-step guide on how to create a structured output response in JSON format.
    We use Python and the `pydantic` library to define a schema for the output in this example.

    <Steps>
      <Step title="Define Object Schema">
        Define a schema that contains information about a dish.

        ```python theme={null}
        from pydantic import BaseModel

        class Result(BaseModel):
            dish: str
            cuisine: str
            calories: int
        ```
      </Step>

      <Step title="Asking the Model for Structured Output">
        Call structured output and use schema to structure the response.

        <CodeGroup>
          ```python {17-22} OpenAI Python SDK theme={null}
          import os
          from openai import OpenAI

          client = OpenAI(
              base_url="https://api.friendli.ai/serverless/v1",
              api_key=os.getenv("API_KEY"),
          )

          completion = client.chat.completions.create(
              model="zai-org/GLM-5.2",
              messages=[
                  {
                      "role": "user",
                      "content": "Suggest a popular Italian dish in JSON format.",
                  },
              ],
              response_format={
                  "type": "json_schema",
                  "json_schema": {
                      "schema": Result.model_json_schema(),
                  }
              }
          )
          ```

          ```python {15-20} Friendli Python SDK theme={null}
          import os
          from friendli import SyncFriendli

          with SyncFriendli(
              token=os.getenv("API_KEY"),
          ) as friendli:
              completion = friendli.serverless.chat.complete(
                  model="zai-org/GLM-5.2",
                  messages=[
                      {
                          "role": "user",
                          "content": "Suggest a popular Italian dish in JSON format.",
                      },
                  ],
                  response_format={
                      "type": "json_schema",
                      "json_schema": {
                          "schema": Result.model_json_schema(),
                      }
                  }
              )
          ```

          ```bash {12-25} curl theme={null}
          curl -X POST https://api.friendli.ai/serverless/v1/chat/completions \
            -H "Authorization: Bearer $API_KEY" \
            -H "Content-Type: application/json" \
            -d '{
              "model": "zai-org/GLM-5.2",
              "messages": [
                {
                  "role": "user",
                  "content": "Suggest a popular Italian dish in JSON format."
                }
              ],
              "response_format": {
                "type": "json_schema",
                "json_schema": {
                  "schema": {
                    "type": "object",
                    "properties": {
                      "dish": {"type": "string"},
                      "cuisine": {"type": "string"},
                      "calories": {"type": "integer"}
                    },
                    "required": ["dish", "cuisine", "calories"]
                  }
                }
              }
            }'
          ```
        </CodeGroup>
      </Step>

      <Step title="Using Structured Output Results">
        You can use the output in the following way.

        ```python theme={null}
        response = completion.choices[0].message.content
        print(response)
        ```

        The code output result is as follows.

        ```json Result: theme={null}
        {
            "dish": "Spaghetti Bolognese",
            "cuisine": "Italian",
            "calories": 540
        }
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab title="json_object">
    This example demonstrates how to generate an arbitrary JSON object response without a predefined schema.

    <Warning>
      In `json_object` mode, the response may start with `{` or `[` and can be any arbitrary JSON object (dictionary) or array. If you need predictable results, we recommend using `json_schema`.
    </Warning>

    <CodeGroup>
      ```bash curl theme={null}
      curl -X POST https://api.friendli.ai/serverless/v1/chat/completions \
        -H "Authorization: Bearer $API_KEY" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "zai-org/GLM-5.2",
          "messages": [
            {"role": "system", "content": "You MUST answer with JSON."},
            {"role": "user", "content": "Generate a lasagna recipe. (very short)"}
          ],
          "response_format": {"type": "json_object"}
        }'
      ```

      ```python OpenAI Python SDK theme={null}
      import os
      from openai import OpenAI

      client = OpenAI(
        base_url="https://api.friendli.ai/serverless/v1",
        api_key=os.getenv("API_KEY"),
      )

      completion = client.chat.completions.create(
        model="zai-org/GLM-5.2",
        messages=[
          {"role": "system", "content": "You MUST answer with JSON."},
          {"role": "user", "content": "Generate a lasagna recipe. (very short)"},
        ],
        response_format={"type": "json_object"},
      )

      print(completion.choices[0].message.content)
      ```

      ```python Friendli Python SDK theme={null}
      import os
      from friendli import SyncFriendli

      with SyncFriendli(
          token=os.getenv("API_KEY"),
      ) as friendli:
          completion = friendli.serverless.chat.complete(
              model="zai-org/GLM-5.2",
              messages=[
                  {"role": "system", "content": "You MUST answer with JSON."},
                  {"role": "user", "content": "Generate a lasagna recipe. (very short)"},
              ],
              response_format={"type": "json_object"},
          )

          print(completion.choices[0].message.content)
      ```
    </CodeGroup>
  </Tab>

  <Tab title="Regex">
    This example shows how to generate output that matches a specific regular expression pattern.

    <CodeGroup>
      ```bash curl theme={null}
      curl -X POST https://api.friendli.ai/serverless/v1/chat/completions \
        -H "Authorization: Bearer $API_KEY" \
        -H "Content-Type: application/json" \
        -d '{
          "model": "zai-org/GLM-5.2",
          "messages": [
            {
              "role": "user",
              "content": "조선 왕조의 첫번째 왕은 누구입니까 (Who is the first king of the Joseon Dynasty)?"
            }
          ],
          "response_format": {
            "type": "regex",
            "schema": "[\\n ,.?!0-9\\uac00-\\ud7af]*"
          }
        }'
      ```

      ```python OpenAI Python SDK theme={null}
      import os
      from openai import OpenAI

      client = OpenAI(
          base_url="https://api.friendli.ai/serverless/v1",
          api_key=os.getenv("API_KEY"),
      )

      completion = client.chat.completions.create(
          model="zai-org/GLM-5.2",
          messages=[
              {
                  "role": "user",
                  "content": "조선 왕조의 첫번째 왕은 누구입니까 (Who is the first king of the Joseon Dynasty)?",
              },
          ],
          # Korean characters and numbers are allowed in the response.
          response_format={"type": "regex", "schema": "[\n ,.?!0-9\uac00-\ud7af]*"},
      )

      print(completion.choices[0].message.content)
      ```

      ```python Friendli Python SDK theme={null}
      import os
      from friendli import SyncFriendli

      with SyncFriendli(
          token=os.getenv("API_KEY"),
      ) as friendli:
          completion = friendli.serverless.chat.complete(
              model="zai-org/GLM-5.2",
              messages=[
                  {
                      "role": "user",
                      "content": "조선 왕조의 첫번째 왕은 누구입니까 (Who is the first king of the Joseon Dynasty)?",
                  },
              ],
              # Korean characters and numbers are allowed in the response.
              response_format={"type": "regex", "schema": "[\n ,.?!0-9\uac00-\ud7af]*"},
          )

          print(completion.choices[0].message.content)
      ```
    </CodeGroup>
  </Tab>
</Tabs>

## Advanced Examples

For more advanced use cases, see our blog: [Structured Output for LLM Agents](https://friendli.ai/blog/structured-output-llm-agents).
