agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

ValueError: Could not find document for id - FAISS index corruption after chat branching

Open sumer-man opened this issue 7 months ago • 2 comments

Been getting this error repeatedly. Sometimes it interrupts LLM's work. Error dump at the bottom. Asked deep-wiki. Here's what I got:

Issue: FAISS Memory Index Corruption After Chat Branching

Description

When branching a chat in Agent Zero v1.10, the FAISS vector database can become inconsistent, causing memory recall to fail with ValueError: Could not find document for id. This interrupts the main model's work and prevents normal operation.

Steps to Reproduce

  1. Start a chat with existing memory entries
  2. Branch the chat (create a new conversation from existing one)
  3. Allow the agent to attempt memory recall operations
  4. Error occurs during search_similarity_threshold in the memory recall extension

Expected Behavior

Memory recall should work seamlessly after branching chats, with proper isolation of memory indexes between branches.

Actual Behavior

ValueError: Could not find document for id CFa2cJilNo, got ID CFa2cJilNo not found.

The error originates in the FAISS similarity search when the vector index contains references to document IDs that no longer exist in the document store.

Environment

  • Agent Zero version: v1.10
  • Memory system: FAISS vector database
  • Trigger: Chat branching operation

Root Cause Analysis

The error occurs in the memory recall extension during similarity search 1 . The FAISS index contains a reference to document ID CFa2cJilNo, but the actual document is missing from the document store.

When branching chats, the memory system creates or references memory indexes that can become inconsistent if documents are deleted or if there's a mismatch between the FAISS index and the document store 2 .

The MyFaiss class has some protection against missing documents in get_by_ids() 3 , but the similarity search operation bypasses this protection.

Workarounds

  1. Rebuild Memory Index:

    await Memory.reload(agent)
    

    This deletes the cached index and recreates it from existing documents 4 .

  2. Clear Corrupted Memory Entries:

    • Access Memory Dashboard in Web UI
    • Delete problematic entries
    • Rebuild the index
  3. Prevention:

    • Disable memory recall temporarily when branching
    • Create backups before major branching operations
    • Use memory management tools regularly

Additional Context

This is a known issue with FAISS vector databases when document stores become inconsistent. The memory system is designed to be isolated per agent context, but branching can create edge cases where the isolation isn't perfect.

The error typically occurs after:

  • Branching chats with existing memory
  • Deleting memory entries without rebuilding the index
  • Concurrent access to memory from multiple agents
  • Corruption of the memory index files

Suggested Fix

Consider adding error handling in the similarity search path to gracefully handle missing document IDs, or implement a more robust memory index synchronization mechanism when branching chats.


Notes

This issue affects the core memory system functionality and can completely interrupt agent operations. The workaround requires manual intervention, which impacts user experience. A proper fix should ensure memory index consistency during chat branching operations.

Wiki pages you might want to explore:

  • Developer Setup & RFC Mode (agent0ai/agent-zero)
  • Glossary (agent0ai/agent-zero)

Error dump:

ValueError: Could not find document for id CFa2cJilNo, got ID CFa2cJilNo not found.

Traceback (most recent call last):
Traceback (most recent call last):
  File "/a0/helpers/extension.py", line 176, in _run_async
    data["result"] = await data["result"]
                     ^^^^^^^^^^^^^^^^^^^^
  File "/a0/agent.py", line 596, in handle_exception
    raise exception # exception handling is done by extensions
    ^^^^^^^^^^^^^^^
  File "/a0/agent.py", line 401, in monologue
    prompt = await self.prepare_prompt(loop_data=self.loop_data)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/a0/helpers/extension.py", line 183, in _run_async
    result = _process_result(data)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/a0/helpers/extension.py", line 143, in _process_result
    raise exc
  File "/a0/helpers/extension.py", line 176, in _run_async
    data["result"] = await data["result"]
                     ^^^^^^^^^^^^^^^^^^^^
  File "/a0/agent.py", line 551, in prepare_prompt
    await extension.call_extensions_async(
  File "/a0/helpers/extension.py", line 235, in call_extensions_async
    await result
  File "/a0/plugins/_memory/extensions/python/message_loop_prompts_after/_91_recall_wait.py", line 30, in execute
    await task
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/futures.py", line 287, in __await__
    yield self  # This tells Task to wait for completion.
    ^^^^^^^^^^
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/tasks.py", line 385, in __wakeup
    future.result()
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/futures.py", line 203, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/tasks.py", line 316, in __step_run_and_handle_result
    result = coro.throw(exc)
             ^^^^^^^^^^^^^^^
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
    return await fut
           ^^^^^^^^^
  File "/a0/plugins/_memory/extensions/python/message_loop_prompts_after/_50_recall_memories.py", line 133, in search_memories
    memories = await db.search_similarity_threshold(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/a0/plugins/_memory/helpers/memory.py", line 345, in search_similarity_threshold
    return await self.db.asearch(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_core/vectorstores/base.py", line 380, in asearch
    docs_and_similarities = await self.asimilarity_search_with_relevance_scores(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_core/vectorstores/base.py", line 603, in asimilarity_search_with_relevance_scores
    docs_and_similarities = await self._asimilarity_search_with_relevance_scores(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 1327, in _asimilarity_search_with_relevance_scores
    docs_and_scores = await self.asimilarity_search_with_score(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 550, in asimilarity_search_with_score
    docs = await self.asimilarity_search_with_score_by_vector(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 481, in asimilarity_search_with_score_by_vector
    return await run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_core/runnables/config.py", line 622, in run_in_executor
    return await asyncio.get_running_loop().run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/futures.py", line 287, in __await__
    yield self  # This tells Task to wait for completion.
    ^^^^^^^^^^
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/tasks.py", line 385, in __wakeup
    future.result()
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/asyncio/futures.py", line 203, in result
    raise self._exception.with_traceback(self._exception_tb)
  File "/opt/pyenv/versions/3.12.4/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_core/runnables/config.py", line 613, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/opt/venv-a0/lib/python3.12/site-packages/langchain_community/vectorstores/faiss.py", line 430, in similarity_search_with_score_by_vector
    raise ValueError(f"Could not find document for id {_id}, got {doc}")

sumer-man avatar May 02 '26 13:05 sumer-man

Little update: The error above stops in their tracks every agent (including subordinates) Makes work essentially impossible. The "workaround" await Memory.reload(agent) does not work. Tried. I'm thinking that maybe this happens when the memory system tries to recall memories that were created in a different/previous branch? I branch a lot. I'm finding it useful for context management.

sumer-man avatar May 04 '26 01:05 sumer-man

Closing due to inactivity of 90 days.

github-actions[bot] avatar Aug 03 '26 04:08 github-actions[bot]