fix(core): don't hand the str builtin to PlainSerializer

Pydantic < 2.9 calls `inspect.signature()` on an annotated serializer to decide
whether to pass an `info` argument, and that raises `ValueError: no signature
found for builtin type <class 'str'>`. The failure is at class creation, so
importing langchain_core.tools broke entirely on pydantic 2.7/2.8.

Use a small named function instead. Verified on pydantic 2.7.4 and 2.13.4.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Emil FilipovandClaude Opus 5 committed 2026-08-13 21:15:20 +02:00
1 parent d6cb258484
commit 1d993ca683
1 file changed
+10 -1
+10 -1
View File
@@ -37,6 +37,15 @@ if TYPE_CHECKING:
from langchain_core.messages import ToolCall
def _serialize_as_str(value: Any) -> str:
"""Stringify a value that has no JSON form.
A plain function rather than the `str` builtin: Pydantic < 2.9 inspects the
serializer's signature, and `inspect.signature` raises for C builtins.
"""
return str(value)
def _serialize_args_schema(args_schema: ArgsSchema) -> Any:
"""Represent a schema class as a string when dumping to JSON.
@@ -56,7 +65,7 @@ def _serialize_args_schema(args_schema: ArgsSchema) -> Any:
_JsonSchemaFallback = PlainSerializer(
_serialize_args_schema, when_used="json-unless-none"
)
_JsonCallableFallback = PlainSerializer(str, when_used="json-unless-none")
_JsonCallableFallback = PlainSerializer(_serialize_as_str, when_used="json-unless-none")
class StructuredTool(BaseTool):