Structured Output
Why response_format returns plain text, JSON won’t parse, or the schema isn’t enforced.
Structured output depends on the model honoring response_format. The most
common surprise is that a model silently ignores it. See
Structured Output for the full guide.
I get plain text instead of JSON (no error)
Not every model enforces response_format. On a model that doesn’t support
it, the request still succeeds and returns ordinary text — it does not
error.
- Switch to a model with first-class support, e.g. OpenAI models or
google/gemini-2.5-flash. - Always parse defensively and handle the case where the content isn’t valid JSON.
422 — malformed response_format
The response_format object is invalid. Check:
typeis one of"text","json_object","json_schema".- For
json_schema, thejson_schemaobject has both anameand aschema. - The
schemais valid JSON Schema (no trailing commas, correct types).
Valid JSON but the wrong keys
json_object only guarantees syntactically valid JSON — not which keys
appear. The structure follows your prompt, not a contract.
- Describe the exact keys you expect in the prompt, or
- Use
json_schemafor a guaranteed shape. SetadditionalProperties: falseto forbid extra fields, and list every required field inrequired.
JSON is truncated / won’t parse
If the output is cut off mid-object, max_tokens was too low — the model ran
out of room before closing the JSON.
- Raise
max_tokens. - Check
finish_reason:"length"means truncated,"stop"means complete.
content is a string, not an object
The JSON is returned as a string in choices[0].message.content. You must
parse it yourself:
Output isn’t deterministic across runs
Sampling introduces variation even with a schema.
- Set
temperature: 0for the most deterministic, schema-faithful output. - Note that for Gemini models,
response_formatis translated server-side to Gemini’s nativeresponseMimeType/responseSchema, so enforcement happens on the provider — behavior can differ subtly from OpenAI.
Still stuck?
See the Mesh API error reference or email contact@meshapi.ai.