Home / Class/ Blob Class — langchain Architecture

Blob Class — langchain Architecture

Architecture documentation for the Blob class in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce["Blob"]
  ba6283e7_314f_f690_d884_764090007051["BaseMedia"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|extends| ba6283e7_314f_f690_d884_764090007051
  1241bfcd_16b1_a16e_1fde_a4ccdbf83db2["base.py"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|defined in| 1241bfcd_16b1_a16e_1fde_a4ccdbf83db2
  ed1394c1_18b5_f0e8_d7b0_0539c75e74b2["source()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| ed1394c1_18b5_f0e8_d7b0_0539c75e74b2
  8fb39201_3ed0_1cbe_189d_79e589c5ac31["check_blob_is_valid()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| 8fb39201_3ed0_1cbe_189d_79e589c5ac31
  7bcae93a_806d_7517_0417_b3d6b2e74ec9["as_string()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| 7bcae93a_806d_7517_0417_b3d6b2e74ec9
  a3733f9c_e6f4_c6f3_5422_afff1eac0afd["as_bytes()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| a3733f9c_e6f4_c6f3_5422_afff1eac0afd
  7d96445f_f767_4973_d287_f97b238652be["as_bytes_io()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| 7d96445f_f767_4973_d287_f97b238652be
  26c75129_62f1_2055_48d2_3dbb4f17a9f5["from_path()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| 26c75129_62f1_2055_48d2_3dbb4f17a9f5
  7e943d0f_ce28_7496_7cee_ed19578b1515["from_data()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| 7e943d0f_ce28_7496_7cee_ed19578b1515
  e138873e_4039_bd75_8387_5a598677b2f4["__repr__()"]
  3557cb87_7ae6_78b3_0aa2_bbb7f023fdce -->|method| e138873e_4039_bd75_8387_5a598677b2f4

Relationship Graph

Source Code

libs/core/langchain_core/documents/base.py lines 59–285

class Blob(BaseMedia):
    """Raw data abstraction for document loading and file processing.

    Represents raw bytes or text, either in-memory or by file reference. Used
    primarily by document loaders to decouple data loading from parsing.

    Inspired by [Mozilla's `Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob)

    ???+ example "Initialize a blob from in-memory data"

        ```python
        from langchain_core.documents import Blob

        blob = Blob.from_data("Hello, world!")

        # Read the blob as a string
        print(blob.as_string())

        # Read the blob as bytes
        print(blob.as_bytes())

        # Read the blob as a byte stream
        with blob.as_bytes_io() as f:
            print(f.read())
        ```

    ??? example "Load from memory and specify MIME type and metadata"

        ```python
        from langchain_core.documents import Blob

        blob = Blob.from_data(
            data="Hello, world!",
            mime_type="text/plain",
            metadata={"source": "https://example.com"},
        )
        ```

    ??? example "Load the blob from a file"

        ```python
        from langchain_core.documents import Blob

        blob = Blob.from_path("path/to/file.txt")

        # Read the blob as a string
        print(blob.as_string())

        # Read the blob as bytes
        print(blob.as_bytes())

        # Read the blob as a byte stream
        with blob.as_bytes_io() as f:
            print(f.read())
        ```
    """

    data: bytes | str | None = None
    """Raw data associated with the `Blob`."""

    mimetype: str | None = None
    """MIME type, not to be confused with a file extension."""

    encoding: str = "utf-8"
    """Encoding to use if decoding the bytes into a string.

    Uses `utf-8` as default encoding if decoding to string.
    """

    path: PathLike | None = None
    """Location where the original content was found."""

    model_config = ConfigDict(
        arbitrary_types_allowed=True,
        frozen=True,
    )

    @property
    def source(self) -> str | None:
        """The source location of the blob as string if known otherwise none.

Extends

Frequently Asked Questions

What is the Blob class?
Blob is a class in the langchain codebase, defined in libs/core/langchain_core/documents/base.py.
Where is Blob defined?
Blob is defined in libs/core/langchain_core/documents/base.py at line 59.
What does Blob extend?
Blob extends BaseMedia.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free