Skip to content
Closed
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: 2 additions & 0 deletions libs/core/langchain_core/documents/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def as_bytes_io(self) -> Generator[BytesIO | BufferedReader, None, None]:
"""
if isinstance(self.data, bytes):
yield BytesIO(self.data)
elif isinstance(self.data, str):
yield BytesIO(self.data.encode(self.encoding))
elif self.data is None and self.path:
with Path(self.path).open("rb") as f:
yield f
Expand Down
21 changes: 21 additions & 0 deletions libs/core/tests/unit_tests/documents/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Tests for `langchain_core.documents.base.Blob`."""

from langchain_core.documents.base import Blob


def test_as_bytes_io_with_string_data() -> None:
blob = Blob.from_data("hello")
with blob.as_bytes_io() as stream:
assert stream.read() == b"hello"


def test_as_bytes_io_with_string_data_respects_encoding() -> None:
blob = Blob.from_data("café", encoding="utf-16")
with blob.as_bytes_io() as stream:
assert stream.read() == "café".encode("utf-16")


def test_as_bytes_io_with_bytes_data() -> None:
blob = Blob.from_data(b"\x00\x01\x02")
with blob.as_bytes_io() as stream:
assert stream.read() == b"\x00\x01\x02"
Loading