Skip to main content

JSON mode

Force the model to return valid JSON by setting response_format:

JSON Schema (strict mode)

Define the exact shape of the response:
Most models follow the schema closely, but nothing on the way validates the answer against it, so keep parsing in a try and validate the result client-side (the Pydantic example below does both).

Which models support structured outputs?

If a model doesn’t support json_schema, use json_object mode and describe the format in your prompt. Check supports_structured_outputs on GET /v1/models first — a few models, including the sonar search models, return false and support neither mode.

With Pydantic (Python)

With TypeScript/Zod