Skip to main content
Beyond chat, Stepfun models can drive application logic—for example, sentiment analysis of reviews or content scoring. You can design prompts that return structured outputs. JSON Mode helps you get machine-parseable JSON for easier integration.

How to use it

To use JSON Mode, do three things:
  1. In the system prompt, describe the expected JSON structure (JSON Schema-style descriptions are recommended).
  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:

Use JSON Schema to guide structure

Sometimes the model may drift from the expected shape. Adding a JSON Schema can clarify your intent. You can define required fields, meanings, and ranges. For example, the Schema below expects an object with url and notes, both required:
Include the Schema in your prompt and enable JSON Mode so the model returns content in the exact structure you want:

Notes

  • When using JSON Mode, check whether finish_reason is stop. If it is length, the model hit max_tokens and the JSON may be incomplete or unparsable.
  • Provide input/output examples in the prompt to help the model understand your scenario and produce outputs that match expectations.