Skip to content

Fix Agent node structured output not updating flow state variables#5548

Merged
HenryHengZJ merged 1 commit intoFlowiseAI:mainfrom
AhmedMostafa16:fix/agent-structured-output-state-update
Dec 5, 2025
Merged

Fix Agent node structured output not updating flow state variables#5548
HenryHengZJ merged 1 commit intoFlowiseAI:mainfrom
AhmedMostafa16:fix/agent-structured-output-state-update

Conversation

@AhmedMostafa16
Copy link
Copy Markdown
Contributor

Description

Problem

The Agent node's structured output was not properly updating flow state template variables like {{output.decision}}. Variables remained as literal template strings instead of being replaced with actual values.

Root Cause

When structured output is enabled in Agent.ts, finalResponse is wrapped in markdown code blocks for display:

finalResponse = '```json\n' + JSON.stringify(response, null, 2) + '\n```'

This markdown-wrapped string was passed to processTemplateVariables(), which failed to parse it as JSON due to the markdown markers, leaving template variables unprocessed.

Solution

Create a separate outputForStateProcessing variable that contains clean JSON (without markdown wrapping) when structured output is enabled.

This ensures processTemplateVariables receives valid JSON that can:

  1. Be parsed for nested property access (e.g., {{output.decision}})
  2. Be used in string replacements (e.g., "Result: {{ output }}") without [object Object] coercion

Changes

  • packages/components/nodes/agentflow/Agent/Agent.ts: Pass clean JSON string to processTemplateVariables when structured output is enabled, instead of the markdown-wrapped finalResponse

Testing

  • Verified structured output values now correctly replace template variables in flow state
  • Verified both full replacement ({{output}}) and nested property access ({{output.decision}}) work
  • Verified partial string templates ("Result: {{ output }}") work without [object Object] issues

Updated the Agent class to handle structured output by passing a JSON string instead of a markdown-wrapped response to the `processTemplateVariables` function. This change allows for proper nested property access and resolves string replacement issues with object coercion.
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @AhmedMostafa16, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the Agent node where structured output failed to correctly populate flow state template variables. The fix ensures that the underlying JSON data, rather than its markdown-formatted display version, is used for processing template variables, thereby enabling accurate replacement of placeholders like "{{output.decision}}" within the flow state.

Highlights

  • Agent Node Structured Output Fix: Resolved an issue where the Agent node's structured output failed to update flow state template variables (e.g., "{{output.decision}}") because the processTemplateVariables function received markdown-wrapped JSON.
  • Root Cause Identification: The problem stemmed from finalResponse being wrapped in markdown code blocks (json\n...\n) before being passed to processTemplateVariables(), preventing proper JSON parsing and variable replacement.
  • Solution Implementation: Implemented a separate outputForStateProcessing variable to provide clean, unwrapped JSON to processTemplateVariables(), ensuring correct parsing and template variable replacement for both full and nested properties within the flow state.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request addresses a bug where structured output from the Agent node was not correctly updating flow state variables. The root cause was that a markdown-formatted JSON string was being passed for template processing, which failed to parse. The fix introduces a new variable to hold the clean, non-markdown JSON string specifically for state processing, which correctly resolves the issue. The change is clear, well-targeted, and effectively fixes the bug. I have no further comments, great work!

Copy link
Copy Markdown
Contributor

@HenryHengZJ HenryHengZJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you!

@HenryHengZJ HenryHengZJ merged commit 47de407 into FlowiseAI:main Dec 5, 2025
2 checks passed
davehamptonusa pushed a commit to davehamptonusa/Flowise that referenced this pull request Dec 8, 2025
…lowiseAI#5548)

Fix Agent state processing for structured output

Updated the Agent class to handle structured output by passing a JSON string instead of a markdown-wrapped response to the `processTemplateVariables` function. This change allows for proper nested property access and resolves string replacement issues with object coercion.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants