Skip to content

Structured Output

ZenMux provides structured output to ensure model responses strictly follow your defined JSON Schema format. Use this feature whenever you need fixed, structured data!

Parameters

response_format

  • Set { "type": "json_object" } to produce valid JSON output, without guarantees about specific structure or fields.
  • Set { "type": "json_schema", "json_schema": {...} } to strictly constrain the JSON output structure with stronger type and shape guarantees.
  1. Enable json_object mode

Input structure:

json
{
  "response_format": {
    "type": "json_object"
  }
}

Output structure: content returns a valid JSON payload

json
{
    "model": "openai/gpt-5-nano",
    "choices": [
        {
            "message": {
                // The actual content is a JSON string; shown as JSON here for readability
                "content": {
                    "description": "I am ChatGPT, an AI assistant built by OpenAI. I help answer questions, brainstorm ideas, draft text, explain concepts, debug code, and learn topics. I use patterns from training data to generate helpful, clear responses while recognizing limits and inviting follow-up questions. I adapt tone and detail to your needs."
                }
            }
            ....
        }
    ]
    ....
}
  1. Enable json_schema mode

Define the input according to the standard JSON Schema format:

json
{
  "response_format": {
    "type": "json_schema",
    // Standard json_schema payload
    "json_schema": {
      "name": "role",
      "description": "Introduce yourself",
      "schema": {
        "type": "object",
        "description": "Your messages",
        "properties": {
          "name": {
            "type": "string",
            "description": "your name"
          },
          "city": {
            "type": "string",
            "description": "where your city"
          },
          "desc": {
            "type": "string",
            "description": "description"
          }
        },
        "required": ["name", "city", "desc"],
        "additionalProperties": false
      }
    }
  }
}

The returned content will be JSON conforming to the specified schema:

json
{
    "model": "openai/gpt-5-nano",
    "choices": [
        {
            "message": {
                // The actual content is a JSON string; shown as JSON here for readability
                "content": {
                    "name": "ChatGPT",
                    "city": "Internet",
                    "desc": "I am ChatGPT, an AI language model created by OpenAI. I help with questions, ideas, writing, and problem-solving. I learn from patterns in text and aim to be helpful, accurate, and respectful. I don't have personal experiences, but I strive to understand your needs and respond clearly and kindly today."
                }
                ...
            }
        }
    ],
    ...
}

Supported Models

On the model card page, find the corresponding provider and check whether response_format is listed among the supported parameters, as shown below:

img

API Call Examples

python
from openai import OpenAI

# 1. Initialize the OpenAI client
client = OpenAI(
    # 2. Point the base URL to the ZenMux endpoint
    base_url="https://zenmux.ai/api/v1", 
    # 3. Replace with the API Key from your ZenMux user console
    api_key="<your ZENMUX_API_KEY>", 
)

# 4. Send a request
completion = client.chat.completions.create(
    # 5. Specify the model you want to use, in the format "provider/model_name"
    model="openai/gpt-5", 
    messages=[
        {
            "role": "user",
            "content": "Hi, who are you? Describe yourself using about 50 words. Use JSON response format?"
        }
    ],
    # Option 1: Output is valid JSON, but without guarantees about specific structure or fields.
    # response_format = {
    #      "type": "json_object"
    #  }
    # Option 2: Strictly enforce the JSON output structure with stronger type and shape guarantees
    response_format = { 
        "type": "json_schema", 
        "json_schema": {
            "name": "role",
            "description": "Introduce yourself",
            "schema": {
                "type": "object",
                "description": "Your messages",
                "properties": {
                    "name": {
                        "type": "string",
                        "description": "your name"
                    },
                    "city": {
                        "type": "string",
                        "description": "where your city"
                    },
                    "desc": {
                        "type": "string",
                        "description": "description"
                    }
                },
                "required": ["name", "city", "desc"],
                "additionalProperties": False
            }
        }
    }
)

print(completion.choices[0].message.content)
ts
import OpenAI from "openai";

// 1. Initialize the OpenAI client
const openai = new OpenAI({
  // 2. Point the base URL to the ZenMux endpoint
  baseURL: "https://zenmux.ai/api/v1", 
  // 3. Replace with the API Key from your ZenMux user console
  apiKey = "<your ZENMUX_API_KEY>", 
});

async function main() {
  // 4. Send a request
  const completion = await openai.chat.completions.create({
    // 5. Specify the model you want to use, in the format "provider/model_name"
    model: "openai/gpt-5",
    messages: [
      {
        role: "user",
        content:
          "Hi, who are you? Describe yourself using about 50 words. Use JSON response format?",
      },
    ],
    # Option 1: Output is valid JSON, but without guarantees about specific structure or fields.
    // response_format: {
    //     "type": "json_object"
    // }
    // Option 2: Strictly enforce the JSON output structure with stronger type and shape guarantees
    response_format: {
      type: "json_schema", 
      json_schema: {
        name: "role",
        description: "Introduce yourself",
        schema: {
          type: "object",
          description: "Your messages",
          properties: {
            name: {
              type: "string",
              description: "your name",
            },
            city: {
              type: "string",
              description: "where your city",
            },
            desc: {
              type: "string",
              description: "description",
            },
          },
          required: ["name", "city", "desc"],
          additionalProperties: false,
        },
      },
    },
  });

  console.log(completion.choices[0].message.content);
}

main();