Skip to content

IndexError when ChromaDB query returns empty results #195

@wanikua

Description

@wanikua

Bug

searcher.search(), searcher.search_memories(), and Layer3.search() all access results["documents"][0] without checking if the outer list is empty first.

When a query matches zero documents — e.g. a freshly initialized palace before any mining, or a wing/room filter that excludes everything — ChromaDB returns {"documents": [], ...} and the [0] indexing crashes with IndexError.

Affected locations

File Function Line
searcher.py search() ~58
searcher.py search_memories() ~132
layers.py Layer3.search() ~289

Steps to reproduce

import chromadb

client = chromadb.PersistentClient(path="/tmp/test_palace")
col = client.get_or_create_collection("mempalace_drawers")

# Query an empty collection
results = col.query(query_texts=["test"], n_results=5, include=["documents", "metadatas", "distances"])
print(results["documents"])  # [] — empty list
print(results["documents"][0])  # IndexError: list index out of range

Expected behavior

Should return "No results found" gracefully instead of crashing.

Fix

Guard the outer list before indexing: if not results["documents"] or not results["documents"][0].

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/searchSearch and retrievalbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions