core[patch]: Fix issue with adapter utility for pydantic repr (#26099)

This repr will be deleted prior to release -- it's temporarily here to
make it easy to separate code changes in langchain vs. code changes
stemming from breaking changes in pydantic
This commit is contained in:
Eugene Yurtsev
2024-09-05 12:27:01 -04:00
committed by GitHub
parent a267da6a3a
commit 1c51e1693d
4 changed files with 61 additions and 40 deletions

View File

@@ -200,6 +200,8 @@ def v1_repr(obj: BaseModel) -> str:
if isinstance(value, BaseModel): if isinstance(value, BaseModel):
repr_.append(f"{name}={v1_repr(value)}") repr_.append(f"{name}={v1_repr(value)}")
else: else:
if field.exclude:
continue
if not field.is_required(): if not field.is_required():
if not value: if not value:
continue continue

View File

@@ -167,7 +167,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/InvalidToolCall', '$ref': '#/$defs/InvalidToolCall',
}), }),
'title': 'Invalid Tool Calls', 'title': 'Invalid Tool Calls',
'type': 'array', 'type': 'array',
@@ -192,7 +192,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCallChunk', '$ref': '#/$defs/ToolCallChunk',
}), }),
'title': 'Tool Call Chunks', 'title': 'Tool Call Chunks',
'type': 'array', 'type': 'array',
@@ -201,7 +201,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCall', '$ref': '#/$defs/ToolCall',
}), }),
'title': 'Tool Calls', 'title': 'Tool Calls',
'type': 'array', 'type': 'array',
@@ -218,7 +218,7 @@
'usage_metadata': dict({ 'usage_metadata': dict({
'anyOf': list([ 'anyOf': list([
dict({ dict({
'$ref': '#/definitions/UsageMetadata', '$ref': '#/$defs/UsageMetadata',
}), }),
dict({ dict({
'type': 'null', 'type': 'null',
@@ -1224,6 +1224,7 @@
'type': 'object', 'type': 'object',
}), }),
'artifact': dict({ 'artifact': dict({
'default': None,
'title': 'Artifact', 'title': 'Artifact',
}), }),
'content': dict({ 'content': dict({
@@ -1368,22 +1369,22 @@
'$ref': '#/$defs/ToolMessage', '$ref': '#/$defs/ToolMessage',
}), }),
dict({ dict({
'$ref': '#/definitions/AIMessageChunk', '$ref': '#/$defs/AIMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/HumanMessageChunk', '$ref': '#/$defs/HumanMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ChatMessageChunk', '$ref': '#/$defs/ChatMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/SystemMessageChunk', '$ref': '#/$defs/SystemMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/FunctionMessageChunk', '$ref': '#/$defs/FunctionMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ToolMessageChunk', '$ref': '#/$defs/ToolMessageChunk',
}), }),
]), ]),
}), }),
@@ -1570,7 +1571,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/InvalidToolCall', '$ref': '#/$defs/InvalidToolCall',
}), }),
'title': 'Invalid Tool Calls', 'title': 'Invalid Tool Calls',
'type': 'array', 'type': 'array',
@@ -1595,7 +1596,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCallChunk', '$ref': '#/$defs/ToolCallChunk',
}), }),
'title': 'Tool Call Chunks', 'title': 'Tool Call Chunks',
'type': 'array', 'type': 'array',
@@ -1604,7 +1605,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCall', '$ref': '#/$defs/ToolCall',
}), }),
'title': 'Tool Calls', 'title': 'Tool Calls',
'type': 'array', 'type': 'array',
@@ -1621,7 +1622,7 @@
'usage_metadata': dict({ 'usage_metadata': dict({
'anyOf': list([ 'anyOf': list([
dict({ dict({
'$ref': '#/definitions/UsageMetadata', '$ref': '#/$defs/UsageMetadata',
}), }),
dict({ dict({
'type': 'null', 'type': 'null',
@@ -2627,6 +2628,7 @@
'type': 'object', 'type': 'object',
}), }),
'artifact': dict({ 'artifact': dict({
'default': None,
'title': 'Artifact', 'title': 'Artifact',
}), }),
'content': dict({ 'content': dict({
@@ -2770,22 +2772,22 @@
'$ref': '#/$defs/ToolMessage', '$ref': '#/$defs/ToolMessage',
}), }),
dict({ dict({
'$ref': '#/definitions/AIMessageChunk', '$ref': '#/$defs/AIMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/HumanMessageChunk', '$ref': '#/$defs/HumanMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ChatMessageChunk', '$ref': '#/$defs/ChatMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/SystemMessageChunk', '$ref': '#/$defs/SystemMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/FunctionMessageChunk', '$ref': '#/$defs/FunctionMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ToolMessageChunk', '$ref': '#/$defs/ToolMessageChunk',
}), }),
]), ]),
}), }),

