Skip to content

Commit e6059e6

Browse files
authored
fix: truncate ByteStream string representation (#8673)
* fix: truncate ByteStream string representation * add reno * better reno * add test * Update test_byte_stream.py * apply feedback * update reno
1 parent 8e3f647 commit e6059e6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

Diff for: haystack/dataclasses/byte_stream.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from typing import Any, Dict, Optional
88

99

10-
@dataclass
10+
@dataclass(repr=False)
1111
class ByteStream:
1212
"""
1313
Base data class representing a binary object in the Haystack API.
@@ -63,3 +63,15 @@ def to_string(self, encoding: str = "utf-8") -> str:
6363
:raises: UnicodeDecodeError: If the ByteStream data cannot be decoded with the specified encoding.
6464
"""
6565
return self.data.decode(encoding)
66+
67+
def __repr__(self) -> str:
68+
"""
69+
Return a string representation of the ByteStream, truncating the data to 100 bytes.
70+
"""
71+
fields = []
72+
truncated_data = self.data[:100] + b"..." if len(self.data) > 100 else self.data
73+
fields.append(f"data={truncated_data!r}")
74+
fields.append(f"meta={self.meta!r}")
75+
fields.append(f"mime_type={self.mime_type!r}")
76+
fields_str = ", ".join(fields)
77+
return f"{self.__class__.__name__}({fields_str})"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
ByteStream now truncates the data to 100 bytes in the string representation to avoid excessive log output.

Diff for: test/dataclasses/test_byte_stream.py

+9
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,12 @@ def test_to_file(tmp_path, request):
7171
ByteStream(test_str.encode()).to_file(test_path)
7272
with open(test_path, "rb") as fd:
7373
assert fd.read().decode() == test_str
74+
75+
76+
def test_str_truncation():
77+
test_str = "1234567890" * 100
78+
b = ByteStream.from_string(test_str, mime_type="text/plain", meta={"foo": "bar"})
79+
string_repr = str(b)
80+
assert len(string_repr) < 200
81+
assert "text/plain" in string_repr
82+
assert "foo" in string_repr

0 commit comments

Comments
 (0)