langchain-ai/langchain · error · ValueError

Unable to get string for blob {self}

Error message

Unable to get string for blob {self}

What it means

Thrown by `Blob.as_string()` when the blob's content cannot be resolved to text. The method handles three cases: reading from `path`, decoding `bytes` data, and returning `str` data. If `data` is not None but is neither bytes nor str (and no path fallback applies because data is set), the blob's payload type is unsupported and a `ValueError` is raised.

Source

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

        return values

    def as_string(self) -> str:
        """Read data as a string.

        Raises:
            ValueError: If the blob cannot be represented as a string.

        Returns:
            The data as a string.
        """
        if self.data is None and self.path:
            return Path(self.path).read_text(encoding=self.encoding)
        if isinstance(self.data, bytes):
            return self.data.decode(self.encoding)
        if isinstance(self.data, str):
            return self.data
        msg = f"Unable to get string for blob {self}"
        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)

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Ensure `Blob.data` is a `str` or `bytes`, or that a `path` is set when data is None.
  2. Never construct `Blob(data=None)` without a path — use `Blob.from_path()` instead.
  3. If you control the writer side, serialize the payload (`json.dumps`, `.encode()`) before storing it in a Blob.

Example fix

# before
blob = Blob(data=None)  # validator passes, as_string() fails
s = blob.as_string()

# after
blob = Blob.from_path("notes.txt")
s = blob.as_string()
Defensive patterns

Strategy: type-guard

Validate before calling

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

Type guard

def is_readable_blob(blob: Blob) -> bool:
    data = blob.data
    if data is None:
        return bool(blob.path)
    return isinstance(data, (str, bytes))

Try / catch

try:
    text = blob.as_string()
except ValueError as e:
    logger.warning("Skipping unreadable blob %s: %s", blob.source, e)
    continue

Prevention

When it happens

Trigger: Calling `blob.as_string()` on a Blob whose `data` is a non-str/bytes object (e.g. an int, dict, or custom object), or on a Blob constructed with `data=None` and no `path` (slips past the validator because the key was present).

Common situations: Custom loaders stuffing arbitrary Python objects into `Blob.data`; creating `Blob(data=None)` explicitly, which bypasses the 'either data or path' validator; test fixtures building Blobs with placeholder values.

Related errors


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