Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion haystack/dataclasses/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def to_dict(self, flatten=True) -> Dict[str, Any]:

if flatten:
meta = data.pop("meta")
return {**data, **meta}
return {**meta, **data}

return data

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Make Document's first-level fields to take precedence over meta fields when flattening the dictionary representation.
17 changes: 17 additions & 0 deletions test/dataclasses/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ def test_to_dict_with_custom_parameters_without_flattening():
}


def test_to_dict_field_precedence():
"""
Test that Document's first-level fields take precedence over meta fields
when flattening the dictionary representation.
"""

doc = Document(content="from-content", score=0.9, meta={"content": "from-meta", "score": 0.5, "source": "web"})

flat_dict = doc.to_dict(flatten=True)

# First-level fields should take precedence
assert flat_dict["content"] == "from-content"
assert flat_dict["score"] == 0.9
# Meta-only fields should be preserved
assert flat_dict["source"] == "web"


def test_from_dict():
assert Document.from_dict({}) == Document()

Expand Down
Loading