Mesh API

Structured Output

Why response_format returns plain text, JSON won’t parse, or the schema isn’t enforced.

View as Markdown

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:

  • type is one of "text", "json_object", "json_schema".
  • For json_schema, the json_schema object has both a name and a schema.
  • The schema is 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_schema for a guaranteed shape. Set additionalProperties: false to forbid extra fields, and list every required field in required.

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:

1data = json.loads(resp.choices[0].message.content)
1const data = JSON.parse(resp.choices[0].message?.content ?? "{}");

Output isn’t deterministic across runs

Sampling introduces variation even with a schema.

  • Set temperature: 0 for the most deterministic, schema-faithful output.
  • Note that for Gemini models, response_format is translated server-side to Gemini’s native responseMimeType / 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.