langchain-ai/langchain · error · ValueError

Unable to get bytes for blob {self}

Error message

Unable to get bytes for blob {self}

What it means

Thrown by `Blob.as_bytes()` when the blob's content cannot be resolved to bytes. Supported cases: `data` already bytes, `data` as str (encoded via `blob.encoding`), or reading from `path` when data is None. Any other combination (data present but of an unsupported type, or both data effectively unusable and no path) raises a `ValueError`.

Source

Thrown at libs/core/langchain_core/documents/base.py:192

        raise ValueError(msg)

    def as_bytes(self) -> bytes:
        """Read data as bytes.

        Raises:
            ValueError: If the blob cannot be represented as bytes.

        Returns:
            The data as bytes.
        """
        if isinstance(self.data, bytes):
            return self.data
        if isinstance(self.data, str):
            return self.data.encode(self.encoding)
        if self.data is None and self.path:
            return Path(self.path).read_bytes()
        msg = f"Unable to get bytes for blob {self}"
        raise ValueError(msg)

    @contextlib.contextmanager
    def as_bytes_io(self) -> Generator[BytesIO | BufferedReader, None, None]:
        """Read data as a byte stream.

        Raises:
            NotImplementedError: If the blob cannot be represented as a byte stream.

        Yields:
            The data as a byte stream.
        """
        if isinstance(self.data, bytes):
            yield BytesIO(self.data)
        elif self.data is None and self.path:
            with Path(self.path).open("rb") as f:
                yield f
        else:
            msg = f"Unable to convert blob {self}"

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Store raw content in `data` as `bytes`/`str`, or set `path` and leave data unset.
  2. If you have a Path, use `Blob.from_data(path.read_bytes())` or `Blob.from_path(path)` rather than `Blob(data=Path(...))`.
  3. Check `isinstance(blob.data, (str, bytes)) or blob.path` before calling as_bytes in generic code.

Example fix

# before
blob = Blob(data=Path("img.png"))
b = blob.as_bytes()

# after
blob = Blob.from_path("img.png")
b = blob.as_bytes()
Defensive patterns

Strategy: type-guard

Validate before calling

def blob_can_as_bytes(blob) -> bool:
    return isinstance(blob.data, (str, bytes)) or (blob.data is None and bool(blob.path))

Type guard

def is_bytes_readable(blob: Blob) -> bool:
    return blob.data is None and blob.path is not None or isinstance(blob.data, (str, bytes))

Try / catch

try:
    payload = blob.as_bytes()
except ValueError:
    payload = b""  # or skip document

Prevention

When it happens

Trigger: Calling `blob.as_bytes()` with `data` set to a non-str/bytes value, or with `data=None` and no `path`. Distinct from as_string: a str payload is fine here, but e.g. a dict payload fails.

Common situations: Custom document loaders storing parsed objects instead of raw bytes; Blob fixtures with `data=None`; passing a `Path` object as `data` instead of using the `path` field.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/3a544bb317b50043. Report an issue: GitHub.