Skip to content

fix(backend): AI Chat can't find conversations by speaker/participant names — hybrid keyword+vector search (#5072) - #7811

Merged
kodjima33 merged 4 commits into
mainfrom
watchdog/issue-5072-chat-name-search
Jun 11, 2026
Merged

kodjima33 merged 4 commits into
mainfrom
watchdog/issue-5072-chat-name-search

Conversation

@kodjima33

Copy link
Copy Markdown
Collaborator

Bug

#5072 — asking AI Chat "when did I talk to Steph?" returns nothing, even when the conversation exists with the name clearly in its summary. The user can see the conversation in the feed and the app's search bar would find it, but chat cannot.

Root cause

Both chat retrieval paths are vector-search-only:

  • search_conversations_tool (agentic chat) → vector_db.query_vectors (Pinecone)
  • search_conversations_text (tool_services, desktop/web REST + mobile LangChain) → same

Pinecone always returns the k nearest neighbors regardless of relevance, and embeddings rank proper names poorly — so a name query returns k semantically-near (wrong) conversations and the right one never surfaces. The existing Typesense keyword search (utils/conversations/search.py, query_by: structured.overview, structured.title) that powers the app's search bar would match the name literally, but chat never calls it.

(The issue's suggested fix — indexing speakers/transcripts in Typesense — requires Firestore→Typesense extension schema changes, infra-side; verified against the live collection schema that only structured.* fields are indexed. This PR is the in-code fix that resolves the reported failure: names that appear in titles/overviews — which conversation summaries normally include — become findable by chat.)

Fix

Hybrid retrieval at both call sites:

  • New keyword_search_conversation_ids() — wraps the existing Typesense search, returns ids only, fail-open (any Typesense error → [] → exact previous vector-only behavior).
  • New merge_conversation_search_ids() — keyword hits first (exact text matches, sorted newest-first), then vector hits, deduplicated.
  • No change to the Typesense query itself (same query_by fields already used in prod by /v1/conversations/search), so no schema risk. Locked conversations are already excluded by both halves; keyword search excludes discarded.
  • Tool/docstring updated so the agent knows the tool now handles exact name matching.

Tests

tests/unit/test_conversation_hybrid_search.py (registered in test.sh): merge ordering/dedup/non-mutation, id extraction, fail-open on search error, filter passthrough (incl. include_discarded=False), plus structural guards that both call sites keep the hybrid path. Helper logic verified locally via stdlib harness (pytest not installed locally; CI runs the suite). black + lint_async_blockers clean. UNVERIFIED at runtime against a live chat session.

Fixes #5072

🤖 automated by hourly watchdog; opened for review, not merged.

🤖 Generated with Claude Code

@greptile-apps

greptile-apps Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Fixes AI Chat's inability to find conversations by speaker/participant names by replacing pure Pinecone vector search with a hybrid keyword+vector approach at both chat retrieval paths (search_conversations_tool and search_conversations_text).

  • Adds keyword_search_conversation_ids — a fail-open Typesense wrapper that returns IDs matching the query in structured.title / structured.overview; any Typesense error degrades gracefully to [].
  • Adds merge_conversation_search_ids — keyword hits first (exact text matches, newest-first), then vector hits, deduplicated; both helpers are covered by new unit tests including a structural guard that prevents either call site from silently dropping the hybrid path.

Confidence Score: 4/5

Safe to merge for the core bug fix; the hybrid retrieval logic is correct and the fail-open design prevents regressions.

The two new helper functions are small and well-tested. The only thing worth watching is that both keyword and vector each independently pull up to limit results before merging, so the undeduped combined set passed to Firestore and ultimately to the LLM can be up to twice the requested limit. This doesn't produce wrong answers, but it can silently inflate LLM context and Firestore read cost relative to what callers expect.

The merge-without-cap pattern appears in both conversation_tools.py and tool_services/conversations.py.

Important Files Changed

Filename Overview
backend/utils/conversations/search.py Adds keyword_search_conversation_ids (fail-open Typesense wrapper) and merge_conversation_search_ids (keyword-first dedup merge). Both helpers are small, well-tested, and isolated from the existing search_conversations function.
backend/utils/retrieval/tools/conversation_tools.py Replaces vector-only search with hybrid keyword+vector at search_conversations_tool. The merged IDs list is not capped at limit before the Firestore fetch, so up to 2 * limit conversations can reach the LLM.
backend/utils/retrieval/tool_services/conversations.py Applies the same hybrid pattern to search_conversations_text (desktop/web REST + mobile LangChain path). Same uncapped merge issue as conversation_tools.py.
backend/tests/unit/test_conversation_hybrid_search.py New unit tests covering merge ordering, dedup, fail-open, filter passthrough, and structural guards ensuring both call sites import and use the hybrid path.
backend/test.sh Registers the new test file in the test runner suite.

Sequence Diagram

sequenceDiagram
    participant LLM as AI Chat / LangChain
    participant Tool as search_conversations_tool / search_conversations_text
    participant KW as keyword_search_conversation_ids (Typesense)
    participant VEC as vector_db.query_vectors (Pinecone)
    participant Merge as merge_conversation_search_ids
    participant FS as Firestore (get_conversations_by_id)

    LLM->>Tool: query, limit, uid
    par Keyword search
        Tool->>KW: uid, query, limit
        KW-->>Tool: keyword_ids (or [] on error)
    and Vector search
        Tool->>VEC: "query, uid, starts_at, ends_at, k=limit"
        VEC-->>Tool: vector_ids
    end
    Tool->>Merge: keyword_ids, vector_ids
    Merge-->>Tool: merged_ids (keyword-first, deduped, up to 2x limit)
    Tool->>FS: uid, merged_ids
    FS-->>Tool: conversations_data
    Tool-->>LLM: formatted conversations string
Loading

Reviews (1): Last reviewed commit: "test(backend): cover hybrid conversation..." | Re-trigger Greptile

Comment on lines +402 to +406
keyword_ids = keyword_search_conversation_ids(
uid=uid, query=query, limit=limit, start_date=starts_at, end_date=ends_at
)
vector_ids = vector_db.query_vectors(query=query, uid=uid, starts_at=starts_at, ends_at=ends_at, k=limit)
conversation_ids = merge_conversation_search_ids(keyword_ids, vector_ids)

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.

P2 Merged result set can silently exceed limit

Both keyword and vector search independently fetch up to limit results. After merge_conversation_search_ids, conversation_ids can contain up to 2 * limit unique entries — all of which are fetched from Firestore and passed verbatim into the LLM context. With limit=5 (the function default) this could double the context consumed; at limit=20 that's up to 40 conversations fed to the model. There is no trim step between the merge and the Firestore fetch (get_conversations_by_id). The same issue exists at the parallel call site in tool_services/conversations.py at line 185-189.

@kodjima33
kodjima33 merged commit 98ebb97 into main Jun 11, 2026
3 checks passed
@kodjima33
kodjima33 deleted the watchdog/issue-5072-chat-name-search branch June 11, 2026 05:56
@undivisible undivisible added the AI label Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: AI Chat can't find conversations by participant/speaker names

2 participants