{"record":{"id":"2a84c477f796ec31","repo":"langchain-ai/langchain","slug":"unable-to-convert-blob-self","errorCode":null,"errorMessage":"Unable to convert blob {self}","messagePattern":"Unable to convert blob (.+?)","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/documents/base.py","lineNumber":211,"sourceCode":"\n    @contextlib.contextmanager\n    def as_bytes_io(self) -> Generator[BytesIO | BufferedReader, None, None]:\n        \"\"\"Read data as a byte stream.\n\n        Raises:\n            NotImplementedError: If the blob cannot be represented as a byte stream.\n\n        Yields:\n            The data as a byte stream.\n        \"\"\"\n        if isinstance(self.data, bytes):\n            yield BytesIO(self.data)\n        elif self.data is None and self.path:\n            with Path(self.path).open(\"rb\") as f:\n                yield f\n        else:\n            msg = f\"Unable to convert blob {self}\"\n            raise NotImplementedError(msg)\n\n    @classmethod\n    def from_path(\n        cls,\n        path: PathLike,\n        *,\n        encoding: str = \"utf-8\",\n        mime_type: str | None = None,\n        guess_type: bool = True,\n        metadata: dict[Any, Any] | None = None,\n    ) -> Blob:\n        \"\"\"Load the blob from a path like object.\n\n        Args:\n            path: Path-like object to file to be read\n            encoding: Encoding to use if decoding the bytes into a string\n            mime_type: If provided, will be set as the MIME type of the data\n            guess_type: If `True`, the MIME type will be guessed from the file","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/documents/base.py#L193-L229","documentation":"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`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Wrap the str yourself: `io.BytesIO(blob.data.encode(blob.encoding))`, or use `io.BytesIO(blob.as_bytes())`.","Construct the Blob from bytes: `Blob.from_data(text.encode(\"utf-8\"))` so as_bytes_io works directly.","Write the content to a temp file and use `Blob.from_path()` if the consumer needs a real file handle."],"exampleFix":"# before\nblob = Blob(data=html_text)\nwith blob.as_bytes_io() as f:\n    parser.read(f)\n\n# after\nimport io\nwith io.BytesIO(blob.as_bytes()) as f:\n    parser.read(f)","handlingStrategy":"validation","validationCode":"import io\n\ndef safe_bytes_io(blob):\n    if isinstance(blob.data, bytes) or (blob.data is None and blob.path):\n        return blob.as_bytes_io()\n    return io.BytesIO(blob.as_bytes())  # str payload path","typeGuard":"def supports_bytes_io(blob: Blob) -> bool:\n    return isinstance(blob.data, bytes) or (blob.data is None and bool(blob.path))","tryCatchPattern":"try:\n    with blob.as_bytes_io() as f:\n        consume(f)\nexcept NotImplementedError:\n    with io.BytesIO(blob.as_bytes()) as f:\n        consume(f)","preventionTips":["Remember as_bytes_io rejects str payloads even though as_bytes accepts them.","Prefer building Blobs from bytes or paths when downstream code needs streams."],"tags":["blob","documents","not-implemented","file-like"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}