Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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
1 change: 1 addition & 0 deletions 1 haystack/dataclasses/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def from_dict(cls, data: dict[str, Any]) -> "Document":

The `blob` field is converted to its original type.
"""
data = data.copy()
if blob := data.get("blob"):
data["blob"] = ByteStream.from_dict(blob)
if sparse_embedding := data.get("sparse_embedding"):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Prevent ``Document.from_dict()`` from mutating the input dictionary during deserialization.
37 changes: 36 additions & 1 deletion 37 test/dataclasses/test_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# SPDX-License-Identifier: Apache-2.0

import warnings
from copy import deepcopy
from dataclasses import replace

import pytest
Expand Down Expand Up @@ -213,7 +214,7 @@ def test_from_dict():
assert Document.from_dict({}) == Document()


def from_from_dict_with_parameters():
def test_from_dict_with_parameters():
blob_data = b"some bytes"
assert Document.from_dict(
{
Expand All @@ -234,6 +235,40 @@ def from_from_dict_with_parameters():
)


def test_from_dict_does_not_mutate_input():
blob_data = b"some bytes"
data = {
"content": "test text",
"blob": {"data": list(blob_data), "mime_type": "text/markdown"},
"score": 0.812,
"embedding": [0.1, 0.2, 0.3],
"sparse_embedding": {"indices": [0, 2, 4], "values": [0.1, 0.2, 0.3]},
"date": "10-10-2023",
"type": "article",
}
original_data = deepcopy(data)

assert Document.from_dict(data) == Document(
content="test text",
blob=ByteStream(blob_data, mime_type="text/markdown"),
score=0.812,
embedding=[0.1, 0.2, 0.3],
sparse_embedding=SparseEmbedding(indices=[0, 2, 4], values=[0.1, 0.2, 0.3]),
meta={"date": "10-10-2023", "type": "article"},
)
assert data == original_data


def test_from_dict_does_not_mutate_input_with_explicit_meta():
data = {"content": "test text", "meta": {"date": "10-10-2023", "type": "article"}, "score": 0.812}
original_data = deepcopy(data)

assert Document.from_dict(data) == Document(
content="test text", meta={"date": "10-10-2023", "type": "article"}, score=0.812
)
assert data == original_data


def test_from_dict_with_legacy_fields():
assert Document.from_dict(
{
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.