Skip to content

Python: fix(redis): let an injected redis_client take precedence in RedisHistoryProvider - #10041

Closed
Fish (wxhking) wants to merge 2 commits into
microsoft:mainfrom
wxhking:fix/history-provider-client-precedence
Closed

Fish (wxhking) wants to merge 2 commits into
microsoft:mainfrom
wxhking:fix/history-provider-client-precedence

Conversation

@wxhking

Copy link
Copy Markdown
Contributor

Fixes #9966

Summary

RedisHistoryProvider treated redis_client, redis_url and credential_provider as strictly mutually exclusive, so a working caller-owned client passed together with a stray redis_url (or credential provider) raised ValueError: redis_client, redis_url, and credential_provider are mutually exclusive at construction instead of using the injected client.

The Redis vector-store constructors already follow the intended behavior: they bypass URL, credential and host settings whenever redis_client is supplied. This gives the history provider the same precedence:

  • the mutual-exclusion check only rejects multiple non-client connection sources
  • the host is required check only applies when the provider will build a client itself
  • the injected client stays caller-owned (_owns_client=False) and nothing about it is validated beyond the existing type / decode_responses checks
  • provider-level storage settings (key scope, retention) keep applying
  • docstrings and the Raises block updated to state the precedence

Test plan

  • new test_borrowed_client_takes_precedence_over_other_connection_sources (parametrized over redis_url / credential_provider+host): the injected client is used and no mutual-exclusion error is raised — replaces the test that pinned the old rejection
  • test_url_and_credential_stay_mutually_exclusive_without_client keeps the no-client case covered (already existed as test_both_url_and_credential_raises)
  • pytest tests -m 'not integration' in python/packages/redis — 337 passed, 58 deselected (integration), no new failures
  • ruff check / format clean on both changed files

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
…oryProvider

RedisHistoryProvider treated redis_client, redis_url and credential_provider
as strictly mutually exclusive, so a working caller-owned client passed
together with a stray redis_url (or credential provider) raised at
construction instead of using the injected client. The Redis vector-store
constructors already follow the intended behavior: they bypass URL and
.env connection settings whenever redis_client is supplied.

Give redis_client the same precedence in the history provider: the
mutual-exclusion and host checks only apply to provider-created clients,
so the injected client stays caller-owned and the other settings are
never validated.

Fixes microsoft#9966
Copilot AI balanced review requested due to automatic review settings November 3, 2026 13:47
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 3, 2026
@github-actions github-actions Bot changed the title fix(redis): let an injected redis_client take precedence in RedisHistoryProvider Python: fix(redis): let an injected redis_client take precedence in RedisHistoryProvider Nov 3, 2026

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.

🟡 Changes recommended

Query escaping also alters the text passed to aembed(), potentially changing hybrid rankings; embeddings should use the original stripped text, with regression coverage asserting that input.

1 open issue
What changed in this PR

This PR aligns RedisHistoryProvider with Redis vector-store behavior by prioritizing caller-owned clients, addressing #9966. It also adds query-syntax escaping to Redis context searches.

Changes:

  • Prefer injected history clients over other connection settings while preserving ownership and compatibility checks.
  • Escape user text before constructing text and hybrid search queries.
  • Add regression tests for client precedence and query escaping.
File Description
python/​packages/​redis/​tests/​test_providers.py Tests injected-client precedence and caller ownership.
python/​packages/​redis/​tests/​test_context_provider_edges.py Tests punctuation escaping in text and hybrid searches.
python/​packages/​redis/​agent_framework_redis/​_history_provider.py Updates connection validation and documents client precedence.
python/​packages/​redis/​agent_framework_redis/​_context_provider.py Adds escaping for Redis search text.

Review effort: Balanced

self._validate_filters()

q = (text or "").strip()
q = _SEARCH_TEXT_ESCAPER.escape((text or "").strip())
@wxhking

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@wxhking

Copy link
Copy Markdown
Contributor Author

Closing: upstream #9977 (merged Nov 3) landed the same fix — an injected redis_client now takes precedence over redis_url/credential_provider, redundant connection settings are ignored rather than rejected, and self.redis_url reports the source actually used. The upstream version's test coverage supersedes this branch's, so nothing is left to add here. Thanks!

This branch was successfully deployed

No deployments
github-app-auth — 1727065e Deployed Nov 3, 2026 by wxhking via team_check #8565
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]: RedisHistoryProvider rejects connection settings instead of preferring an injected client

2 participants