#13040·haystack

InMemoryDocumentStore: count_unique_metadata_by_filter and get_metadata_field_unique_values disagree on int/float/bool values

Author: Harsh23KashyapCreated Sep 30, 2026Updated Sep 30, 2026

On the same documents, count_unique_metadata_by_filter and get_metadata_field_unique_values disagree on how many unique values a metadata field has when the values mix int, float, and bool.

Reproduction (current main, 2ae9acd):

python
from haystack import Document
from haystack.document_stores.in_memory import InMemoryDocumentStore

store = InMemoryDocumentStore()
store.write_documents([
    Document(content="a", meta={"page": 1}),
    Document(content="b", meta={"page": 1.0}),
])

print(store.count_unique_metadata_by_filter(filters={}, metadata_fields=["page"]))
# {'page': 1}

values, total = store.get_metadata_field_unique_values("page", size=100)
print(values, total)
# [1, 1.0] 2

The count says one unique value; the values endpoint returns two for the same field on the same documents. Mixing booleans in makes it worse: True, 1, and 1.0 all collapse into one entry in the count, while the values endpoint lists all three.

Cause:

  • count_unique_metadata_by_filter deduplicates with a plain set of _make_metadata_value_hashable(value). Scalars pass through unchanged, and True == 1 == 1.0 with equal hashes in Python, so the set merges them.
  • get_metadata_field_unique_values deduplicates on (type(value).__name__, _make_metadata_value_hashable(value)), so the same values stay distinct.

Mixed int/float metadata is common when values come from different ingest paths (JSON parsing, pandas), so a UI built on these two endpoints would show a count that does not match the values it lists.

I would expect the two APIs to agree on what a unique value is. Keying the count by type name plus value, same as the values endpoint, keeps the existing tests passing. Happy to open a PR with that change if you confirm the direction.