Structured outputs

Get a response as JSON that follows your schema, on chat completions, Messages, and Responses.

Set response_format to a JSON schema, and the model's response follows it.

import json
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://inference.trymorse.ai/v1",
    api_key=os.environ["MORSE_API_KEY"],
)

completion = client.chat.completions.create(
    model="moonshotai/kimi-k3.1-pro",
    messages=[{"role": "user", "content": "Name a city and its country."}],
    response_format={
        "type": "json_schema",
        "json_schema": {
            "name": "city",
            "strict": True,
            "schema": {
                "type": "object",
                "properties": {
                    "city": {"type": "string"},
                    "country": {"type": "string"},
                },
                "required": ["city", "country"],
                "additionalProperties": False,
            },
        },
    },
)
print(json.loads(completion.choices[0].message.content))

For JSON with no schema, set response_format to {"type": "json_object"}. {"type": "text"} is the default and passes on every model.

Find a model that takes it

Read supported_parameters in GET /v1/models:

ListedThe model takes
structured_outputsjson_schema
response_formatjson_object
Python
import requests

models = requests.get("https://inference.trymorse.ai/v1/models").json()["data"]
for m in models:
    if "structured_outputs" in m["supported_parameters"]:
        print(m["id"])

A model lists a format when at least one of its providers honours it. Morse sends the request only to those providers.

Errors

Status and codeWhen
400 unsupported_parameterNo provider of the model honours the format you asked for.
400 setting_not_forwardedThe key has Let my settings reach the model host on the key's page in the console turned off.

Morse refuses before it calls the model or holds any credit. See Settings.

Messages and Responses

Ask for a schema in each API's own parameter. Morse sends it as response_format, so the same models and errors apply.

APIParameterTypes
Anthropic Messagesoutput_config.formatjson_schema
OpenAI Responsestext.formatjson_object, json_schema
curl https://inference.trymorse.ai/v1/messages \
  -H "x-api-key: $MORSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "moonshotai/kimi-k3.1-pro",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Name a city and its country."}],
    "output_config": {
      "format": {
        "type": "json_schema",
        "schema": {
          "type": "object",
          "properties": {"city": {"type": "string"}, "country": {"type": "string"}},
          "required": ["city", "country"]
        }
      }
    }
  }'

The OpenAI SDK's responses.parse and the Agents SDK's output_type use text.format, so both work.