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:
| Listed | The model takes |
|---|---|
structured_outputs | json_schema |
response_format | json_object |
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 code | When |
|---|---|
400 unsupported_parameter | No provider of the model honours the format you asked for. |
400 setting_not_forwarded | The 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.
| API | Parameter | Types |
|---|---|---|
| Anthropic Messages | output_config.format | json_schema |
| OpenAI Responses | text.format | json_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.