Skip to content

fix: skip empty metadata filter in RetrieverTool to prevent vector store error#5976

Open
majiayu000 wants to merge 2 commits intoFlowiseAI:mainfrom
majiayu000:bugfix/issue-4900-retriever-tool-empty-filter
Open

fix: skip empty metadata filter in RetrieverTool to prevent vector store error#5976
majiayu000 wants to merge 2 commits intoFlowiseAI:mainfrom
majiayu000:bugfix/issue-4900-retriever-tool-empty-filter

Conversation

@majiayu000
Copy link

Summary

Fixes #4900

When a user adds an "Additional Metadata Filter" in the Retriever Tool node then removes it, the empty filter object {} was still passed to the vector store. This caused Pinecone (and potentially other vector stores) to reject the query with:

"You must enter a filter object with at least one key-value pair."

The fix adds a guard to check that the resolved metadata filter has at least one key-value pair before assigning it to vectorStore.filter. This follows the same pattern already used in Pinecone_LlamaIndex.ts.

Changes

  • packages/components/nodes/tools/RetrieverTool/RetrieverTool.ts: Add Object.keys().length > 0 check before setting vectorStore.filter
  • packages/components/nodes/tools/RetrieverTool/RetrieverTool.test.ts: Add 6 tests covering empty filter, non-empty filter, null filter, and end-to-end string-to-object parsing scenarios

Test plan

  • New unit tests pass (6 test cases)
  • Existing tests unaffected
  • Verified empty {} filter is not passed to vector store
  • Verified non-empty filter {"category": "docs"} is correctly applied
  • Verified null/undefined filter is handled safely

…ore error

When a user adds an "Additional Metadata Filter" then removes it, the
empty filter object {} was still passed to the vector store, causing
Pinecone (and potentially others) to reject it with "You must enter a
filter object with at least one key-value pair." Now checks that the
resolved filter has at least one key before assigning it.

Fixes FlowiseAI#4900

Signed-off-by: majiayu000 <1835304752@qq.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 addresses a critical bug in the Retriever Tool where an empty metadata filter could lead to errors in vector store operations. By introducing a robust check for non-empty filter objects, the change ensures that only valid filters are applied, thereby enhancing the stability and reliability of interactions with vector store APIs and improving the overall user experience when configuring retrieval tools.

Highlights

  • Bug Fix: Resolved an issue where an empty metadata filter, resulting from a user adding and then removing an 'Additional Metadata Filter' in the Retriever Tool node, was incorrectly passed to vector stores.
  • Error Prevention: Implemented a guard condition to prevent vector stores (like Pinecone) from rejecting queries due to empty filter objects, which previously caused errors.
  • Code Logic: Added a check to ensure that the resolved metadata filter has at least one key-value pair (Object.keys(resolved).length > 0) before it is assigned to vectorStore.filter.
  • Test Coverage: Introduced six new unit tests to cover various scenarios, including empty filters, non-empty filters, null filters, and end-to-end string-to-object parsing for metadata filters.
Changelog
  • packages/components/nodes/tools/RetrieverTool/RetrieverTool.test.ts
    • Added new unit tests to validate the handling of metadata filters, specifically covering cases for empty, non-empty, and null filters, as well as end-to-end parsing scenarios to confirm the new guard logic functions correctly.
  • packages/components/nodes/tools/RetrieverTool/RetrieverTool.ts
    • Added a conditional check to prevent empty metadata filter objects from being passed to the vector store, ensuring that vectorStore.filter is only set with valid, non-empty filters.
Activity
  • Fixed issue Retriever tool will throw error if "additional metadata filter" is added then removed #4900, which reported errors with empty metadata filters in the Retriever Tool.
  • New unit tests were added to validate the fix, covering 6 distinct test cases.
  • All new unit tests passed successfully.
  • Existing tests were confirmed to be unaffected by the changes.
  • Verified that empty {} filters are no longer passed to the vector store.
  • Verified that non-empty filters like {"category": "docs"} are correctly applied.
  • Verified that null/undefined filters are handled safely without causing issues.
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 addresses an issue where an empty metadata filter object could be passed to the vector store in RetrieverTool, causing errors. The fix introduces a guard to prevent this, and new unit tests have been added to cover this change.

A similar pattern of assigning a metadata filter without checking if it's an empty object appears to exist in packages/components/nodes/vectorstores/Pinecone/Pinecone.ts on lines 254-258. This could lead to the same error you're fixing here. Consider applying a similar guard in that file as well to improve robustness.

Comment on lines +86 to +90
const metadatafilter =
typeof retrieverToolMetadataFilter === 'object'
? retrieverToolMetadataFilter
: JSON.parse(retrieverToolMetadataFilter)
const newMetadataFilter = resolveFlowObjValue(metadatafilter, {})
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

For consistency with the camelCase naming convention used elsewhere in this file and the codebase (e.g., retrieverToolMetadataFilter, newMetadataFilter), please rename metadatafilter to metadataFilter.

Suggested change
const metadatafilter =
typeof retrieverToolMetadataFilter === 'object'
? retrieverToolMetadataFilter
: JSON.parse(retrieverToolMetadataFilter)
const newMetadataFilter = resolveFlowObjValue(metadatafilter, {})
const metadataFilter =
typeof retrieverToolMetadataFilter === 'object'
? retrieverToolMetadataFilter
: JSON.parse(retrieverToolMetadataFilter)
const newMetadataFilter = resolveFlowObjValue(metadataFilter, {})

Copy link
Author

Choose a reason for hiding this comment

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

Fixed. Renamed metadatafilter to metadataFilter in both the test file and the source file for camelCase consistency.

Signed-off-by: majiayu000 <1835304752@qq.com>
@majiayu000
Copy link
Author

Thanks for the suggestion about Pinecone.ts! You're right that lines 254-258 have the same pattern. However, the Pinecone case is slightly different — the filter is used as part of PineconeStoreParams during store initialization (not at query time), and there's follow-up logic on lines 259-266 that may append $or conditions to obj.filter. Adding an empty-check guard there could affect that downstream logic.

I think it's better to address that in a separate PR with its own testing to avoid unintended side effects. I'll open a follow-up issue for it.

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.

Retriever tool will throw error if "additional metadata filter" is added then removed

1 participant