Repository navigation
fix(memory): split mixed-script search queries in InMemoryMemoryService - #8019
Closed
AtulJoshi1206 wants to merge 1 commit into
Closed
AtulJoshi1206 wants to merge 1 commit into
AtulJoshi1206 wants to merge 1 commit into
Conversation
InMemoryMemoryService splits a stored event's mixed-script \w+ token into its Latin and non-Latin runs, so 私はPythonを使う is indexed under "python". The query never got the same treatment. search_memory tokenized it with _extract_words_lower, which keeps Pythonについて教えて as a single token, and a non-ASCII token can only match as a substring of the event text. So a Chinese or Japanese question that names an English term found nothing in an English memory about it. preload_memory searches with the user's whole message, which has exactly this shape for CJK users, and InMemoryMemoryService is the default for InMemoryRunner, adk web and adk run. SqliteMemoryService and MongoDbMemoryService already put queries and stored text through the same tokenizer. Run the query through _extract_searchable_words as well, so both sides share one tokenizer. Split each token into script runs before lowercasing, as the SQLite and MongoDB services do: "İ".lower() is "i" plus U+0307, which is not Latin, so splitting afterwards leaves a stray "i". On the query side that would make "İstanbul旅行" match every English memory that contains "I". Pure-ASCII queries tokenize exactly as before, and a query that matched an event before still matches it.
its-amann
reviewed
Nov 7, 2026
| words.add(''.join(group)) | ||
| # Split before lowercasing: "İ".lower() is "i" plus U+0307, which is | ||
| # not Latin, so splitting afterwards yields a stray "i" word. | ||
| words.update( |
Contributor
There was a problem hiding this comment.
if a non ascii token has an underscore, the split gives a bare "" word. since queries go through this now, naïve_bayes matches any japanese memory with something like my_var in it, because both sides have "". maybe skip runs that are only underscores?
Suggested change
| words.update( | |
| for _, group in itertools.groupby(word, _is_latin): | |
| run = ''.join(group) | |
| if run.strip('_'): | |
| words.add(run.lower()) |
copybara-service Bot
pushed a commit
that referenced
this pull request
Nov 9, 2026
Merge #8019 search_memory now tokenizes the query with the same function as stored events, so a Latin word inside Chinese or Japanese query text, such as Python in Pythonについて教えて, matches a memory containing that word. A mixed-script query token also counts its single-script runs, so such a query can rank an event slightly higher than before. Runs split out of a query word match whole words only, never as substrings. PiperOrigin-RevId: 1017511658
Collaborator
|
Thank you @AtulJoshi1206 for your contribution! 🎉 Your changes have been successfully imported and merged via Copybara in commit e9f9790. Closing this PR as the changes are now in the main branch. |
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
1. Link to an existing issue (if applicable):
2. Or, if no issue exists, describe the change:
Problem:
InMemoryMemoryServicesplits a stored event's mixed-script token into its Latin and non-Latin runs (#6773), so私はPythonを使うis indexed underpython. The query never got the same split.search_memorystill tokenizes it with_extract_words_lower, soPythonについて教えてstays one token, and a non-ASCII token can only match as a substring of the event text. A Chinese or Japanese question that names an English term finds nothing in an English memory about it:preload_memoryhits this directly, because it searches with the user's whole message, which has exactly this shape for CJK users.InMemoryMemoryServiceis also the default forInMemoryRunner,adk webandadk run.SqliteMemoryServiceandMongoDbMemoryServicealready run queries and stored text through the same tokenizer.Solution:
search_memorynow tokenizes the query with_extract_searchable_words, the function already used for stored events._extract_words_lowerhad no other callers, so I removed it._extract_searchable_wordsnow splits a token into script runs before lowercasing, as the SQLite and MongoDB services do."İ".lower()isiplus U+0307, which is not Latin, so splitting after lowercasing leaves a strayi. On the query side that would makeİstanbul旅行match every English memory containing "I". One of the new cases covers this and fails if only the query line is changed.Pure-ASCII queries tokenize exactly as before, and a query that matched an event before still matches it. The one ranking change is that a mixed-script query token now also counts its runs, so an exact mixed-script match ranks a bit higher. The MongoDB service already counts keyword overlap this way.
Testing Plan
Unit Tests:
I added six cases to
test_search_memory_non_latin:I write my backend in Python.Pythonについて教えてI write my backend in Python.我用Python写代码I visited İstanbul last year.İstanbul旅行I write my backend in Python.Javaについて教えてI write my backend in Python.thonについて教えてI like cats.İstanbul旅行i)pyinkandisortare clean.Manual End-to-End (E2E) Tests:
I ran the queries from the Problem section against
InMemoryMemoryServiceandSqliteMemoryServiceside by side. With this change both return 1 for every query.Checklist
Additional context
I checked the other memory services for the same mismatch:
SqliteMemoryServiceuses_split_script_runson both sides.MongoDbMemoryServiceuses_extract_keywordson both sides.All are already consistent.