{"record":{"id":"3a544bb317b50043","repo":"langchain-ai/langchain","slug":"unable-to-get-bytes-for-blob-self","errorCode":null,"errorMessage":"Unable to get bytes for blob {self}","messagePattern":"Unable to get bytes for blob (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/documents/base.py","lineNumber":192,"sourceCode":"        raise ValueError(msg)\n\n    def as_bytes(self) -> bytes:\n        \"\"\"Read data as bytes.\n\n        Raises:\n            ValueError: If the blob cannot be represented as bytes.\n\n        Returns:\n            The data as bytes.\n        \"\"\"\n        if isinstance(self.data, bytes):\n            return self.data\n        if isinstance(self.data, str):\n            return self.data.encode(self.encoding)\n        if self.data is None and self.path:\n            return Path(self.path).read_bytes()\n        msg = f\"Unable to get bytes for blob {self}\"\n        raise ValueError(msg)\n\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}\"","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/documents/base.py#L174-L210","documentation":"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`.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Store raw content in `data` as `bytes`/`str`, or set `path` and leave data unset.","If you have a Path, use `Blob.from_data(path.read_bytes())` or `Blob.from_path(path)` rather than `Blob(data=Path(...))`.","Check `isinstance(blob.data, (str, bytes)) or blob.path` before calling as_bytes in generic code."],"exampleFix":"# before\nblob = Blob(data=Path(\"img.png\"))\nb = blob.as_bytes()\n\n# after\nblob = Blob.from_path(\"img.png\")\nb = blob.as_bytes()","handlingStrategy":"type-guard","validationCode":"def blob_can_as_bytes(blob) -> bool:\n    return isinstance(blob.data, (str, bytes)) or (blob.data is None and bool(blob.path))","typeGuard":"def is_bytes_readable(blob: Blob) -> bool:\n    return blob.data is None and blob.path is not None or isinstance(blob.data, (str, bytes))","tryCatchPattern":"try:\n    payload = blob.as_bytes()\nexcept ValueError:\n    payload = b\"\"  # or skip document","preventionTips":["Keep raw payloads (str/bytes) in Blob.data; put Path in Blob.path, not data.","Validate blob.data types at loader construction time, not at read time."],"tags":["blob","documents","type-error"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}