fix(mem0): build v2-compatible search filters for mixed entity IDs#835
fix(mem0): build v2-compatible search filters for mixed entity IDs#835liuxiaopai-ai wants to merge 1 commit intoagentscope-ai:mainfrom
Conversation
|
root seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
Summary of ChangesHello @liuxiaopai-ai, 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 compatibility issues with Mem0 v2 API search filters by refactoring the filter construction logic within Highlights
Changelog
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses an issue with Mem0 search filter generation by implementing OR logic for multiple entity IDs and properly nesting custom metadata. The logic is sound and well-tested. I've provided a couple of suggestions to further improve code clarity and remove a minor redundancy.
| Mem0SearchRequest request = | ||
| Mem0SearchRequest.builder().query(query).userId(userId).topK(5).build(); |
There was a problem hiding this comment.
The .userId(userId) call here is redundant. The Mem0SearchRequest.Builder::userId method does modify the request's filters, but this is immediately overwritten by the request.setFilters(buildSearchFilters()) call on the next line. The new buildSearchFilters() method already handles the userId correctly. Removing this redundant call will make the code cleaner and prevent potential confusion.
| Mem0SearchRequest request = | |
| Mem0SearchRequest.builder().query(query).userId(userId).topK(5).build(); | |
| Mem0SearchRequest request = | |
| Mem0SearchRequest.builder().query(query).topK(5).build(); |
| if (entityExpression != null && metadataExpression != null) { | ||
| List<Map<String, Object>> andExpressions = new ArrayList<>(); | ||
| andExpressions.add(entityExpression); | ||
| andExpressions.add(metadataExpression); | ||
| return Map.of("AND", andExpressions); | ||
| } | ||
|
|
||
| if (entityExpression != null) { | ||
| return entityExpression; | ||
| } | ||
|
|
||
| if (metadataExpression != null) { | ||
| return metadataExpression; | ||
| } | ||
|
|
||
| return builder.build(); | ||
| return new HashMap<>(); |
There was a problem hiding this comment.
The logic for combining entityExpression and metadataExpression can be simplified for better readability and maintainability. Instead of multiple if statements to handle the different combinations, you can collect all non-null expressions into a list and then decide how to combine them. This approach is more concise and easier to extend if more filter expressions are added in the future.
List<Map<String, Object>> finalExpressions = new ArrayList<>();
if (entityExpression != null) {
finalExpressions.add(entityExpression);
}
if (metadataExpression != null) {
finalExpressions.add(metadataExpression);
}
if (finalExpressions.isEmpty()) {
return new HashMap<>();
}
if (finalExpressions.size() == 1) {
return finalExpressions.get(0);
}
return Map.of("AND", finalExpressions);
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
PTAL @shiyiyue1102 |

Summary
Fixes #823 by generating Mem0 search filters that match v2 syntax and expected matching semantics:
user_id,agent_id,run_id) instead of implicit top-level AND.metadatafield (instead of flattening metadata keys at filter root).Why
Issue #823 reported two problems in current request building:
user_id+agent_idwere serialized as sibling filter keys (implicit AND), which can miss records when memory belongs to only one entity dimension.Implementation notes
Mem0LongTermMemory.buildSearchRequestto build filters through a dedicated helper:ORexpression.{ "metadata": { ... } }.AND(entityExpr, metadataExpr)when both exist.Tests
Added request-shape assertions in
Mem0LongTermMemoryTest:testRetrieveUsesOrForMultipleEntityFilterstestRetrieveNestsMetadataUnderMetadataFilterFieldValidation performed:
mvn -pl agentscope-extensions/agentscope-extensions-mem0 -am \ -DskipITs -DskipIntegrationTests \ test -Dtest=Mem0LongTermMemoryTest,Mem0SearchRequestTest,Mem0ClientTestResult:
Tests run: 74, Failures: 0, Errors: 0, Skipped: 0