Skip to content

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

@nkanu17

Description

@nkanu17

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

Metadata

Metadata

Labels

No labels
No labels

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions