Node Details

  • Name: structuredOutputParser

  • Type: StructuredOutputParser

  • Version: 1.0

  • Category: Output Parsers

Base Classes

  • StructuredOutputParser

  • BaseOutputParser (from LangChain)

Parameters

Inputs

  1. Autofix (Optional)

    • Type: boolean

    • Description: Enables automatic error correction by making additional model calls when parsing fails

    • Default: false

  2. 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:

      [
        {
          "property": "answer",
          "type": "string",
          "description": "answer to the user's question"
        },
        {
          "property": "source",
          "type": "string",
          "description": "sources used to answer the question, should be websites"
        }
      ]
      

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

  1. Configure the desired JSON structure using the datagrid input.

  2. Optionally enable the autofix feature.

  3. Connect this node to an LLM node to parse its output.

  4. 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

  1. Data Extraction

    • Structured information retrieval

    • Consistent output formatting

    • Field validation

    • Multi-field extraction

  2. 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

  1. Schema Design

    • Clear property names

    • Appropriate data types

    • Descriptive field documentation

    • Validation requirements

  2. Error Management

    • Enable autofix for reliability

    • Handle parsing failures

    • Validate input structures

    • Monitor error patterns

  3. Performance Optimization

    • Efficient schema design

    • Appropriate type selection

    • Minimal structure complexity

    • Optimal autofix usage