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).
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
- Python
- Node.js
- Go
Valid JSON without a schema
When you only need parseable JSON and don’t want to define a schema, usejson_object and describe the expected keys in the prompt:
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 supportsresponse_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: falseto prevent extra fields in the response. finish_reasonwill 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.