feat(core): add an option to make deserialization more permissive (#32054)

## Description

Currently when deserializing objects that contain non-deserializable
values, we throw an error. However, there are cases (e.g. proxies that
return response fields containing extra fields like Python datetimes),
where these values are not important and we just want to drop them.

Twitter handle: @hacubu

---------

Co-authored-by: Mason Daugherty <github@mdrxy.com>
This commit is contained in:
Jacob Lee
2025-07-15 14:00:01 -07:00
committed by GitHub
parent 3628dccbf3
commit 535ba43b0d
2 changed files with 67 additions and 2 deletions

View File

@@ -232,3 +232,47 @@ def test_serialization_with_pydantic() -> None:
def test_serialization_with_generation() -> None:
generation = Generation(text="hello-world")
assert dumpd(generation)["kwargs"] == {"text": "hello-world", "type": "Generation"}
def test_serialization_with_ignore_unserializable_fields() -> None:
data = {
"messages": [
[
{
"lc": 1,
"type": "constructor",
"id": ["langchain", "schema", "messages", "AIMessage"],
"kwargs": {
"content": "Call tools to get entity details",
"response_metadata": {
"other_field": "foo",
"create_date": {
"lc": 1,
"type": "not_implemented",
"id": ["datetime", "datetime"],
"repr": "datetime.datetime(2025, 7, 15, 13, 14, 0, 000000, tzinfo=datetime.timezone.utc)", # noqa: E501
},
},
"type": "ai",
"id": "00000000-0000-0000-0000-000000000000",
},
},
]
]
}
ser = dumpd(data)
deser = load(ser, ignore_unserializable_fields=True)
assert deser == {
"messages": [
[
AIMessage(
id="00000000-0000-0000-0000-000000000000",
content="Call tools to get entity details",
response_metadata={
"other_field": "foo",
"create_date": None,
},
)
]
]
}