From 3d972ccba8a00a274f7dc3805625954a9e2a51ce Mon Sep 17 00:00:00 2001 From: Vishal Bala Date: Wed, 14 Oct 2026 11:30:49 +0200 Subject: [PATCH] fix(message-history): fall back within the requested session in get_relevant() get_relevant(session_tag=..., fall_back=True) searched the requested session but, when nothing was relevant, fell back to get_recent() without the session_tag. It therefore returned the recent messages of the instance's default session, letting a lookup for one conversation surface another's. The fall-back now passes session_tag through, so it reads the same session the search did. SemanticSessionManager inherits the fix; MessageHistory has no fall_back and is unaffected. The new integration test stores one exchange in the default session and one in another, then asks for the other session with distance_threshold=0 so the fall-back fires. On main it returns the default session's exchange; with this change it returns the requested one. --- .../message_history/semantic_history.py | 8 +++++++- tests/integration/test_message_history.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/redisvl/extensions/message_history/semantic_history.py b/redisvl/extensions/message_history/semantic_history.py index 41004f5f..dc02c70b 100644 --- a/redisvl/extensions/message_history/semantic_history.py +++ b/redisvl/extensions/message_history/semantic_history.py @@ -330,7 +330,13 @@ def get_relevant( # if we don't find semantic matches fallback to returning recent context if not messages and fall_back: - return self.get_recent(as_text=as_text, top_k=top_k, raw=raw, role=role) + return self.get_recent( + as_text=as_text, + top_k=top_k, + raw=raw, + session_tag=session_tag, + role=role, + ) if raw: return messages return self._format_context(messages, as_text) diff --git a/tests/integration/test_message_history.py b/tests/integration/test_message_history.py index 9bf09383..89f00f39 100644 --- a/tests/integration/test_message_history.py +++ b/tests/integration/test_message_history.py @@ -411,6 +411,22 @@ def test_semantic_scope(semantic_history): assert no_context == [] +@pytest.mark.requires_hf +def test_semantic_get_relevant_falls_back_within_the_requested_session( + semantic_history, +): + semantic_history.store("default prompt", "default response") + semantic_history.store("other prompt", "other response", session_tag="other") + + context = semantic_history.get_relevant( + "unrelated", session_tag="other", fall_back=True, distance_threshold=0 + ) + assert context == [ + {"role": "user", "content": "other prompt"}, + {"role": "llm", "content": "other response"}, + ] + + @pytest.mark.requires_hf def test_semantic_store_and_get_recent(semantic_history): context = semantic_history.get_recent()