Skip to content

Commit f77b09e

Browse files
authored
Add _to_trace_dict for image content and file content (#10989)
* Add _to_trace_dict for image content and file content * update
1 parent 01b11aa commit f77b09e

6 files changed

Lines changed: 57 additions & 2 deletions

File tree

haystack/dataclasses/chat_message.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,9 +559,9 @@ def _to_trace_dict(self) -> dict[str, Any]:
559559
for part in self._content:
560560
serialized_part = _serialize_content_part(part)
561561
if isinstance(part, ImageContent):
562-
serialized_part["image"]["base64_image"] = f"Base64 string ({len(part.base64_image)} characters)"
562+
serialized_part["image"] = part._to_trace_dict()
563563
elif isinstance(part, FileContent):
564-
serialized_part["file"]["base64_data"] = f"Base64 string ({len(part.base64_data)} characters)"
564+
serialized_part["file"] = part._to_trace_dict()
565565
serialized["content"].append(serialized_part)
566566

567567
return serialized

haystack/dataclasses/file_content.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ def to_dict(self) -> dict[str, Any]:
8484
"""
8585
return asdict(self)
8686

87+
def _to_trace_dict(self) -> dict[str, Any]:
88+
"""
89+
Convert the FileContent to a dictionary representation for tracing.
90+
91+
The base64_data is replaced with a placeholder string to avoid sending large payloads to the tracing backend.
92+
93+
:returns:
94+
Serialized version of the object only for tracing purposes.
95+
"""
96+
data = self.to_dict()
97+
data["base64_data"] = f"Base64 string ({len(self.base64_data)} characters)"
98+
return data
99+
87100
@classmethod
88101
def from_dict(cls, data: dict[str, Any]) -> "FileContent":
89102
"""

haystack/dataclasses/image_content.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,19 @@ def to_dict(self) -> dict[str, Any]:
142142
"""
143143
return asdict(self)
144144

145+
def _to_trace_dict(self) -> dict[str, Any]:
146+
"""
147+
Convert the ImageContent to a dictionary representation for tracing.
148+
149+
The base64_image is replaced with a placeholder string to avoid sending large payloads to the tracing backend.
150+
151+
:returns:
152+
Serialized version of the object only for tracing purposes.
153+
"""
154+
data = self.to_dict()
155+
data["base64_image"] = f"Base64 string ({len(self.base64_image)} characters)"
156+
return data
157+
145158
@classmethod
146159
def from_dict(cls, data: dict[str, Any]) -> "ImageContent":
147160
"""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
enhancements:
3+
- |
4+
Add ``_to_trace_dict`` method to ``ImageContent`` and ``FileContent`` dataclasses.
5+
When tracing is enabled, the large base64-encoded binary fields (``base64_image`` and ``base64_data``)
6+
are replaced with placeholder strings (e.g. ``"Base64 string (N characters)"``),
7+
consistent with the behavior of ``ByteStream._to_trace_dict``.

test/dataclasses/test_file_content.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,14 @@ def test_file_content_warn_on_inplace_mutation():
164164
fc = FileContent(base64_data="dGVzdA==", mime_type="text/plain", validation=False)
165165
with pytest.warns(Warning, match="dataclasses.replace"):
166166
fc.mime_type = "application/pdf"
167+
168+
169+
def test_file_content_to_trace_dict(base64_pdf_string):
170+
file_content = FileContent(
171+
base64_data=base64_pdf_string, mime_type="application/pdf", filename="test.pdf", extra={"key": "value"}
172+
)
173+
data = file_content._to_trace_dict()
174+
assert data["base64_data"] == f"Base64 string ({len(base64_pdf_string)} characters)"
175+
assert data["mime_type"] == "application/pdf"
176+
assert data["filename"] == "test.pdf"
177+
assert data["extra"] == {"key": "value"}

test/dataclasses/test_image_content.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,14 @@ def test_image_content_warn_on_inplace_mutation(base64_image_string):
235235
ic = ImageContent(base64_image=base64_image_string, mime_type="image/png")
236236
with pytest.warns(Warning, match="dataclasses.replace"):
237237
ic.detail = "high"
238+
239+
240+
def test_image_content_to_trace_dict(base64_image_string):
241+
image_content = ImageContent(
242+
base64_image=base64_image_string, mime_type="image/png", detail="auto", meta={"key": "value"}
243+
)
244+
data = image_content._to_trace_dict()
245+
assert data["base64_image"] == f"Base64 string ({len(base64_image_string)} characters)"
246+
assert data["mime_type"] == "image/png"
247+
assert data["detail"] == "auto"
248+
assert data["meta"] == {"key": "value"}

0 commit comments

Comments
 (0)