← Back to Pull requests

fix: keep the type of meta values in the Document ID hash #13847

Closed
unknown closed this pull request without merging on Nov 23, 2026 09:17 UTC
View on GitHub
unknown unknown commented on Nov 20, 2026 18:46 UTC
Author

Related Issues

No issue for this, I hit it while working on something else.

Proposed Changes:

Document._create_id builds the hash from json.dumps(self.meta, sort_keys=True, default=str). Two things in there drop the type of a value:

  • JSON has no tuple, so json.dumps writes (1, 2) as [1, 2], the same as a list.

  • A set isn't JSON serializable at all, so it goes through default=str and becomes the string "{1, 2}".
  • Both cases make unrelated documents collide:

    Document(content="x", meta={"tags": [1, 2]}).id == Document(content="x", meta={"tags": (1, 2)}).id  # True
    Document(content="x", meta={"tags": {1, 2}}).id == Document(content="x", meta={"tags": "{1, 2}"}).id  # True
    

    InMemoryDocumentStore.write_documents([...]) then raises DuplicateDocumentError: ID 'de0fa6eb5c49...' already exists, and with DuplicatePolicy.OVERWRITE the second document is silently dropped. Indexing a JSONL where the same field is sometimes a list and sometimes a tuple from the source format is enough to hit it.

    The fix walks meta before hashing and marks the two problematic types:

  • a tuple becomes {"__tuple__": [...]}, keeping the element order

  • a set becomes {"__set__": [...]}, sorted by the JSON of each element so that iteration order doesn't leak into the ID
  • Everything else is passed through untouched, which is what keeps existing IDs stable: json.dumps never sees the marker dicts unless the metadata actually contained a tuple or a set, so documents whose metadata only uses JSON types hash exactly as before. The hardcoded IDs in test_init_with_legacy_fields and test_init still pass, and test_to_dict still asserts document["id"] == doc._create_id().

    I did not touch the empty vs None content case, there are a few closed issues and PRs about that and I didn't want to reopen a settled discussion here.

    How did you test it?

    New tests in test/dataclasses/test_document.py:

  • test_id_distinguishes_meta_value_types, parametrized over list vs tuple, set vs its str(), set vs list, nested lists vs nested tuples, and two different nestings of the same tuple.

  • test_id_of_set_meta_is_independent_of_iteration_order: {1, 2, 3} and {3, 2, 1} give the same ID, which is what the sorting is for.

  • test_id_of_tuple_meta_depends_on_element_order: (1, 2) and (2, 1) don't.

  • test_documents_with_different_meta_types_can_be_stored_together: writes the list and the tuple document into an InMemoryDocumentStore and gets 2 documents back.
  • Before: pytest test/dataclasses/test_document.py gives 4 failed, 58 passed. After: 62 passed.
    pytest test/dataclasses test/document_stores/test_in_memory.py gives 794 passed, 8 skipped. The one failure in that run, test_image_content_from_url_wrong_mime_type, fails on a clean main too (it fetches a URL). ruff check and ruff format --check are clean.

    Notes for the reviewer

  • The marker dicts live in the hash input only. Document.to_dict() and Document.meta are unchanged, so round trips and serialization are not affected.

  • Someone who already relied on a tuple and a list collapsing to the same document will now get two documents. That's the point of the change, but it is a visible difference, hence the release note.

  • _make_metadata_value_hashable in utils/misc.py has a similar blind spot for sets, but it's used by the evaluators for grouping and not for IDs. I left it alone to keep this PR focused.
  • Checklist

  • I have read the contributors guidelines and the code of conduct.

  • I have updated the related issue with new insights and changes.

  • I have added unit tests and updated the docstrings.

  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.

  • I have documented my code.

  • I have added a release note file, following the contributors guidelines.

  • I have run pre-commit hooks and fixed any issue.
  • Diff: 552 lines (+414 / -138) across 6 files Reviewer: 1AbdullahButt Review duration: 3750m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 552 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.378
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #13847"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.1% (MEDIUM) · Higher = larger blast radius
    48.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19% · Safe to merge
    19%