mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-14 14:05:37 +00:00
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:
@@ -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,
|
||||
},
|
||||
)
|
||||
]
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user