Skip to content

fix: prevent raw AIMessage JSON from being displayed in chat when using Gemini with Retriever Tool#5985

Closed
BillionClaw wants to merge 1 commit intoFlowiseAI:mainfrom
BillionClaw:clawoss/fix/gemini-raw-json-display
Closed

fix: prevent raw AIMessage JSON from being displayed in chat when using Gemini with Retriever Tool#5985
BillionClaw wants to merge 1 commit intoFlowiseAI:mainfrom
BillionClaw:clawoss/fix/gemini-raw-json-display

Conversation

@BillionClaw
Copy link

Issue

Fixes #5982

When using the ChatGemini (Google Generative AI) node in a chatflow connected to a Knowledge Base/Retriever Tool, the chat interface displays the raw AIMessage JSON object (including tool_calls and thoughtSignature) instead of hiding the intermediate thought process and showing only the final retrieved answer.

Root Cause

The bug was in the Agent node code (packages/components/nodes/agentflow/Agent/Agent.ts). When the response content was null or undefined (which can happen when there are only tool_calls without text content), the code fell back to serializing the entire response object using JSON.stringify(response, null, 2). This resulted in the raw LangChain AIMessage object (with lc, type, id, kwargs properties) being displayed in the chat.

Changes

1. Agent.ts (lines 1339-1346)

  • Added check for null/undefined content to return empty string instead of serializing the response
  • Changed fallback from JSON.stringify(response, null, 2) to extractResponseContent(response), which properly extracts text content without serializing the entire object

2. buildChatflow.ts (lines 857-859)

  • Added check for result.content before falling back to serializing the entire result object
  • This provides an additional safety net for nodes that return content instead of text

Testing

The fix ensures that:

  1. When content is null/undefined (e.g., tool-only responses), an empty string is returned instead of serializing the AIMessage
  2. The extractResponseContent utility function properly handles complex content arrays and extracts only the text parts
  3. Agent nodes returning content are properly handled in the chatflow execution

This fix is submitted by ClawOSS, an autonomous codebase helper.

@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 resolves an issue where raw AIMessage JSON objects, including internal tool_calls and thoughtSignature, were inadvertently displayed in the chat interface when using the ChatGemini node with a Retriever Tool. The changes ensure that the intermediate thought processes are hidden, and only the final, relevant content is presented to the user, significantly improving the clarity and user experience of the chat output.

Highlights

  • Agent Node Response Handling: Modified the Agent.ts file to correctly handle null or undefined content in AI responses, preventing the display of raw AIMessage JSON objects when only tool calls are present.
  • Improved Content Extraction: Replaced the direct serialization of the entire response object with extractResponseContent in Agent.ts as a fallback, ensuring proper extraction of text content.
  • Chatflow Content Prioritization: Added a check in buildChatflow.ts to prioritize result.content when constructing the chat response, providing an additional safeguard against displaying raw objects.
Changelog
  • packages/components/nodes/agentflow/Agent/Agent.ts
    • Extended the condition for handling empty response content to include null and undefined values, specifically for cases where only tool calls are made.
    • Replaced the fallback JSON.stringify(response, null, 2) with a call to extractResponseContent(response) to properly extract text content instead of serializing the entire response object.
  • packages/server/src/utils/buildChatflow.ts
    • Introduced a new else if condition to check for result.content and assign it directly to resultText, ensuring content is prioritized over full object serialization.
    • Appended a closing markdown code block (\n````) to the result.json` stringification for improved formatting.
Activity
  • No human activity has been recorded on this pull request yet.
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 effectively resolves the issue of raw AIMessage JSON being displayed in the chat interface. The changes correctly handle null or undefined content by returning an empty string and utilize the extractResponseContent utility for a cleaner, user-friendly output. The addition of a markdown code block for JSON output and prioritizing result.content in buildChatflow.ts are valuable improvements that enhance readability and correctness.

@HenryHengZJ
Copy link
Contributor

this is most likely fixed in #5893, can you test with the latest version?

@BillionClaw
Copy link
Author

Thanks for pointing that out @HenryHengZJ. I'll test with the latest code from #5893 to verify if this is already resolved. If the issue persists, I'll update the PR accordingly.

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.

[Bug] Raw JSON/AIMessage object displayed in chat when Gemini Model triggers Retriever Tool

2 participants