View File

@@ -5344,7 +5344,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/InvalidToolCall', '$ref': '#/$defs/InvalidToolCall',
}), }),
'title': 'Invalid Tool Calls', 'title': 'Invalid Tool Calls',
'type': 'array', 'type': 'array',
@@ -5369,7 +5369,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCallChunk', '$ref': '#/$defs/ToolCallChunk',
}), }),
'title': 'Tool Call Chunks', 'title': 'Tool Call Chunks',
'type': 'array', 'type': 'array',
@@ -5378,7 +5378,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCall', '$ref': '#/$defs/ToolCall',
}), }),
'title': 'Tool Calls', 'title': 'Tool Calls',
'type': 'array', 'type': 'array',
@@ -5395,7 +5395,7 @@
'usage_metadata': dict({ 'usage_metadata': dict({
'anyOf': list([ 'anyOf': list([
dict({ dict({
'$ref': '#/definitions/UsageMetadata', '$ref': '#/$defs/UsageMetadata',
}), }),
dict({ dict({
'type': 'null', 'type': 'null',
@@ -6401,6 +6401,7 @@
'type': 'object', 'type': 'object',
}), }),
'artifact': dict({ 'artifact': dict({
'default': None,
'title': 'Artifact', 'title': 'Artifact',
}), }),
'content': dict({ 'content': dict({
@@ -6544,22 +6545,22 @@
'$ref': '#/$defs/ToolMessage', '$ref': '#/$defs/ToolMessage',
}), }),
dict({ dict({
'$ref': '#/definitions/AIMessageChunk', '$ref': '#/$defs/AIMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/HumanMessageChunk', '$ref': '#/$defs/HumanMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ChatMessageChunk', '$ref': '#/$defs/ChatMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/SystemMessageChunk', '$ref': '#/$defs/SystemMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/FunctionMessageChunk', '$ref': '#/$defs/FunctionMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ToolMessageChunk', '$ref': '#/$defs/ToolMessageChunk',
}), }),
]), ]),
}), }),
@@ -6742,7 +6743,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/InvalidToolCall', '$ref': '#/$defs/InvalidToolCall',
}), }),
'title': 'Invalid Tool Calls', 'title': 'Invalid Tool Calls',
'type': 'array', 'type': 'array',
@@ -6767,7 +6768,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCallChunk', '$ref': '#/$defs/ToolCallChunk',
}), }),
'title': 'Tool Call Chunks', 'title': 'Tool Call Chunks',
'type': 'array', 'type': 'array',
@@ -6776,7 +6777,7 @@
'default': list([ 'default': list([
]), ]),
'items': dict({ 'items': dict({
'$ref': '#/definitions/ToolCall', '$ref': '#/$defs/ToolCall',
}), }),
'title': 'Tool Calls', 'title': 'Tool Calls',
'type': 'array', 'type': 'array',
@@ -6793,7 +6794,7 @@
'usage_metadata': dict({ 'usage_metadata': dict({
'anyOf': list([ 'anyOf': list([
dict({ dict({
'$ref': '#/definitions/UsageMetadata', '$ref': '#/$defs/UsageMetadata',
}), }),
dict({ dict({
'type': 'null', 'type': 'null',
@@ -6992,22 +6993,22 @@
'$ref': '#/$defs/ToolMessage', '$ref': '#/$defs/ToolMessage',
}), }),
dict({ dict({
'$ref': '#/definitions/AIMessageChunk', '$ref': '#/$defs/AIMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/HumanMessageChunk', '$ref': '#/$defs/HumanMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ChatMessageChunk', '$ref': '#/$defs/ChatMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/SystemMessageChunk', '$ref': '#/$defs/SystemMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/FunctionMessageChunk', '$ref': '#/$defs/FunctionMessageChunk',
}), }),
dict({ dict({
'$ref': '#/definitions/ToolMessageChunk', '$ref': '#/$defs/ToolMessageChunk',
}), }),
]), ]),
}), }),
@@ -7888,6 +7889,7 @@
'type': 'object', 'type': 'object',
}), }),
'artifact': dict({ 'artifact': dict({
'default': None,
'title': 'Artifact', 'title': 'Artifact',
}), }),
'content': dict({ 'content': dict({

View File

@@ -12,6 +12,7 @@ from langchain_core.utils.pydantic import (
is_basemodel_instance, is_basemodel_instance,
is_basemodel_subclass, is_basemodel_subclass,
pre_init, pre_init,
v1_repr,
) )
@@ -194,3 +195,17 @@ def test_fields_pydantic_v1_from_2() -> None:
fields = get_fields(Foo) fields = get_fields(Foo)
assert fields == {"x": Foo.__fields__["x"]} 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]