Repository navigation
Python: fix(redis): escape RediSearch query syntax in memory search text - #10039
Fish (wxhking) wants to merge 1 commit into
Conversation
RedisContextProvider passed the user's message straight into TextQuery / AggregateHybridQuery as the search text. RediSearch reads several punctuation marks as query syntax there — '|' unions terms, '?' is a fuzzy marker, backticks quote exact phrases — so ordinary messages like 'ps aux | grep python' or 'Why ?' reached the engine as malformed grammar (e.g. '@content:(ps | aux | | | grep | python)') and raised a syntax error that failed the whole agent run; the model was never called. Escape the search text with a TokenEscaper configured for the RediSearch syntax characters before building either query. Spaces stay unescaped, so the terms keep tokenizing and BM25 scoring works as before. Tag filter values were already escaped through _vector_store's escaper; only the free-text path was exposed. Fixes microsoft#10002
|
Fish (@wxhking) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟡 Changes recommended
Escaping the text before embedding makes hybrid search use different embedding input from stored content, potentially changing retrieval rankings; preserve the original embedding input and add a regression assertion.
1 open issue
What changed in this PR
Addresses #10002 by escaping RediSearch syntax in user messages used for Redis memory retrieval, aiming to prevent punctuation from causing syntax errors that abort agent runs.
Changes:
- Adds a search-text escaper shared by text and hybrid queries, leaving spaces unescaped.
- Adds regression tests for punctuation-heavy messages and escaped text passed to hybrid queries.
| File | Description |
|---|---|
python/packages/redis/tests/test_context_provider_edges.py |
Adds text-query escaping cases and a hybrid-query regression test. |
python/packages/redis/agent_framework_redis/_context_provider.py |
Escapes search text before constructing Redis queries. |
Review effort: Balanced
| self._validate_filters() | ||
|
|
||
| q = (text or "").strip() | ||
| q = _SEARCH_TEXT_ESCAPER.escape((text or "").strip()) |

Fixes #10002
Summary
RedisContextProvider._redis_searchpassed the user's message straight intoTextQuery/AggregateHybridQueryas the search text. RediSearch reads several punctuation marks as query syntax there —|unions terms,?is a fuzzy marker, backticks quote exact phrases — so ordinary messages likeps aux | grep python,Why ?or a markdown table reached the engine as malformed grammar (e.g.@content:(ps | aux | | | grep | python)) and raisedIntegrationInvalidRequestException: Redis text search failed: ... Syntax error, failing the whole agent run before the model was ever called.The search text is plain prose, not a query: escape the RediSearch syntax characters with a
TokenEscaperconfigured for them before building either query._vector_store's escaper; only the free-text path was exposedTextQuery(text='ps aux | grep python').query_string()yields'@content:(ps | aux | | | grep | python)'before the fix; after it the terms are escaped and the broken| |shape is goneTest plan
python/packages/redis/tests/test_context_provider_edges.py: all five failing messages from the issue are asserted to produce an escaped query string (no unescaped| ?/| |shapes, escaped chars present), for both the text and hybrid-vector paths — 7 new casespytest tests -m 'not integration'inpython/packages/redis— 337 passed, 58 deselected (integration), no new failures