Skip to content

The agent engineering platform.

core[patch]: Add unit tests for Serializable (#25152)

Eugene Yurtsev7b1a132affb6
diff --git a/libs/core/tests/unit_tests/load/test_serializable.py b/libs/core/tests/unit_tests/load/test_serializable.py
new file mode 100644
index 0000000000..ce6964933f
--- /dev/null
+++ b/libs/core/tests/unit_tests/load/test_serializable.py
@@ -0,0 +1,71 @@
+from typing import Dict
+
+from langchain_core.load import Serializable, dumpd
+
+
+def test_simple_serialization() -> None:
+ class Foo(Serializable):
+ bar: int
+ baz: str
+
+ foo = Foo(bar=1, baz="hello")
+ assert dumpd(foo) == {
+ "id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
+ "lc": 1,
+ "repr": "Foo(bar=1, baz='hello')",
+ "type": "not_implemented",
+ }
+
+
+def test_simple_serialization_is_serializable() -> None:
+ class Foo(Serializable):
+ bar: int
+ baz: str
+
+ @classmethod
+ def is_lc_serializable(cls) -> bool:
+ return True
+
+ foo = Foo(bar=1, baz="hello")
+ assert foo.lc_id() == ["tests", "unit_tests", "load", "test_serializable", "Foo"]
+ assert dumpd(foo) == {
+ "id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
+ "kwargs": {"bar": 1, "baz": "hello"},
+ "lc": 1,
+ "type": "constructor",
+ }
+
+
+def test_simple_serialization_secret() -> None:
+ """Test handling of secrets."""
+ from langchain_core.load import Serializable
+ from langchain_core.pydantic_v1 import SecretStr
+
+ class Foo(Serializable):
+ bar: int
+ baz: str
+ secret: SecretStr
+ secret_2: str
+
+ @classmethod
+ def is_lc_serializable(cls) -> bool:
+ return True
+
+ @property
+ def lc_secrets(self) -> Dict[str, str]:
+ return {"secret": "MASKED_SECRET", "secret_2": "MASKED_SECRET_2"}
+
+ foo = Foo(
+ bar=1, baz="baz", secret=SecretStr("SUPER_SECRET"), secret_2="SUPER_SECRET"
+ )
+ assert dumpd(foo) == {
+ "id": ["tests", "unit_tests", "load", "test_serializable", "Foo"],
+ "kwargs": {
+ "bar": 1,
+ "baz": "baz",
+ "secret": {"id": ["MASKED_SECRET"], "lc": 1, "type": "secret"},
+ "secret_2": {"id": ["MASKED_SECRET_2"], "lc": 1, "type": "secret"},
+ },
+ "lc": 1,
+ "type": "constructor",
+ }