redis / redis/agent-memory-server

`optimize_query` only applies to semantic search, consider supporting keyword and hybrid modes

Open
#252 1 comment 0 reactions 2 assignees View on GitHub

@vishal-bala is already working on this.

Since Mar 27, 2026.

Dominant language
Python
Stars
316
Forks
63
Avg merge
14d 23h
Merged PRs (30d)
1

Description

Current Behavior

optimize_query is currently only applied when search_mode == "semantic". For keyword and hybrid modes, it is silently ignored.

The gating logic is in long_term_memory.py:

# Optimize query only for semantic search; keyword and hybrid modes should
# preserve the literal text for lexical matching.
if optimize_query and text and search_mode == SearchModeEnum.SEMANTIC:
    search_query = await optimize_query_for_vector_search(text)

The current optimization uses an LLM to rewrite conversational queries into embedding-friendly form:

  • "tell me what I like to eat""user food preferences dietary likes dislikes favorite meals cuisine"

This is effective for vector similarity but would hurt BM25/keyword matching since the rewritten terms may not appear in stored text.

Why This Matters

  • Users setting optimize_query=True with search_mode="keyword" or "hybrid" get no optimization with no warning
  • Hybrid mode uses both vector and keyword components — the vector half could benefit from optimization while the keyword half uses the original text

Options to Consider

  1. Do nothing — current behavior is intentional and documented (docstrings now clarify this is semantic-only). Keyword search needs the original terms for lexical matching.

  2. Hybrid-aware optimization — for hybrid mode, optimize the query for the vector component but pass the original text to the keyword component. This would require search_memories to accept two query strings (one optimized for embedding, one original for BM25).

  3. Keyword-specific optimization — create a separate optimization path for keyword search (e.g., expanding synonyms, stripping filler words while keeping searchable terms) instead of the current vector-focused rewrite.

  4. Warn when ignored — log a warning or raise when optimize_query=True is passed with a non-semantic mode, so users know it's not being applied.

Related Files

  • agent_memory_server/long_term_memory.py — gating logic (line 1187)
  • agent_memory_server/llm/client.pyoptimize_query_for_vector_search()
  • agent_memory_server/config.pyquery_optimization_prompt_template

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.