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.

Typed parsing with parse() (Node.js)

The Node.js SDK (2.0.0) has client.chat.completions.parse(): it builds response_format from a schema, sends a non-streaming request, and returns the parsed object. Pass a Standard Schema validator (Zod v4, Valibot with @valibot/to-json-schema, ArkType) to get a validated, typed result, or a raw JSON schema to get JSON.parse output without validation.
maxRetries feeds a response that fails validation back to the model with the error appended. Each retry is a billed call; the default is 0. An empty response (a refusal or a tool call) is never retried. If the response still can’t be parsed, parse() throws StructuredOutputError. The usual cause is a model that doesn’t support structured output.
parse() is not in the current Python (0.1.11) or Go (v0.1.12) releases. In those SDKs, call create() with response_format and parse choices[0].message.content yourself, as in the examples above.