Skip to main content
StepFun models can drive application logic such as sentiment analysis, information extraction, and content scoring. Step 5 Preview supports both JSON Mode and JSON Schema structured outputs. Choose the format that fits your application.

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 to step-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 to true to enable strict mode.
  • schema: The expected JSON structure, including properties, types, and required fields.
This example uses 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:
Here, 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:
  1. 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.
  2. Set response_format to { "type": "json_object" } so the model returns parsable JSON.
  3. 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:
Include the schema in your prompt and set 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_reason is stop. If it is length, the output may have been truncated by max_tokens and must not be treated as complete JSON. Consider increasing the output limit and retrying.
  • Handle request failures, refusals, and missing usable content separately 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_format and its nested fields reach the API server intact. Including a field in the client request does not prove that intermediaries preserve it.