Skip to content

fix(memory): split mixed-script search queries in InMemoryMemoryService - #8019

Closed
AtulJoshi1206 wants to merge 1 commit into
google:mainfrom
AtulJoshi1206:fix/memory-query-script-runs
Closed

AtulJoshi1206 wants to merge 1 commit into
google:mainfrom
AtulJoshi1206:fix/memory-query-script-runs

Conversation

@AtulJoshi1206

Copy link
Copy Markdown
Contributor

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:

InMemoryMemoryService splits a stored event's mixed-script token into its Latin and non-Latin runs (#6773), so 私はPythonを使う is indexed under python. The query never got the same split. search_memory still tokenizes it with _extract_words_lower, so Pythonについて教えて 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:

# memory: "I write my backend in Python."
query                    in-memory (main)   sqlite
'Python'                 1                  1
'Pythonについて教えて'     0                  1
'我用Python写代码'         0                  1

preload_memory hits this directly, because it searches with the user's whole message, which has exactly this shape for CJK users. InMemoryMemoryService is also the default for InMemoryRunner, adk web and adk run. SqliteMemoryService and MongoDbMemoryService already run queries and stored text through the same tokenizer.

Solution:

  • Same tokenizer for queries: search_memory now tokenizes the query with _extract_searchable_words, the function already used for stored events. _extract_words_lower had no other callers, so I removed it.
  • Split before lowercasing: _extract_searchable_words now splits a 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 after lowercasing leaves a stray i. 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 have added or updated unit tests for my change.
  • All unit tests pass locally.

I added six cases to test_search_memory_non_latin:

memory query expected
I write my backend in Python. Pythonについて教えて 1 (fails on main)
I write my backend in Python. 我用Python写代码 1 (fails on main)
I visited İstanbul last year. İstanbul旅行 1 (fails on main)
I write my backend in Python. Javaについて教えて 0
I write my backend in Python. thonについて教えて 0 (a partial word still doesn't match)
I like cats. İstanbul旅行 0 (no stray i)
# main, with the new cases
3 failed, 27 passed   (the three "fails on main" cases)

# this branch
tests/unittests/memory + tools/test_preload_memory_tool.py + tools/test_load_memory_tool.py
253 passed

pyink and isort are clean.

Manual End-to-End (E2E) Tests:

I ran the queries from the Problem section against InMemoryMemoryService and SqliteMemoryService side by side. With this change both return 1 for every query.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules. (none)

Additional context

I checked the other memory services for the same mismatch:

  • SqliteMemoryService uses _split_script_runs on both sides.
  • MongoDbMemoryService uses _extract_keywords on both sides.
  • The Vertex AI services search server-side.

All are already consistent.

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.
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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@adk-bot

adk-bot commented Nov 9, 2026

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged [Status] This PR is merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants