Skip to main content
Pass a response_format with type: "json_schema" to get a response that always matches your schema. Works with any model that supports response_format, including OpenAI and Google Gemini models (e.g. google/gemini-2.5-flash).
Not every model enforces response_format. It only takes effect on models that support structured output. If a model doesn’t, the request still succeeds and returns ordinary text — it does not error. Always parse/validate the response, and prefer a model with first-class support such as OpenAI models or Google Gemini (e.g. google/gemini-2.5-flash).

Output modes

response_format is an object whose type selects how the output is shaped: In the JSON modes the content comes back as a string in choices[0].message.content — parse it client-side.

Basic example

Valid JSON without a schema

When you only need parseable JSON and don’t want to define a schema, use json_object and describe the expected keys in the prompt:
The output is always valid JSON, but the exact keys depend on the model following your prompt. Use json_schema when you need that guarantee.

How it works across providers

response_format follows the OpenAI convention and is forwarded to the upstream provider. For providers with a different native contract, Mesh API translates it automatically — for example, Google Gemini models on Vertex AI are converted to Gemini’s native structured-output config (responseMimeType for json_object, plus responseSchema for json_schema), so enforcement runs on the provider side.

Supported models

Structured output works with any model that supports response_format — this includes OpenAI and Google Gemini models (e.g. google/gemini-2.5-flash). Models that don’t support it simply return ordinary text. Use GET /v1/models to see the models enabled on your account.

Notes

  • Set additionalProperties: false to prevent extra fields in the response.
  • finish_reason will be "stop" on success.
  • The response content is a JSON string — parse it with json.loads() / JSON.parse() / json.Unmarshal.

Auto-retry on validation failure (Python)

Some models only best-effort the schema. Set max_retries on parse() to feed a failed response back to the model with the validation error appended. Each retry is a billed call; the default is 0 (no retry).
parse() returns the parsed object directly: parse() is non-streaming — use create() when you need the raw string content plus usage and cost metadata. The async client exposes the same await client.chat.completions.parse(...).