InMemoryDocumentStore unique metadata count fails for list and dict values
Describe the bug
InMemoryDocumentStore.count_unique_metadata_by_filter() raises TypeError when a requested metadata field contains a JSON-serializable list or dictionary. The async variant fails through the same implementation.
Error message
TypeError: unhashable type: list
TypeError: unhashable type: dictExpected behavior
The method should count distinct JSON-serializable metadata values, including lists and dictionaries, consistently with the Document.meta contract. Equal list or dictionary values should count once, while list order should remain significant.
Additional context
Document.meta is documented as JSON-serializable. For the same documents, json.dumps(document.to_dict()), filter_documents(), and get_metadata_field_unique_values() work with these values. Existing scalar count tests pass, but composite metadata is not covered. The failure occurs when a user explicitly calls this public metadata statistics API; it is not part of the automatic retrieval path.
To Reproduce
from haystack import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore
for metadata in ({"tags": ["news", "ai"]}, {"source": {"type": "docs", "section": {"id": 1}}}):
store = InMemoryDocumentStore()
store.write_documents([Document(content="Document", meta=metadata)])
field = next(iter(metadata))
store.count_unique_metadata_by_filter(filters={}, metadata_fields=[field])Each call raises TypeError from the set-based unique-value collection. count_unique_metadata_by_filter_async() reproduces the same error.
FAQ Check
- Have you had a look at our new FAQ page?
System:
- OS: Linux
- Haystack version: main at
f7b46875f(2026-09-14)
Source: deepset-ai/haystack