Choose an output format
Describing a structure or including a JSON Schema in the prompt can guide JSON Mode, but does not provide strict API-level schema constraints. For strict structure constraints, use the
json_schema parameter rather than only including a schema in the prompt.
JSON Schema: constrain structure through the API
The following approach applies tostep-5-preview, step-3.7-flash, step-3.5-flash, and step-3.5-flash-2603.
Set response_format.type to json_schema and provide these fields in response_format.json_schema:
name: The schema name.strict: Set totrueto enable strict mode.schema: The expected JSON structure, including properties, types, and required fields.
step-5-preview to extract a person’s name and age. The schema is passed directly through the API parameters; it does not need to be repeated in the prompt. Replace YOUR_STEP_API_KEY with your API key and set the STEPFUN_API_KEY environment variable before sending the request; $STEPFUN_API_KEY reads its value. These commands use Bash or Zsh and the pay-as-you-go API.
choices[0].message.content is a string containing JSON. After parsing, an example result is:
required makes both fields mandatory, while additionalProperties: false disallows extra fields. Schema constraints do not guarantee factual or business-level correctness; validate the extracted values in your application. See the Chat Completions API for parameter details.
JSON Mode: return parseable JSON
How to use it
To use JSON Mode, do three things:- Explicitly request JSON in the system prompt and describe the expected structure. You can include a JSON Schema as prompt guidance, but this does not provide strict schema constraints.
- Set
response_formatto{ "type": "json_object" }so the model returns parsable JSON. - Parse the result and validate it. Once it matches expectations, pass it into your business logic.
Sample code
Here’s an example sentiment analyzer that returns JSON:Optional: use a schema in the prompt to guide JSON Mode
JSON Schema describes output fields, types, and required properties. Including a schema in a JSON Mode prompt can guide the output structure, but does not enforce strict constraints. The following schema defines an object with two required fields,url and notes:
response_format: {"type": "json_object"} to guide the output structure. The following is still a prompt example, not an example of the API’s json_schema parameter:
Notes
- For both modes, check that the request succeeded and
finish_reasonisstop. If it islength, the output may have been truncated bymax_tokensand must not be treated as complete JSON. Consider increasing the output limit and retrying. - Handle request failures, refusals, and missing usable
contentseparately rather than parsing them as business data. - After parsing
choices[0].message.content, validate the values against your business requirements. With JSON Mode, also validate fields, types, and required properties yourself. - Input/output examples in a JSON Mode prompt can improve adherence to the intended structure, but do not establish strict schema compliance.
- When using a proxy or gateway, ensure that
response_formatand its nested fields reach the API server intact. Including a field in the client request does not prove that intermediaries preserve it.

