Skip to main content

Overview

Structured outputs allow you to:
  1. Enforce JSON Schema validation on model responses
  2. Get consistent, type-safe outputs
  3. Avoid parsing errors and hallucinated fields
  4. Simplify downstream integrations

Using Strucutred Outputs

Include a response_format parameter in your request with type set to json_schema. The json_schema object defines the exact structure you want.
title="Example Request"
{
  "model": "openai/gpt-4o",
  "messages": [
    { "role": "user", "content": "What's the weather like in London?" }
  ],
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "weather",
      "strict": true,
      "schema": {
        "type": "object",
        "properties": {
          "location": { "type": "string", "description": "City or location name" },
          "temperature": { "type": "number", "description": "Temperature in Celsius" },
          "conditions": { "type": "string", "description": "Weather conditions description" }
        },
        "required": ["location", "temperature", "conditions"],
        "additionalProperties": false
      }
    }
  }
}
The model will return a JSON object that strictly follows your schema:
{
  "location": "London",
  "temperature": 18,
  "conditions": "Partly cloudy with light drizzle"
}

Model Support

Structured outputs are supported on select Anannas models:

OpenAI

gpt-4o-audio-previewgpt-5-chatgpt-5gpt-5-minigpt-5-nanogpt-oss-120bgpt-oss-20bo3-proo3o3-minicodex-minio4-minigpt-4.1gpt-4.1-minigpt-4.1-nanoo1-proo1o1-minio1-mini-2024-09-12gpt-4o-mini-search-previewgpt-4o-search-previewchatgpt-4o-latestgpt-4o-2024-11-20gpt-4o-2024-08-06gpt-4o-minigpt-4o-mini-2024-07-18gpt-4ogpt-4o:extendedgpt-4o-2024-05-13gpt-4-turbo

Anthropic

claude-opus-4claude-sonnet-4claude-3-opusclaude-3-sonnetclaude-3.7-sonnetclaude-3-haikuclaude-3.5-haiku

DeepSeek

deepseek-chatdeepseek-r1deepseek-r1-distill-llama-70b

Meta

llama-3.3-70b-instructllama-3.1-405b-instructllama-3.1-70b-instructllama-3.1-8b-instruct

Google

gemini-2.5-flash-image-previewgemini-2.5-flash-litegemini-2.5-flash-lite-preview-06-17gemini-2.5-flashgemini-2.5-progemini-2.5-pro-previewgemini-2.5-pro-preview-05-06gemini-2.0-flash-lite-001gemini-2.0-flash-001gemini-2.0-flash-exp:freegemini-flash-1.5-8bgemini-flash-1.5gemini-pro-1.5

Gemma

gemma-3-4b-it:freegemma-3-4b-itgemma-3-12b-it:freegemma-3-12b-itgemma-3-27b-it:freegemma-3-27b-itgemma-2-27b-itgemma-2-9b-it:freegemma-2-9b-it

Qwen

qwen-2.5-72b-instructqwen-2.5-7b-instructqwq-32b-preview

Mistral

mistral-medium-3.1codestral-2508devstral-mediumdevstral-smallmistral-small-3.2-24b-instruct:freemistral-small-3.2-24b-instructmagistral-small-2506magistral-medium-2506

Cohere

command-r-pluscommand-r

Moonshot

kimi-k2-0905kimi-k2-0711

Best Practices

  1. Add property descriptions -> Guides the model to populate fields correctly.
  2. Use strict: true-> Ensure no unexpected or hallucinated fields.
  3. Keep schemas lena -> Simpler schemas yield more reliable results.

Example Implementation

typescript
const response = await fetch('https://api.anannas.ai/v1/chat/completions', {
  method: 'POST',
  headers: {
    Authorization: 'Bearer {{API_KEY_REF}}',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'openai/gpt-4o',
    messages: [{ role: 'user', content: 'What is the weather like in London?' }],
    response_format: {
      type: 'json_schema',
      json_schema: {
        name: 'weather',
        strict: true,
        schema: {
          type: 'object',
          properties: {
            location: { type: 'string', description: 'City or location name' },
            temperature: { type: 'number', description: 'Temperature in Celsius' },
            conditions: { type: 'string', description: 'Weather conditions description' }
          },
          required: ['location', 'temperature', 'conditions'],
          additionalProperties: false,
        },
      },
    },
  }),
});
const data = await response.json();
const weatherInfo = data.choices[0].message.content;
python
import requests

response = requests.post(
  "https://api.anannas.ai/v1/chat/completions",
  headers={
    "Authorization": f"Bearer {{API_KEY_REF}}",
    "Content-Type": "application/json",
  },
  json={
    "model": "openai/gpt-4o",
    "messages": [
      {"role": "user", "content": "What is the weather like in London?"},
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "weather",
        "strict": True,
        "schema": {
          "type": "object",
          "properties": {
            "location": {"type": "string", "description": "City or location name"},
            "temperature": {"type": "number", "description": "Temperature in Celsius"},
            "conditions": {"type": "string", "description": "Weather conditions description"},
          },
          "required": ["location", "temperature", "conditions"],
          "additionalProperties": False,
        },
      },
    },
  },
)
data = response.json()
weather_info = data["choices"][0]["message"]["content"]

Streaming with Structured Outputs

Structured outputs also work with streaming.
Just add β€œstream” : true to your request:
json
{
  "stream": true,
  "response_format": {
    "type": "json_schema",
    "json_schema": {
      "name": "weather",
      "strict": true,
      "schema": { "...": "..." }
    }
  }
}

Error Handling

You may encounter:
  1. Model does not support structured outputs -> Error response with unsupported_parameter
  2. Invalid JSON Schema -> Request rejected before completion.
  3. Schema violation -> Response may fail validation in strict mode.
With Anannas, you can unify structured outputs across OpenAI and Anthropic, without needing different request formats.
Was this page helpful?