{"record":{"id":"9bee8d1498df53db","repo":"langchain-ai/langchain","slug":"unable-to-get-string-for-blob-self","errorCode":null,"errorMessage":"Unable to get string for blob {self}","messagePattern":"Unable to get string for blob (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/documents/base.py","lineNumber":174,"sourceCode":"        return values\n\n    def as_string(self) -> str:\n        \"\"\"Read data as a string.\n\n        Raises:\n            ValueError: If the blob cannot be represented as a string.\n\n        Returns:\n            The data as a string.\n        \"\"\"\n        if self.data is None and self.path:\n            return Path(self.path).read_text(encoding=self.encoding)\n        if isinstance(self.data, bytes):\n            return self.data.decode(self.encoding)\n        if isinstance(self.data, str):\n            return self.data\n        msg = f\"Unable to get string for blob {self}\"\n        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)","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/documents/base.py#L156-L192","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Ensure `Blob.data` is a `str` or `bytes`, or that a `path` is set when data is None.","Never construct `Blob(data=None)` without a path — use `Blob.from_path()` instead.","If you control the writer side, serialize the payload (`json.dumps`, `.encode()`) before storing it in a Blob."],"exampleFix":"# before\nblob = Blob(data=None)  # validator passes, as_string() fails\ns = blob.as_string()\n\n# after\nblob = Blob.from_path(\"notes.txt\")\ns = blob.as_string()","handlingStrategy":"type-guard","validationCode":"def blob_can_as_string(blob) -> bool:\n    return (blob.data is None and bool(blob.path)) or isinstance(blob.data, (str, bytes))","typeGuard":"def is_readable_blob(blob: Blob) -> bool:\n    data = blob.data\n    if data is None:\n        return bool(blob.path)\n    return isinstance(data, (str, bytes))","tryCatchPattern":"try:\n    text = blob.as_string()\nexcept ValueError as e:\n    logger.warning(\"Skipping unreadable blob %s: %s\", blob.source, e)\n    continue","preventionTips":["Only store str/bytes in Blob.data.","In generic pipelines, gate as_string() behind an isinstance check on blob.data."],"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"}