Skip to content

Python: extract keywords from non-English text for topic selection#7130

Open
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/memory-non-english-keywords
Open

Python: extract keywords from non-English text for topic selection#7130
he-yufeng wants to merge 1 commit into
microsoft:mainfrom
he-yufeng:fix/memory-non-english-keywords

Conversation

@he-yufeng

Copy link
Copy Markdown
Contributor

Motivation and Context

_WORD_PATTERN in _harness/_memory.py was re.compile(r"[a-z0-9][a-z0-9_-]{1,}", flags=re.IGNORECASE) — ASCII-only. _extract_keywords runs it over the input messages, and _select_topics returns early when the keyword set is empty. So a message written in CJK, Cyrillic, or any other non-Latin script yields no keywords, and non-English users never get their memory topic files auto-loaded.

Description

Make the pattern Unicode-aware: re.compile(r"[^\W_][\w-]+"). [^\W_] is a Unicode letter or digit (excluding underscore) and [\w-] is a word char or hyphen, so this is the exact Unicode generalization of the old pattern — English tokenization is byte-for-byte unchanged, and CJK/Cyrillic/etc. text now produces keywords. Verified the invariant directly:

'hello world'        -> ['hello', 'world']      (unchanged)
'foo-bar_baz'        -> ['foo-bar_baz']          (unchanged)
'GPT-4 model'        -> ['GPT-4', 'model']       (unchanged)
'こんにちは 元気ですか'  -> ['こんにちは', '元気ですか']   (was [])
'привет мир друзья'  -> ['привет', 'мир', 'друзья'] (was [])
'a I x' / '!!! ???'  -> []                        (single chars / punctuation still skipped)

Related Issue

Fixes #6989

Contribution Checklist

  • Tests pass locally (pytest test_harness_memory.py -k non_english, ruff format + check clean)
  • Added a test proving non-English keyword extraction (and unchanged English extraction)
  • Linked to an issue with a closing keyword
  • Change is focused and touches no unrelated code

_WORD_PATTERN matched only ASCII (`[a-z0-9]...`), so a message written in
CJK, Cyrillic or any other non-Latin script produced an empty keyword set.
_select_topics returns early on an empty keyword set, so non-English users
never had memory topic files loaded automatically.

Make the pattern Unicode-aware (`[^\W_][\w-]+`, a letter/digit start plus
word chars/hyphen), which is the exact Unicode generalization of the old
pattern: English tokenization is unchanged and CJK/Cyrillic text now yields
keywords.
Copilot AI review requested due to automatic review settings July 15, 2026 11:18

Copilot AI left a comment

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.

Pull request overview

This PR fixes memory topic auto-loading for non-English user messages by making keyword extraction Unicode-aware in the Python harness memory subsystem.

Changes:

  • Update _WORD_PATTERN to match 2+ character “word-like” tokens across Unicode scripts (CJK/Cyrillic/etc.), preventing empty keyword sets for non-Latin input.
  • Add a regression test ensuring non-English keyword extraction works and that basic English extraction remains unchanged.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/core/agent_framework/_harness/_memory.py Replaces the ASCII-only keyword regex with a Unicode-aware pattern so non-English messages produce keywords for topic selection.
python/packages/core/tests/core/test_harness_memory.py Adds a regression test covering CJK and Cyrillic keyword extraction plus a simple English invariance check.

@rogerbarreto rogerbarreto added the python Usage: [Issues, PRs], Target: Python label Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: non-English users (input messages) will never automatically load topic files.

3 participants