Skip to content

The agent engineering platform.

core[patch]: check for model_fields attribute (#25108)

ccurme803eba3163dc
diff --git a/libs/core/langchain_core/tools.py b/libs/core/langchain_core/tools.py
index 98a86b0aaf..7693a4fa68 100644
--- a/libs/core/langchain_core/tools.py
+++ b/libs/core/langchain_core/tools.py
@@ -1666,7 +1666,10 @@ def _get_all_basemodel_annotations(
for name, param in inspect.signature(cls).parameters.items():
# Exclude hidden init args added by pydantic Config. For example if
# BaseModel(extra="allow") then "extra_data" will part of init sig.
- if (fields := getattr(cls, "__fields__", {})) and name not in fields:
+ if (
+ fields := getattr(cls, "model_fields", {}) # pydantic v2+
+ or getattr(cls, "__fields__", {}) # pydantic v1
+ ) and name not in fields:
continue
annotations[name] = param.annotation
orig_bases: Tuple = getattr(cls, "__orig_bases__", tuple())
diff --git a/libs/core/tests/unit_tests/test_tools.py b/libs/core/tests/unit_tests/test_tools.py
index 19b24b041a..47d13863a1 100644
--- a/libs/core/tests/unit_tests/test_tools.py
+++ b/libs/core/tests/unit_tests/test_tools.py
@@ -1746,6 +1746,7 @@ def test__is_message_content_type(obj: Any, expected: bool) -> None:
@pytest.mark.skipif(PYDANTIC_MAJOR_VERSION != 2, reason="Testing pydantic v2.")
@pytest.mark.parametrize("use_v1_namespace", [True, False])
def test__get_all_basemodel_annotations_v2(use_v1_namespace: bool) -> None:
A = TypeVar("A")