How to Use JSON Schema Output in Dify?
JSON Schema is a specification for describing JSON data structures. Developers can define JSON Schema structures to specify that LLM outputs strictly adhere to the defined data or content, such as generating clear document or code structures.
Models Supporting JSON Schema Functionality
gpt-4o-mini-2024-07-18
and later versionsgpt-4o-2024-08-06
and later versions
For more information on the structured output capabilities of OpenAI series models, please refer to Structured Outputs.
Usage of Structured Outputs
Connect the LLM to tools, functions, data, and other components within the system. Set
strict: true
in the function definition. When enabled, the Structured Outputs feature ensures that the parameters generated by the LLM for function calls precisely match the JSON schema you provided in the function definition.When the LLM responds to users, it outputs content in a structured format according to the definitions in the JSON Schema.
Enabling JSON Schema in Dify
Switch the LLM in your application to one of the models supporting JSON Schema output mentioned above. Then, in the settings form, enable JSON Schema
and fill in the JSON Schema template. Simultaneously, enable the response_format
column and switch it to the json_schema
format.
The content generated by the LLM supports output in the following format:
Text: Output in text format
Defining JSON Schema Templates
You can refer to the following JSON Schema format to define your template content:
Step-by-step guide:
Define basic information:
Set
name
: Choose a descriptive name for your schema.Add
description
: Briefly explain the purpose of the schema.Set
strict
: true to ensure strict mode.
Create the
schema
object:Set
type: "object"
to specify the root level as an object type.Add a
properties
object to define all fields.
Define fields:
Create an object for each field, including
type
anddescription
.Common types:
string
,number
,boolean
,array
,object
.For arrays, use
items
to define element types.For objects, recursively define
properties
.
Set constraints:
Add a
required
array at each level, listing all required fields.Set
additionalProperties: false
at each object level.
Handle special fields:
Use
enum
to restrict optional values.Use
$ref
to implement recursive structures.
Example
1. Chain of thought(routine)
JSON Schema Example
Prompts
UI generation(root recursion mode)
JSON Schema Example
Prompts
Example Output:
Tips
Ensure that the application prompt includes instructions on how to handle cases where user input cannot produce a valid response.
The model will always attempt to follow the provided schema. If the input content is completely unrelated to the specified schema, it may cause the LLM to generate hallucinations.
If the LLM detects that the input is incompatible with the task, you can include language in the prompt to specify returning empty parameters or specific sentences.
All fields must be
required
, For details, please refer to Supported Schemas.additionalProperties: false must always be set in objects.
The root level object of the schema must be an object.
Appendix
Last updated