diff --git a/libs/core/langchain_core/utils/pydantic.py b/libs/core/langchain_core/utils/pydantic.py index dd6b9aba597..fb4a72a29c7 100644 --- a/libs/core/langchain_core/utils/pydantic.py +++ b/libs/core/langchain_core/utils/pydantic.py @@ -200,6 +200,8 @@ def v1_repr(obj: BaseModel) -> str: if isinstance(value, BaseModel): repr_.append(f"{name}={v1_repr(value)}") else: + if field.exclude: + continue if not field.is_required(): if not value: continue diff --git a/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr b/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr index 3d9d2461646..29ab354d126 100644 --- a/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr +++ b/libs/core/tests/unit_tests/prompts/__snapshots__/test_chat.ambr @@ -167,7 +167,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/InvalidToolCall', + '$ref': '#/$defs/InvalidToolCall', }), 'title': 'Invalid Tool Calls', 'type': 'array', @@ -192,7 +192,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCallChunk', + '$ref': '#/$defs/ToolCallChunk', }), 'title': 'Tool Call Chunks', 'type': 'array', @@ -201,7 +201,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCall', + '$ref': '#/$defs/ToolCall', }), 'title': 'Tool Calls', 'type': 'array', @@ -218,7 +218,7 @@ 'usage_metadata': dict({ 'anyOf': list([ dict({ - '$ref': '#/definitions/UsageMetadata', + '$ref': '#/$defs/UsageMetadata', }), dict({ 'type': 'null', @@ -1224,6 +1224,7 @@ 'type': 'object', }), 'artifact': dict({ + 'default': None, 'title': 'Artifact', }), 'content': dict({ @@ -1368,22 +1369,22 @@ '$ref': '#/$defs/ToolMessage', }), dict({ - '$ref': '#/definitions/AIMessageChunk', + '$ref': '#/$defs/AIMessageChunk', }), dict({ - '$ref': '#/definitions/HumanMessageChunk', + '$ref': '#/$defs/HumanMessageChunk', }), dict({ - '$ref': '#/definitions/ChatMessageChunk', + '$ref': '#/$defs/ChatMessageChunk', }), dict({ - '$ref': '#/definitions/SystemMessageChunk', + '$ref': '#/$defs/SystemMessageChunk', }), dict({ - '$ref': '#/definitions/FunctionMessageChunk', + '$ref': '#/$defs/FunctionMessageChunk', }), dict({ - '$ref': '#/definitions/ToolMessageChunk', + '$ref': '#/$defs/ToolMessageChunk', }), ]), }), @@ -1570,7 +1571,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/InvalidToolCall', + '$ref': '#/$defs/InvalidToolCall', }), 'title': 'Invalid Tool Calls', 'type': 'array', @@ -1595,7 +1596,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCallChunk', + '$ref': '#/$defs/ToolCallChunk', }), 'title': 'Tool Call Chunks', 'type': 'array', @@ -1604,7 +1605,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCall', + '$ref': '#/$defs/ToolCall', }), 'title': 'Tool Calls', 'type': 'array', @@ -1621,7 +1622,7 @@ 'usage_metadata': dict({ 'anyOf': list([ dict({ - '$ref': '#/definitions/UsageMetadata', + '$ref': '#/$defs/UsageMetadata', }), dict({ 'type': 'null', @@ -2627,6 +2628,7 @@ 'type': 'object', }), 'artifact': dict({ + 'default': None, 'title': 'Artifact', }), 'content': dict({ @@ -2770,22 +2772,22 @@ '$ref': '#/$defs/ToolMessage', }), dict({ - '$ref': '#/definitions/AIMessageChunk', + '$ref': '#/$defs/AIMessageChunk', }), dict({ - '$ref': '#/definitions/HumanMessageChunk', + '$ref': '#/$defs/HumanMessageChunk', }), dict({ - '$ref': '#/definitions/ChatMessageChunk', + '$ref': '#/$defs/ChatMessageChunk', }), dict({ - '$ref': '#/definitions/SystemMessageChunk', + '$ref': '#/$defs/SystemMessageChunk', }), dict({ - '$ref': '#/definitions/FunctionMessageChunk', + '$ref': '#/$defs/FunctionMessageChunk', }), dict({ - '$ref': '#/definitions/ToolMessageChunk', + '$ref': '#/$defs/ToolMessageChunk', }), ]), }), diff --git a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr index ea1ec005fee..074fe7ddba3 100644 --- a/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr +++ b/libs/core/tests/unit_tests/runnables/__snapshots__/test_runnable.ambr @@ -5344,7 +5344,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/InvalidToolCall', + '$ref': '#/$defs/InvalidToolCall', }), 'title': 'Invalid Tool Calls', 'type': 'array', @@ -5369,7 +5369,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCallChunk', + '$ref': '#/$defs/ToolCallChunk', }), 'title': 'Tool Call Chunks', 'type': 'array', @@ -5378,7 +5378,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCall', + '$ref': '#/$defs/ToolCall', }), 'title': 'Tool Calls', 'type': 'array', @@ -5395,7 +5395,7 @@ 'usage_metadata': dict({ 'anyOf': list([ dict({ - '$ref': '#/definitions/UsageMetadata', + '$ref': '#/$defs/UsageMetadata', }), dict({ 'type': 'null', @@ -6401,6 +6401,7 @@ 'type': 'object', }), 'artifact': dict({ + 'default': None, 'title': 'Artifact', }), 'content': dict({ @@ -6544,22 +6545,22 @@ '$ref': '#/$defs/ToolMessage', }), dict({ - '$ref': '#/definitions/AIMessageChunk', + '$ref': '#/$defs/AIMessageChunk', }), dict({ - '$ref': '#/definitions/HumanMessageChunk', + '$ref': '#/$defs/HumanMessageChunk', }), dict({ - '$ref': '#/definitions/ChatMessageChunk', + '$ref': '#/$defs/ChatMessageChunk', }), dict({ - '$ref': '#/definitions/SystemMessageChunk', + '$ref': '#/$defs/SystemMessageChunk', }), dict({ - '$ref': '#/definitions/FunctionMessageChunk', + '$ref': '#/$defs/FunctionMessageChunk', }), dict({ - '$ref': '#/definitions/ToolMessageChunk', + '$ref': '#/$defs/ToolMessageChunk', }), ]), }), @@ -6742,7 +6743,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/InvalidToolCall', + '$ref': '#/$defs/InvalidToolCall', }), 'title': 'Invalid Tool Calls', 'type': 'array', @@ -6767,7 +6768,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCallChunk', + '$ref': '#/$defs/ToolCallChunk', }), 'title': 'Tool Call Chunks', 'type': 'array', @@ -6776,7 +6777,7 @@ 'default': list([ ]), 'items': dict({ - '$ref': '#/definitions/ToolCall', + '$ref': '#/$defs/ToolCall', }), 'title': 'Tool Calls', 'type': 'array', @@ -6793,7 +6794,7 @@ 'usage_metadata': dict({ 'anyOf': list([ dict({ - '$ref': '#/definitions/UsageMetadata', + '$ref': '#/$defs/UsageMetadata', }), dict({ 'type': 'null', @@ -6992,22 +6993,22 @@ '$ref': '#/$defs/ToolMessage', }), dict({ - '$ref': '#/definitions/AIMessageChunk', + '$ref': '#/$defs/AIMessageChunk', }), dict({ - '$ref': '#/definitions/HumanMessageChunk', + '$ref': '#/$defs/HumanMessageChunk', }), dict({ - '$ref': '#/definitions/ChatMessageChunk', + '$ref': '#/$defs/ChatMessageChunk', }), dict({ - '$ref': '#/definitions/SystemMessageChunk', + '$ref': '#/$defs/SystemMessageChunk', }), dict({ - '$ref': '#/definitions/FunctionMessageChunk', + '$ref': '#/$defs/FunctionMessageChunk', }), dict({ - '$ref': '#/definitions/ToolMessageChunk', + '$ref': '#/$defs/ToolMessageChunk', }), ]), }), @@ -7888,6 +7889,7 @@ 'type': 'object', }), 'artifact': dict({ + 'default': None, 'title': 'Artifact', }), 'content': dict({ diff --git a/libs/core/tests/unit_tests/utils/test_pydantic.py b/libs/core/tests/unit_tests/utils/test_pydantic.py index c579c105b22..5bba5e81167 100644 --- a/libs/core/tests/unit_tests/utils/test_pydantic.py +++ b/libs/core/tests/unit_tests/utils/test_pydantic.py @@ -12,6 +12,7 @@ from langchain_core.utils.pydantic import ( is_basemodel_instance, is_basemodel_subclass, pre_init, + v1_repr, ) @@ -194,3 +195,17 @@ def test_fields_pydantic_v1_from_2() -> None: fields = get_fields(Foo) assert fields == {"x": Foo.__fields__["x"]} + + +def test_v1_repr() -> None: + from pydantic import BaseModel, Field + + class Foo(BaseModel): + bar: int = Field(default=1, alias="baz", exclude=True) + x: int = Field(default=2) + + def __repr__(self) -> str: + """Custom repr.""" + return v1_repr(self) + + assert repr(Foo(bar=2, x=3)) == "Foo(x=3)" # type: ignore[call-arg]