Text generation
Structured outputs
Get JSON that matches your schema on Inworld Inference with response_format, see which providers support it, and know how JSON mode differs.
Structured outputs make the model return JSON that follows your JSON Schema. Set response_format on a chat completion. Inworld Inference translates it to the provider's structured output feature.
To pull flat fields such as an invoice number, a total or a date out of text or images, with a confidence for each value, you can also use Extractions. Structured outputs fit nested JSON, generated text and PDF input.
There are two modes:
| Mode | response_format | What you get |
|---|---|---|
| Structured outputs | {"type": "json_schema", "json_schema": {...}} | JSON that follows your schema. |
| JSON mode | {"type": "json_object"} | Valid JSON, with no schema. Some models have no JSON mode and drop it. |
Prefer structured outputs. They work on more models, and tell the model exactly which fields you expect.
Request a schema
The examples on this page use Inworld's Gemma 4 26B, inworld/models/gemma-4-26b-a4b-it, which accepts structured outputs and JSON mode. To use another model, check that it accepts structured outputs first: see Provider behavior.
curl --request POST \
--url https://api.inworld.ai/v1/chat/completions \
--header "Authorization: Basic $INWORLD_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"model": "inworld/models/gemma-4-26b-a4b-it",
"messages": [
{"role": "user", "content": "Extract the event: Alice and Bob meet for lunch on Friday."}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "calendar_event",
"strict": true,
"schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"day": {"type": "string"},
"participants": {"type": "array", "items": {"type": "string"}}
},
"required": ["title", "day", "participants"],
"additionalProperties": false
}
}
}
}'import json
import os
from openai import OpenAI
api_key = os.environ["INWORLD_API_KEY"]
client = OpenAI(
base_url="https://api.inworld.ai/v1",
api_key=api_key,
default_headers={"Authorization": f"Basic {api_key}"},
)
response = client.chat.completions.create(
model="inworld/models/gemma-4-26b-a4b-it",
messages=[
{"role": "user", "content": "Extract the event: Alice and Bob meet for lunch on Friday."}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "calendar_event",
"strict": True,
"schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"day": {"type": "string"},
"participants": {"type": "array", "items": {"type": "string"}},
},
"required": ["title", "day", "participants"],
"additionalProperties": False,
},
},
},
)
choice = response.choices[0]
if choice.finish_reason != "stop" or choice.message.content is None:
raise RuntimeError(f"No complete answer: finish_reason={choice.finish_reason}")
event = json.loads(choice.message.content)The JSON arrives as a string in message.content. Parse it, but handle finish_reason: "length": a response cut short can contain incomplete JSON.
json_schema fields
| Field | Type | Required | Description |
|---|---|---|---|
name | string | Yes | A name for the schema. |
schema | object | No | The JSON Schema the output must follow. Without it, the request is sent as JSON mode (json_object), which some models drop. |
strict | boolean | No | Ask the provider to enforce the schema exactly. Defaults to false. |
description | string | No | What the output is for. |
A json_schema format without a json_schema object, without a name, or with a schema that is not a JSON object returns 400.
Provider behavior
The router sends the format in the form the model's provider understands. Support differs by model. In the List models response, check spec.capabilities.responseFormatCapability:
jsonSchemaistruefor models that accept structured outputs (json_schema). On some of them only the schema is sent, notname,descriptionorstrict, and the model still keeps to the schema.json: truedoesn't guarantee JSON mode (json_object). On some models that report it, the router drops the format and the model replies in ordinary text, with no error; see JSON mode. Parse the reply before you rely on it, or usejson_schemaon a model whosejsonSchemaistrue.
Some models do not accept a response format together with tools. On those models, the router drops the response format when the request also includes tools.
Each provider supports its own subset of JSON Schema. If you route across providers or use fallbacks, keep schemas simple (objects, arrays, strings, numbers, booleans and enums) so they work everywhere.
When the answer doesn't match
A schema makes a matching answer very likely, not certain. A response can return 200 with text that doesn't match, so check choices[0].finish_reason before you parse:
stop: the answer finished. Parse it, then validate the result against your schema before you use it.length: the answer was cut off at the token limit and is likely incomplete. Raisemax_tokensand retry.content_filter: the model refused, or a filter stopped the answer. Some models report a refusal this way. The text may not match your schema; handle it as a refusal rather than parsing it.
JSON mode
{"type": "json_object"} asks for valid JSON without a schema:
{
"model": "inworld/models/gemma-4-26b-a4b-it",
"messages": [
{"role": "system", "content": "Reply in JSON with the keys \"title\" and \"day\"."},
{"role": "user", "content": "Alice and Bob meet for lunch on Friday."}
],
"response_format": {"type": "json_object"}
}Describe the JSON you want in the prompt, because the model has no schema to follow.
Some models have no JSON mode. A request with json_object that reaches one, directly or through a fallback, returns 200 with ordinary text: the router drops the format and does not return an error or a warning. Use json_schema with a schema when a request can reach more than one model.
An unrecognized response_format.type is treated as text rather than rejected.