Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Dec 4, 2025

fix: use simple dict filters for local mem0 Memory instead of AND/OR format

Summary

Fixes #4030

The local mem0 Memory class expects simple dict filters like {"user_id": "bob"}, but the code was passing {"AND": [{"user_id": "bob"}]}. This caused the mem0 library to incorrectly convert the filter to a RediSearch query like @AND:{[{'user_id': 'bob'}]} @user_id:{bob}, resulting in the error: Invalid filter expression: AND is not indexed as a tag field.

Changes:

  • Added for_local_memory parameter to _create_filter_for_search() method
  • When using local Memory (OSS), returns simple dict filters: {"user_id": "bob"}
  • When using MemoryClient (cloud API), preserves existing AND/OR format: {"AND": [{"user_id": "bob"}]}
  • Updated search() method to detect Memory type and use appropriate filter format

Review & Testing Checklist for Human

  • Verify with real valkey/redis: Tests use mocked Memory instances. Please test with an actual valkey/redis setup using the reproduction steps from issue [BUG] using external memory with mem0 and valkey (or redis) fails #4030 to confirm the fix works end-to-end
  • Check docstring accuracy: The docstring at line 84-85 says "only user_id is included" when both are present, but the code actually includes both user_id and agent_id as an implicit AND. Verify this is the intended behavior
  • Verify MemoryClient still works: The AND/OR filter format is preserved for MemoryClient - confirm cloud API users are not affected

Recommended Test Plan

  1. Follow the reproduction steps from issue [BUG] using external memory with mem0 and valkey (or redis) fails #4030 with a local valkey/redis instance
  2. Run crewai run with external memory configured with local_mem0_config and user_id
  3. Verify no filter expression errors occur during memory search

Notes

…format

Fixes #4030

The local mem0 Memory class expects simple dict filters like
{'user_id': 'bob'} instead of {'AND': [{'user_id': 'bob'}]}.

The AND/OR filter format was being incorrectly converted to a
RediSearch query like '@and:{[{"user_id": "bob"}]} @user_id:{bob}'
which caused the error 'Invalid filter expression'.

This change:
- Adds a for_local_memory parameter to _create_filter_for_search()
- Returns simple dict filters for local Memory instances
- Keeps AND/OR format for MemoryClient (cloud API)
- Adds tests covering the issue scenario with valkey/redis

Co-Authored-By: João <[email protected]>
@devin-ai-integration
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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] using external memory with mem0 and valkey (or redis) fails

1 participant