Structured Output Parser
The Structured Output Parser is a node used to parse the output of a Language Model (LLM) call into a predefined JSON structure. This node is particularly useful when you need to extract structured information from the LLM’s response in a consistent format.
Node Details
-
Name: structuredOutputParser
-
Type: StructuredOutputParser
-
Version: 1.0
-
Category: Output Parsers
Base Classes
-
StructuredOutputParser
-
BaseOutputParser (from LangChain)
Parameters
Inputs
-
Autofix (Optional)
-
Type: boolean
-
Description: Enables automatic error correction by making additional model calls when parsing fails
-
Default: false
-
-
JSON Structure (Required)
-
Type: datagrid
-
Description: Defines the expected JSON structure for model outputs
-
Schema:
-
Property: JSON property name
-
Type: Data type (string, number, boolean)
-
Description: Property description
-
-
Default:
-
Input
The node takes the following inputs:
-
Configuration for autofix (boolean)
-
JSON structure definition (datagrid)
Output
The node outputs a StructuredOutputParser instance that can be used to parse LLM responses according to the defined JSON structure.
Usage
-
Configure the desired JSON structure using the datagrid input.
-
Optionally enable the autofix feature.
-
Connect this node to an LLM node to parse its output.
-
The parsed output will be in the predefined JSON structure, making it easy to extract specific information from the LLM’s response.
Implementation Details
-
The node uses Zod for schema validation.
-
It converts the user-defined JSON structure into a Zod schema.
-
The LangChain StructuredOutputParser is initialized with the Zod schema.
-
If autofix is enabled, the parser will attempt to correct any parsing errors by making an additional call to the LLM.
Error Handling
If an invalid JSON structure is provided, the node will throw an error with the message “Invalid JSON in StructuredOutputParser” along with the specific exception details.
Use Cases
-
Data Extraction
-
Structured information retrieval
-
Consistent output formatting
-
Field validation
-
Multi-field extraction
-
-
Response Standardization
-
Output normalization
-
Format enforcement
-
Quality assurance
-
Processing automation
-
Integration Notes
-
Uses Zod for schema validation
-
Supports automatic error correction
-
Integrates with LangChain base classes
-
Handles various data types
Best Practices
-
Schema Design
-
Clear property names
-
Appropriate data types
-
Descriptive field documentation
-
Validation requirements
-
-
Error Management
-
Enable autofix for reliability
-
Handle parsing failures
-
Validate input structures
-
Monitor error patterns
-
-
Performance Optimization
-
Efficient schema design
-
Appropriate type selection
-
Minimal structure complexity
-
Optimal autofix usage
-