Dify - Configure Structured Output

Site: dify|https://dify.ai/

Get plain text:

curl -H "Accept: text/plain" https://jugoya.ai/guides/dify/structured-output

Overview

Configure structured output for LLM nodes in Dify workflows to enforce JSON schema on model responses.

Prerequisites

  • Dify workflow with at least one LLM node
  • LLM node selected in the editor

Access Structured Output Settings

  1. Click on an LLM node in the workflow canvas
  2. In the right panel, scroll to "Output Variables" section
  3. Find "Structured" label with a toggle switch next to it

Enable Structured Output

  1. Click the toggle switch next to "Structured" to enable
  2. Message appears: "Structured output has not been configured yet"
  3. Click button "Configure" to open schema editor

Schema Editor Dialog

Dialog title: "Structured Output Schema"

Editor Tabs

TabPurpose
Visual EditorUser-friendly field builder
JSON SchemaDirect JSON schema input

Visual Editor Features

  • Shows field hierarchy (structured_output > fields)
  • Each field displays: name, type, required status
  • button "Add Field" - Add new field to schema

JSON Schema Features

  • Direct JSON Schema editing
  • Line numbers displayed
  • Syntax error messages shown below editor

Import Schema from Sample JSON

Fastest method to create schema:

  1. Click button "Import from JSON"
  2. Tooltip dialog opens with:
    • input "Editor content" - Paste sample JSON here
    • button "Cancel"
    • button "Submit"
  3. Paste a sample JSON object representing desired output structure
  4. Click "Submit" to auto-generate schema from sample

Example Sample JSON

\{"records":[\{"date":"2020-08-01","value":"100","category":"A"\}]\}

This generates schema with:

  • records (array[object]) - required
    • date (string) - required
    • value (string) - required
    • category (string) - required

Save Schema

  1. Review schema in Visual Editor tab
  2. Click button "Save" to apply
  3. Dialog closes
  4. "Configure" button appears next to Structured toggle
  5. Output Variables section shows new structured_output field

Dialog Buttons

ButtonAction
ResetClear schema to default empty object
CancelClose without saving
SaveApply schema configuration

Tips

  • Use "Import from JSON" for quick schema creation
  • Visual Editor is easier for reviewing/editing existing schema
  • JSON Schema tab may show validation errors - switch to Visual Editor to verify
  • Models with native JSON support (GPT-4, Claude) provide more reliable structured output
  • After configuring, update system prompt to request JSON format instead of plain text

Output Variables After Configuration

Once configured, Output Variables section shows:

  • text (string) - Generate content
  • reasoning_content (string) - Reasoning Content
  • usage (object) - Model Usage Information
  • structured_output (object) - Your configured schema