Structured output
Get answers as JSON that follows your schema.
When code reads the answer, ask for JSON. On chat completions, response_format tells the model to answer in JSON, and with a schema, in JSON of the shape you describe. The answer is then in choices[0].message.content as a string that you parse.
The two formats
| response_format | What you get | Use it when |
|---|---|---|
| {"type": "json_schema", "json_schema": {...}} | JSON that follows your schema. json_schema needs a name (1 to 64 letters, digits, _ or -) and a schema (a JSON Schema object). strict: true is recommended. | You know the fields you need. This is the one to use. |
| {"type": "json_object"} | A JSON object, but with no fixed shape. Describe the fields you want in the prompt, and the word JSON helps. | You cannot write a schema. Expect a code fence around the JSON from some models, and fields of the wrong type. |
curl https://api.ewest.ai/v1/chat/completions \
-H "Authorization: Bearer $EWEST_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "ewest/gemini-flash",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Extract the person: Ana is 31 years old."}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "person",
"strict": true,
"schema": {
"type": "object",
"properties": {"name": {"type": "string"}, "age": {"type": "integer"}},
"required": ["name", "age"],
"additionalProperties": false}}}}'Which models
There is no supports_* flag for structured output in the catalog, so it cannot be read from the model data. We ran the json_schema request above against these chat models, and each returned JSON that matched: ewest/claude-haiku, ewest/claude-sonnet, ewest/claude-opus, ewest/claude-haiku-5.5, ewest/gemini-flash and ewest/deepseek-v4.1-flash. A schema with an enum, an array and nested objects also matched on ewest/claude-haiku, ewest/gemini-flash and ewest/deepseek-v4.1-flash. Not yet tested: ewest/gemini-3.8-flash and ewest/gemini-4-argon, so do not assume they behave the same. A model may write the keys in a different order than your schema. With json_object, the Claude models answered with the JSON inside a Markdown code fence, and ewest/gemini-flash wrote a number as a string, so parse defensively. Test your own schema on the model you plan to use before you rely on it.
Strict schemas
- Set
strict: true, list every property underrequired, and setadditionalProperties: falseon each object. - Stay with plain types: object, array, string, number, integer, boolean,
enumand nesting. Rules such asminimumorpatternare hints at best: models can ignore them, so check those values in your code. - A schema does not make the content true. The model can fill a valid field with a wrong value, so validate what matters to you.
- Streaming works with
response_format: the JSON arrives in pieces, and is complete only at the end, so parse it once the stream is done.
When it goes wrong
- Cut off: if the answer hits
max_tokensit hasfinish_reason: "length"and the JSON stops half-way, so it will not parse. Checkfinish_reasonfirst and raisemax_tokens. Models that think first use part of the limit for it; 1024 is a good start, and more if you setreasoning_effort(a limit that is too small for it is a400that names the minimum). - Not valid or not the right shape: wrap
JSON.parse/json.loadsin a try, check the fields you use, and on failure retry once or twice. A retry can give a different answer. Do not retry a400, which means the request itself must change (see Errors). - A malformed format:
typemust betext(the default),json_objectorjson_schema. We check the format before any model is called and answer400withparampointing at the problem, such asresponse_format.json_schema.schema, and codeinvalid_value,invalid_typeormissing_required_parameter. Fix the request; do not retry it. The same checks apply totext.formaton Responses, where the fields are flat (paramsuch astext.format.schema). We do not check the schema itself, so parse every answer, even when the request looked right. - Other endpoints:
POST /v1/responsestakes the same schema astext.format({"type": "json_schema", "name", "strict", "schema"}). For tool arguments in a fixed shape, see Tool calling.