langchain-ai/langchain · error · NotImplementedError

Unable to convert blob {self}

Error message

Unable to convert blob {self}

What it means

Raised by `Blob.as_bytes_io()` (a context manager) when the blob cannot be exposed as a byte stream. Only two representations are supported: in-memory `bytes` (wrapped in a `BytesIO`) and a filesystem `path` (opened as a `BufferedReader`). A str payload — or any other type — cannot be streamed without an explicit encoding decision, so the library raises `NotImplementedError`.

Source

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

    @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}"
            raise NotImplementedError(msg)

    @classmethod
    def from_path(
        cls,
        path: PathLike,
        *,
        encoding: str = "utf-8",
        mime_type: str | None = None,
        guess_type: bool = True,
        metadata: dict[Any, Any] | None = None,
    ) -> Blob:
        """Load the blob from a path like object.

        Args:
            path: Path-like object to file to be read
            encoding: Encoding to use if decoding the bytes into a string
            mime_type: If provided, will be set as the MIME type of the data
            guess_type: If `True`, the MIME type will be guessed from the file

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Wrap the str yourself: `io.BytesIO(blob.data.encode(blob.encoding))`, or use `io.BytesIO(blob.as_bytes())`.
  2. Construct the Blob from bytes: `Blob.from_data(text.encode("utf-8"))` so as_bytes_io works directly.
  3. Write the content to a temp file and use `Blob.from_path()` if the consumer needs a real file handle.

Example fix

# before
blob = Blob(data=html_text)
with blob.as_bytes_io() as f:
    parser.read(f)

# after
import io
with io.BytesIO(blob.as_bytes()) as f:
    parser.read(f)
Defensive patterns

Strategy: validation

Validate before calling

import io

def safe_bytes_io(blob):
    if isinstance(blob.data, bytes) or (blob.data is None and blob.path):
        return blob.as_bytes_io()
    return io.BytesIO(blob.as_bytes())  # str payload path

Type guard

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

Try / catch

try:
    with blob.as_bytes_io() as f:
        consume(f)
except NotImplementedError:
    with io.BytesIO(blob.as_bytes()) as f:
        consume(f)

Prevention

When it happens

Trigger: Calling `with blob.as_bytes_io():` on a Blob whose `data` is a `str`, or whose data is an unsupported type with no path. Note this is asymmetric with `as_bytes()`, which does accept str.

Common situations: Reusing a Blob built from string data (e.g. scraped HTML) with a parser or media handler that requires a file-like object; assuming as_bytes and as_bytes_io have identical acceptance.

Related errors


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