The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
Blob: Add validator and use future annotations (#3650)
Eugene Yurtsev708787dddb2f
diff --git a/langchain/document_loaders/blob_loaders/schema.py b/langchain/document_loaders/blob_loaders/schema.pyindex f4a9f6be07..6ea20fdbb1 100644--- a/langchain/document_loaders/blob_loaders/schema.py+++ b/langchain/document_loaders/blob_loaders/schema.py@@ -4,14 +4,16 @@ The goal is to facilitate decoupling of content loading from content parsing codIn addition, content loading code should provide a lazy loading interface by default."""+from __future__ import annotations+import contextlibimport mimetypesfrom abc import ABC, abstractmethodfrom io import BufferedReader, BytesIOfrom pathlib import PurePath-from typing import Generator, Iterable, Optional, Union+from typing import Any, Generator, Iterable, Mapping, Optional, Union-from pydantic import BaseModel+from pydantic import BaseModel, root_validatorPathLike = Union[str, PurePath]@@ -44,6 +46,13 @@ class Blob(BaseModel):"""The source location of the blob as string if known otherwise none."""return str(self.path) if self.path else None+ @root_validator(pre=True)+ def check_blob_is_valid(cls, values: Mapping[str, Any]) -> Mapping[str, Any]:+ """Verify that either data or path is provided."""+ if "data" not in values and "path" not in values:+ raise ValueError("Either data or path must be provided")+ return values+def as_string(self) -> str:"""Read data as a string."""if self.data is None and self.path:@@ -87,7 +96,7 @@ class Blob(BaseModel):encoding: str = "utf-8",mime_type: Optional[str] = None,guess_type: bool = True,- ) -> "Blob":+ ) -> Blob:"""Load the blob from a path like object.Args:@@ -116,7 +125,7 @@ class Blob(BaseModel):encoding: str = "utf-8",mime_type: Optional[str] = None,path: Optional[str] = None,- ) -> "Blob":+ ) -> Blob:"""Initialize the blob from in-memory data.Args:diff --git a/tests/unit_tests/document_loader/blob_loaders/test_schema.py b/tests/unit_tests/document_loader/blob_loaders/test_schema.pyindex 4791d97613..fa4a3dca47 100644--- a/tests/unit_tests/document_loader/blob_loaders/test_schema.py+++ b/tests/unit_tests/document_loader/blob_loaders/test_schema.py@@ -90,6 +90,15 @@ def test_mime_type_inference(assert blob.mimetype == expected_mime_type+def test_blob_initialization_validator() -> None:+ """Test that blob initialization validates the arguments."""+ with pytest.raises(ValueError, match="Either data or path must be provided"):+ Blob()++ assert Blob(data=b"Hello, World!") is not None+ assert Blob(path="some_path") is not None++def test_blob_loader() -> None:"""Simple test that verifies that we can implement a blob loader."""