JSONArray Basic Concepts
JSONArray
is an array structure for storing multiple JSON objects. Each element can be a JSON object, an array, or a basic data type (e.g., string, number, etc.). In the Dify In the JSONArray
It mainly involves parsing and generating JSON data.
All array structures supported by Dify: String, Number, Boolean, Integer,Object, Array, Enum, anyOf
guanxi
- JSON Schema JSON Schema is a specification for describing and validating the JSON data format. It defines the structure of JSON data, including data types, attributes, required fields, etc. JSON Schema itself is also a JSON object that uses specific keywords to describe the organization of the data and constraints.
- JSONArray JSONArray is a structure for JSON data that represents an ordered list of values. The list can contain multiple JSON objects, arrays, or basic data types.JSONArray is formatted as a list of values enclosed in parentheses.
[]
Wrapped up.
Validation and Description: JSON Schema can be used to validate JSON data containing JSONArray. For example, you can define a field in a JSON Schema as an array type and specify that each element in the array must be a JSON object of a particular format. This approach ensures that each element in the JSONArray conforms to the expected structure and type when processing the data.
typical example: Suppose we have a JSON data as follows:
{ "users": [ {"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}. {"name": "Bob", "age": 25} ] }
If you need to define a structured JSON data in Dify, you can use JSON Schema to describe it JSONArray
The structure of the JSON Schema. The corresponding JSON Schema can be:
{ "type": "object", "properties": { "users": { "type": "array", { "items": { "type": "object", "properties": { "type": "array", "items": { "properties": { "name": { "type": "string" }, "age": { "type": "integer" }, "properties": { "age": { "type": "integer" } }, "required": ["name", "age"] } } }, "required": ["name", "age"] } "required": ["users"] }
In this example, the JSON Schema explicitly states that the users
field is a JSONArray, and each of these objects must contain the name
cap (a poem) age
Properties.
So the question returns:How do I get the best out of the Dify Use JSON Schema in LLM to make LLM output follow a structured format.
Models that support JSON Schema functionality in Dify
o1-2024-12-17
and latergpt-4o-mini-2024-07-18
and latergpt-4o-2024-08-06
and later
To learn more about the structured output capabilities of the OpenAI family of models, please refer to the Structured OutputThe
Note: Both json_schema and function calling can generate structured data.json_schema is only used to structure data for content.Function calling is more useful when calling data, tools, functions and generating answers. the "Smart Assistant" association tool in Dify uses function calling.
Full text: Configuring JSON Schema Structured Output in Dify Tools
The following usage path specifies the LLM structured output:
- Connects LLM to tools, functions, data, etc. in the system; set in function definition
strict: true
When you turn it on, the Structured-outputs feature ensures that the parameters LLM generates for a function call exactly match the JSON schema you provide in the function definition. - LLM answers the user with output in a structured content format as defined in the JSON Schema.
The following section outlines how to turn on and use the JSON Schema feature in Dify.
1. Open JSON Schema
Switch the LLM in your application to a model that supports JSON Schema output as described above, and then turn on the JSON Schema
and fill in the JSON Schema template; also turn on the response_format
column and switch to the json_schema
Format.
The content generated by LLM supports the following formats for output:
- Text. Output in text format
2. Define JSON Schema template
You can refer to the following JSON Schema format and define the template content:
{
"name": "template_schema",
"description": "A generic template for JSON Schema",
"schema": {
"type": "object", "properties": { "name": "template_schema", "strict":true, "schema": {
"properties": {
"field1": {
"type": "string", "description": { "field1": {
"description": "Description of field1"
},
"field2": {
"description": "Description of field2"
},
"field3": {
"items": {
"type": "string"
}
},
"field4": {
"type": "object", "description": "Description of field4", {
"description": "Description of field4",
"properties": {
"subfield1": {
"type": "string", "description": { "subfield1": {
"description": "Description of subfield1"
}
},
"required": ["subfield1"], "additionalProperties".
"additionalProperties":false
}
},
"required": ["field1", "field2", "field3", "field4"], "additionalProperties":false } }, "additionalProperties":false }
"additionalProperties":false
}
}
Step-by-step instructions:
- Define basic information:
- set up
name
: Give your schema a descriptive name. - increase
description
: A brief description of the use of schema. - set up
strict
: true: Ensure strict mode.
- establish
schema
Object:
- set up
type: "object"
: Specifies the root level as an object type. - increase
properties
Object: Used to define all fields.
- Define the fields:
- Create an object for each field containing
type
cap (a poem)description
The - Common types:
string
,number
,boolean
,array
,object
The - For arrays, use the
items
Defines the element type. - For objects, the recursive definition
properties
The
- Setting constraints:
- At each level add
required
Array listing all required fields. - Setting the
additionalProperties: false
The
- Special field handling:
- utilization
enum
Limit optional values. - utilization
$ref
Implement recursive structures.
typical example
1. Chain of reasoning (conventional)
JSON Schema File Example
{
"name": "math_reasoning", "description".
"description": "Records steps and final answer for mathematical reasoning", "strict":true, { "name".
"strict":true, "schema": { "name": "math_reasoning
"schema": {
"type": "object", "properties": {
"properties": {
"steps": {
"type": "array", "description": {
"description": "Array of reasoning steps",
"items": {
"properties": {
"explanation": {
"type": "string", "description": {
"description": "Explanation of the reasoning step"
},
"output": {
"type": "string", "description": "Explanation of the reasoning step" }, "output": {
"description": "Output of the reasoning step"
}
}, "output": { "type": "string", "description": "Output of the reasoning step" }
"required": ["explanation", "output"], "additionalProperties":falseProperties".
"additionalProperties":false
}
},
"final_answer": {
"type": "string", "description": "The final answer to the mathematical problem".
"description": "The final answer to the mathematical problem"
}
}, "additionalProperties":false, "description": "The final answer to the mathematical problem" }
"required": ["steps", "final_answer"]
}
}
Cue word reference
You are a helpful math tutor. You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer.
You will be provided with a math problem, and your goal will be to output a step by step solution, along with a final answer.
For each step, just provide the output as an equation use the explanation field to detail the reasoning.
UI generator (root recursive mode)
{
"name": "ui", "description": "Dynamically generated UI", {
"description": "Dynamically Generated UI",
"strict":true, { "schema": {
"schema": {
"type": "object", "properties": {
"properties": {
"type": {
"type": "string",
"enum": ["div", "button", "header", "section", "field", "form"]
},
"label": {
"type": "string", "description": {
"description": "The label of the UI component, used for buttons or form fields"
"children": {
"type": "array", "description".
"description": "Nested UI components", "items": {
"items": {
"$ref": "#"
}
}, "attributes": { "$ref": "#" }
"attributes": {
"type": "array", "description": "UI component, suitable for any element", "attributes": {
"description": "Arbitrary attributes for the UI component, suitable for any element", "items": {
"items": {
"type": "object", "properties": {
"properties": {
"name": {
"type": "string", "description".
"description": "The name of the attribute, for example onClick or className"
"value": {
"type": "string", "description".
"description": "The value of the attribute"
}
},
"required": ["name", "value"]
}
}
},
"required": ["type", "label", "children", "attributes"], "additionalProperties":false, "required": ["name", "value"] } }, "additionalProperties":false
"additionalProperties":false
}
}
Cue word reference:
You are a UI generator AI. Convert the user input into a UI.
Example of effect:
draw attention to sth.
- Make sure that the application prompts include instructions on how to handle situations where user input fails to produce a valid response.
- The model will always attempt to follow the provided pattern and may cause the LLM to hallucinate if the input is completely unrelated to the specified pattern.
- If LLM detects that the input is incompatible with the task, you can include the language in the prompt to specify the return of empty parameters or a specific sentence.
- All fields must be
required
For more information, please refer tohere (literary)The - additionalProperties: false must always be set in the object
- The root level object of the schema must be an object
- Structured Data Output Methods for Large Models: A Selected List of LLM JSON Resources