Skip to main content

Use Cases

The Structured Outputs feature enables models to generate responses that conform to a JSON Schema you provide, making generated results more controllable and easy to parse. This feature facilitates downstream parsing and processing, and is beneficial for integrating results into business systems, suitable for various automation and data processing scenarios.

Supported Models

The following models support Structured Outputs:

Usage

Add the following to your request:
  • Set parameters: Specify your defined JSON Schema via the response_format parameter.
  • Prompt guidance: Guide the model to produce structured output in the prompt.

Examples

The following provides a complete Python code example demonstrating how to use the Structured Outputs feature to generate JSON responses conforming to your provided JSON Schema.

1. Initialize the Client

You need to initialize the client with your Myrouter API key.
from openai import OpenAI

client = OpenAI(
    base_url="https://api.myrouter.ai/openai",
    api_key="<Your API Key>",
)

model = "qwen/qwen-2.5-72b-instruct"

2. Define the JSON Schema

You need to define a JSON Schema. The following example creates a JSON Schema for extracting expense information from user input.
# Define the system prompt for expense tracking.
system_prompt = """You are an expense tracking assistant.
Extract expense information from the user's input and format it according to the provided schema."""

# Define the JSON Schema for structured responses.
response_format = {
    "type": "json_schema",
    "json_schema": {
        "name": "expense_tracking_schema",
        "schema": {
            "type": "object",
            "properties": {
                "expenses": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "description": {
                                "type": "string",
                                "description": "Description of the expense"
                            },
                            "amount": {
                                "type": "number",
                                "description": "Amount spent in dollars"
                            },
                            "date": {
                                "type": "string",
                                "description": "When the expense occurred"
                            },
                            "category": {
                                "type": "string",
                                "description": "Category of expense (e.g., food, office, travel)"
                            }
                        },
                        "required": [
                            "description",
                            "amount"
                        ]
                    }
                },
                "total": {
                    "type": "number",
                    "description": "Total amount of all expenses"
                }
            },
            "required": [
                "expenses",
                "total"
            ],
        },
    },
}

3. Make the API Request

Create the API request. This request includes the response_format parameter, which specifies the JSON Schema defined in the previous step.
chat_completion = client.chat.completions.create(
    model=model,
    messages=[
        {
            "role": "system",
            "content": system_prompt,
        },
        {
            "role": "user",
            "content": """I spent $120 on dinner at an Italian restaurant last Friday with my colleagues.
Also bought office supplies for $45 on Monday.""",
        },
    ],
    max_tokens=1024,
    temperature=0.8,
    stream=False,
    response_format=response_format,
)

response_content = chat_completion.choices[0].message.content

# Parse and prettify the JSON
try:
    json_response = json.loads(response_content)
    prettified_json = json.dumps(json_response, indent=2)
    print(prettified_json)
except json.JSONDecodeError:
    print("Could not parse response as JSON. Raw response:")
    print(response_content)
Output:
{
  "expenses": [
    {
      "date": "2023-03-17",
      "description": "Dinner at Italian restaurant",
      "amount": 120,
      "category": "Food & Dining"
    },
    {
      "date": "2023-03-13",
      "description": "Office supplies",
      "amount": 45,
      "category": "Office Supplies"
    }
  ],
  "total": 165
}

Complete Code

from openai import OpenAI
import json

client = OpenAI(
    base_url="https://api.myrouter.ai/openai",
    api_key="<Your API Key>",
)

model = "qwen/qwen-2.5-72b-instruct"

# Example of structured output using JSON Schema
# This example creates a schema for extracting expense information

# Define the system prompt for expense tracking
system_prompt = """You are an expense tracking assistant.
Extract expense information from the user's input and format it according to the provided schema."""

# Define the JSON Schema for structured responses
response_format = {
    "type": "json_schema",
    "json_schema": {
        "name": "expense_tracking_schema",
        "schema": {
            "type": "object",
            "properties": {
                "expenses": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "description": {
                                "type": "string",
                                "description": "Description of the expense"
                            },
                            "amount": {
                                "type": "number",
                                "description": "Amount spent in dollars"
                            },
                            "date": {
                                "type": "string",
                                "description": "When the expense occurred"
                            },
                            "category": {
                                "type": "string",
                                "description": "Category of expense (e.g., food, office, travel)"
                            }
                        },
                        "required": [
                            "description",
                            "amount"
                        ]
                    }
                },
                "total": {
                    "type": "number",
                    "description": "Total amount of all expenses"
                }
            },
            "required": [
                "expenses",
                "total"
            ],
        },
    },
}

chat_completion = client.chat.completions.create(
    model=model,
    messages=[
        {
            "role": "system",
            "content": system_prompt,
        },
        {
            "role": "user",
            "content": """I spent $120 on dinner at an Italian restaurant last Friday with my colleagues.
Also bought office supplies for $45 on Monday.""",
        },
    ],
    max_tokens=1024,
    temperature=0.8,
    stream=False,
    response_format=response_format,
)

response_content = chat_completion.choices[0].message.content

# Parse and prettify the JSON
try:
    json_response = json.loads(response_content)
    prettified_json = json.dumps(json_response, indent=2)
    print(prettified_json)
except json.JSONDecodeError:
    print("Could not parse response as JSON. Raw response:")
    print(response_content)