{"record":{"id":"d7a8f9bdd3c34e31","repo":"langchain-ai/langchain","slug":"either-data-or-path-must-be-provided","errorCode":null,"errorMessage":"Either data or path must be provided","messagePattern":"Either data or path must be provided","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/documents/base.py","lineNumber":155,"sourceCode":"    def source(self) -> str | None:\n        \"\"\"The source location of the blob as string if known otherwise none.\n\n        If a path is associated with the `Blob`, it will default to the path location.\n\n        Unless explicitly set via a metadata field called `'source'`, in which\n        case that value will be used instead.\n        \"\"\"\n        if self.metadata and \"source\" in self.metadata:\n            return cast(\"str | None\", self.metadata[\"source\"])\n        return str(self.path) if self.path else None\n\n    @model_validator(mode=\"before\")\n    @classmethod\n    def check_blob_is_valid(cls, values: dict[str, Any]) -> Any:\n        \"\"\"Verify that either data or path is provided.\"\"\"\n        if \"data\" not in values and \"path\" not in values:\n            msg = \"Either data or path must be provided\"\n            raise ValueError(msg)\n        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}\"","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/documents/base.py#L137-L173","documentation":"Raised by the `Blob` model validator `check_blob_is_valid` in `langchain_core.documents.base`. A `Blob` represents a piece of content that must be located somewhere: either in memory (`data`) or on the filesystem (`path`). Instantiating a `Blob` with neither field leaves the object with no content to read, so Pydantic rejects it at validation time with a `ValueError`.","triggerScenarios":"Constructing `Blob()` with no arguments, or passing only `metadata`/`mime_type`/`encoding` without `data` or `path`. Also happens when code builds a Blob from a variable that is unexpectedly `None`, e.g. `Blob(data=maybe_none)` — note the key must be present in the values dict, so `Blob(data=None)` actually passes this check but fails later; the error fires only when BOTH keys are absent.","commonSituations":"Migrating loaders from the legacy `Document(loader, blob=...)` pattern where content was attached elsewhere; building Blobs dynamically from user input where the source may be empty; forgetting to pass through the `data` kwarg in a custom loader's super() call.","solutions":["Pass `data` (str or bytes) or `path` (str/Path) when constructing the Blob: `Blob(data=\"text\")` or `Blob.from_path(\"file.txt\")`.","If the value may be None at runtime, guard before construction: `if content is not None: Blob(data=content) else: Blob.from_path(p)`.","Prefer the `Blob.from_path()` / `Blob.from_data()` classmethods, which force you to supply a source explicitly."],"exampleFix":"// before\nblob = Blob(metadata={\"source\": \"a.pdf\"})\n\n// after\nblob = Blob.from_path(\"a.pdf\", metadata={\"source\": \"a.pdf\"})","handlingStrategy":"validation","validationCode":"def make_blob(*, data=None, path=None, **kw):\n    if data is None and path is None:\n        raise ValueError(\"Refusing to create Blob without data or path\")\n    return Blob(data=data, path=path, **kw)","typeGuard":"from langchain_core.documents import Blob\n\ndef is_valid_blob_source(data, path) -> bool:\n    return data is not None or path is not None","tryCatchPattern":null,"preventionTips":["Always construct Blobs via Blob.from_path / Blob.from_data so a source is explicit.","Never pass data=None explicitly; omit the kwarg and set path instead."],"tags":["blob","documents","validation","pydantic"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}