langchain-ai/langchain · error · ValueError
Either data or path must be provided
Error message
Either data or path must be provided
What it means
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`.
Source
Thrown at libs/core/langchain_core/documents/base.py:155
def source(self) -> str | None:
"""The source location of the blob as string if known otherwise none.
If a path is associated with the `Blob`, it will default to the path location.
Unless explicitly set via a metadata field called `'source'`, in which
case that value will be used instead.
"""
if self.metadata and "source" in self.metadata:
return cast("str | None", self.metadata["source"])
return str(self.path) if self.path else None
@model_validator(mode="before")
@classmethod
def check_blob_is_valid(cls, values: dict[str, Any]) -> Any:
"""Verify that either data or path is provided."""
if "data" not in values and "path" not in values:
msg = "Either data or path must be provided"
raise ValueError(msg)
return values
def as_string(self) -> str:
"""Read data as a string.
Raises:
ValueError: If the blob cannot be represented as a string.
Returns:
The data as a string.
"""
if self.data is None and self.path:
return Path(self.path).read_text(encoding=self.encoding)
if isinstance(self.data, bytes):
return self.data.decode(self.encoding)
if isinstance(self.data, str):
return self.data
msg = f"Unable to get string for blob {self}"View on GitHub (pinned to e32fa9a52e)
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.
Example fix
// before
blob = Blob(metadata={"source": "a.pdf"})
// after
blob = Blob.from_path("a.pdf", metadata={"source": "a.pdf"}) Defensive patterns
Strategy: validation
Validate before calling
def make_blob(*, data=None, path=None, **kw):
if data is None and path is None:
raise ValueError("Refusing to create Blob without data or path")
return Blob(data=data, path=path, **kw) Type guard
from langchain_core.documents import Blob
def is_valid_blob_source(data, path) -> bool:
return data is not None or path is not None Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Unable to get string for blob {self}
- Unable to get bytes for blob {self}
- Unable to convert blob {self}
- ToolMessage content should be a string or a list of string/d
- If multiple pydantic schemas are provided then args_only sho
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/d7a8f9bdd3c34e31.
Report an issue: GitHub.