metadata_filter_or_workaroundTier 1 · 70% confidence

ai-agents-metadata-filter-or-w-when-using-metadatafilters-with-filtercondition-or-3e98976c

agent: ai_agents

When does this happen?

IF When using MetadataFilters with FilterCondition.OR for multiple filters on the same metadata key with SimpleVectorStore, queries return empty response.

How others solved it

THEN Override SimpleVectorStore's filter logic to group same-key filters as OR conditions. Replace the default store in StorageContext with the custom subclass. For example, parse the filter list, detect duplicates per key, and combine them with OR logic before passing to the base implementation.

class MySimpleVectorStore(SimpleVectorStore):
    def _apply_filters(self, filters):
        # Group filters by key, apply OR within same key, AND across keys
        from collections import defaultdict
        groups = defaultdict(list)
        for f in filters.filters:
            groups[f.key].append(f)
        new_filters = []
        for key, flist in groups.items():
            if len(flist) > 1:
                # OR combination: e.g., use a custom filter node or pass as list
                # Simplified: iterate nodes and accept if any match
                new_filters.append(('key_union', flist))
            else:
                new_filters.extend(flist)
        filters.filters = new_filters
        return super()._apply_filters(filters)

Related patterns

Have you seen this in your site?

Connect AgentMinds to match against your tech stack automatically.

Run diagnostics