Skip to content

feat: Preserve object return type in Agentflow Custom Function#5895

Open
beliga44 wants to merge 2 commits intoFlowiseAI:mainfrom
beliga44:feat/custom-function-object-return
Open

feat: Preserve object return type in Agentflow Custom Function#5895
beliga44 wants to merge 2 commits intoFlowiseAI:mainfrom
beliga44:feat/custom-function-object-return

Conversation

@beliga44
Copy link

@beliga44 beliga44 commented Mar 4, 2026

Closes #4811

Problem

Custom Function nodes in Agentflow V2 always coerce object returns
to strings via JSON.stringify(). This forces API consumers to
JSON.parse() the response and prevents downstream Custom Functions
from receiving structured data.

Solution

Remove the automatic JSON.stringify() on object returns. When user
code returns a JavaScript object, output.content now preserves the
original object. String returns remain unchanged.

Changes

  • 1 file: packages/components/nodes/agentflow/CustomFunction/CustomFunction.ts
  • Removed if (typeof response === 'object') { finalOutput = JSON.stringify(...) }
  • Added string conversion only for processTemplateVariables (state processing)

Backward Compatibility

  • Users who return JSON.stringify(obj) explicitly → unchanged (string in, string out)
  • Users who return obj → now get object instead of string (the fix)
  • processTemplateVariables still receives string for state updates
  • Internal display/execution logging unaffected
image image

…ectly

Previously, object return values were automatically stringified before being
passed downstream. Now objects are returned as-is, with stringification only
applied when updating flow state template variables. Also adds freeSolo support
for the state key field and updates the example comment accordingly.

Co-Authored-By: beliga44 <beliga44@users.noreply.github.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 significantly enhances the flexibility of Agentflow Custom Functions by enabling them to return structured JavaScript objects directly, rather than forcing a string conversion. This change streamlines data handling for subsequent operations and external consumers, improving the overall efficiency and usability of the Agentflow system. It ensures that while structured data is preserved for direct output, state updates continue to receive stringified representations where necessary, maintaining backward compatibility for internal processes.

Highlights

  • Object Return Type Preservation: Agentflow Custom Functions now preserve the original JavaScript object when returned, instead of automatically coercing it to a string via JSON.stringify().
  • Improved Data Flow: This change allows downstream Custom Functions and API consumers to receive structured data directly, eliminating the need for manual JSON.parse() operations.
  • Documentation Update: The internal documentation for Custom Functions has been updated to reflect that both string values and JSON objects can now be returned.
  • State Variable Handling: Objects are now explicitly stringified before being passed to processTemplateVariables to ensure state updates continue to receive string representations.
  • Node Version Increment: The version of the Custom Function node has been updated from 1.1 to 1.2.
  • Enhanced Key Input: The 'Key' input field for runtime state keys now supports freeSolo mode, allowing users to input custom keys not present in the predefined list.
Changelog
  • packages/components/nodes/agentflow/CustomFunction/CustomFunction.ts
    • Updated the internal comment to indicate that Custom Functions can now return either a string or a JSON object.
    • Incremented the node version from 1.1 to 1.2.
    • Added the freeSolo: true property to the 'Key' input field for runtime state keys, allowing custom input.
    • Removed the automatic JSON.stringify() conversion for object returns, preserving the original object.
    • Introduced explicit JSON.stringify() for objects when updating flow state via processTemplateVariables to ensure consistent state handling.
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
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 correctly addresses the issue of preserving object return types from Custom Functions in Agentflow. The implementation is clean, removing the unconditional stringification for the node's output while ensuring that dependent logic like state processing continues to function correctly by providing it with a stringified version. I've identified one minor edge case regarding null return values that introduces a subtle change in behavior and have provided a suggestion to align it with the previous implementation. Overall, this is a great enhancement that improves the flexibility of Custom Functions.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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.

[FEATURE] Allow Custom JS Function nodes to return and reference JSON objects

1 participant