Description of the Bug
In backend/app/routes/chat.py, when a user queries across multiple documents, the code creates a composite cache key:
cache_doc_key = "multi:" + ",".join(sorted(payload.document_ids))
This composite string (e.g., "multi:doc1,doc2,doc3") is then passed directly to _save_message() as the document_id parameter:
msg = _save_message(
db=db,
user_id=user_id,
document_id=cache_doc_key, # ← composite key, not a real document ID
...
)
This value is stored in the ChatMessage.document_id column, which is supposed to be a foreign key referencing the Document table. By storing a fabricated composite string:
- The foreign key contract is broken
- Queries filtering by
document_id won't find these messages
- Any UI that looks up document title/metadata using
document_id will fail
- Chat history for multi-document queries is effectively orphaned from document records
Steps to Reproduce
- Upload at least two documents
- Send a multi-document query (selecting both documents)
- Check the
chat_messages table in the database
- Observe that
document_id contains "multi:doc1,doc2" instead of a valid document UUID
Expected Behavior
Multi-document queries should store document_id as None (since there is no single document). The cache key should be stored separately, perhaps in a new field or as part of a cache subsystem, not in the document_id column.
Affected File
backend/app/routes/chat.py (lines ~259-260 and ~374)
Suggested Fix
Pass document_id=None for multi-document queries and maintain the cache key separately. Add a cache_key field to the ChatMessage model for this purpose, or use a separate cache data structure.
GSSoC '26
Description of the Bug
In
backend/app/routes/chat.py, when a user queries across multiple documents, the code creates a composite cache key:This composite string (e.g.,
"multi:doc1,doc2,doc3") is then passed directly to_save_message()as thedocument_idparameter:This value is stored in the
ChatMessage.document_idcolumn, which is supposed to be a foreign key referencing theDocumenttable. By storing a fabricated composite string:document_idwon't find these messagesdocument_idwill failSteps to Reproduce
chat_messagestable in the databasedocument_idcontains"multi:doc1,doc2"instead of a valid document UUIDExpected Behavior
Multi-document queries should store
document_idasNone(since there is no single document). The cache key should be stored separately, perhaps in a new field or as part of a cache subsystem, not in the document_id column.Affected File
backend/app/routes/chat.py(lines ~259-260 and ~374)Suggested Fix
Pass
document_id=Nonefor multi-document queries and maintain the cache key separately. Add acache_keyfield to theChatMessagemodel for this purpose, or use a separate cache data structure.GSSoC '26