fix(vector-stores): add agent_id and run_id to Elasticsearch/OpenSearch default mappings#4906
Merged
kartik-mem0 merged 1 commit intomem0ai:mainfrom Apr 20, 2026
Conversation
…ch default mappings Without explicit keyword mappings for agent_id and run_id, Elasticsearch and OpenSearch dynamically infer types when the first document is indexed. This causes filter queries on metadata.agent_id and metadata.run_id to return empty results or raise a TypeError because the inferred type does not match the expected keyword term query. mem0 scopes memories by user_id, agent_id, and run_id. All three fields must be declared as keyword in the index mapping so that term filters work reliably from the first insert. Fixes mem0ai#3744 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
kartik-mem0
approved these changes
Apr 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked Issue
Closes #3744
Root Cause
create_index()in bothElasticsearchDBandOpenSearchDBonly declareduser_idas akeywordin the metadata mapping:mem0 scopes memories by three canonical entity IDs:
user_id,agent_id, andrun_id(seememory/main.py:292–297). When a memory withagent_idorrun_idis inserted first, both Elasticsearch and OpenSearch dynamically infer a type for those fields from the first value seen. If the inferred type differs fromkeyword(e.g. ES may inferobjectwhen an enum-style value is encountered), subsequenttermfilter queries onmetadata.agent_idormetadata.run_idsilently return empty results or raise aTypeError.Fix
Explicitly declare
agent_idandrun_idaskeywordin the mapping for both stores, matching the existinguser_iddeclaration:Files Changed
mem0/vector_stores/elasticsearch.pyagent_id,run_idkeyword fields tocreate_indexmappingmem0/vector_stores/opensearch.pytests/vector_stores/test_elasticsearch.pykeywordtests/vector_stores/test_opensearch.pyNotes
PUT mappingallows adding new explicit fields to an existing dynamic mapping without reindexing.results#3744 for Elasticsearch; both are fixed together to keep the two backends consistent.