@@ -56,6 +56,8 @@ def __init__(
56
56
additional_import_mappings : Optional [
57
57
dict [tuple [str , ...], tuple [str , ...]]
58
58
] = None ,
59
+ * ,
60
+ ignore_unserializable_fields : bool = False ,
59
61
) -> None :
60
62
"""Initialize the reviver.
61
63
@@ -70,6 +72,8 @@ def __init__(
70
72
additional_import_mappings: A dictionary of additional namespace mappings
71
73
You can use this to override default mappings or add new mappings.
72
74
Defaults to None.
75
+ ignore_unserializable_fields: Whether to ignore unserializable fields.
76
+ Defaults to False.
73
77
"""
74
78
self .secrets_from_env = secrets_from_env
75
79
self .secrets_map = secrets_map or {}
@@ -88,6 +92,7 @@ def __init__(
88
92
if self .additional_import_mappings
89
93
else ALL_SERIALIZABLE_MAPPINGS
90
94
)
95
+ self .ignore_unserializable_fields = ignore_unserializable_fields
91
96
92
97
def __call__ (self , value : dict [str , Any ]) -> Any :
93
98
"""Revive the value."""
@@ -108,6 +113,8 @@ def __call__(self, value: dict[str, Any]) -> Any:
108
113
and value .get ("type" ) == "not_implemented"
109
114
and value .get ("id" ) is not None
110
115
):
116
+ if self .ignore_unserializable_fields :
117
+ return None
111
118
msg = (
112
119
"Trying to load an object that doesn't implement "
113
120
f"serialization: { value } "
@@ -170,6 +177,7 @@ def loads(
170
177
valid_namespaces : Optional [list [str ]] = None ,
171
178
secrets_from_env : bool = True ,
172
179
additional_import_mappings : Optional [dict [tuple [str , ...], tuple [str , ...]]] = None ,
180
+ ignore_unserializable_fields : bool = False ,
173
181
) -> Any :
174
182
"""Revive a LangChain class from a JSON string.
175
183
@@ -187,14 +195,20 @@ def loads(
187
195
additional_import_mappings: A dictionary of additional namespace mappings
188
196
You can use this to override default mappings or add new mappings.
189
197
Defaults to None.
198
+ ignore_unserializable_fields: Whether to ignore unserializable fields.
199
+ Defaults to False.
190
200
191
201
Returns:
192
202
Revived LangChain objects.
193
203
"""
194
204
return json .loads (
195
205
text ,
196
206
object_hook = Reviver (
197
- secrets_map , valid_namespaces , secrets_from_env , additional_import_mappings
207
+ secrets_map ,
208
+ valid_namespaces ,
209
+ secrets_from_env ,
210
+ additional_import_mappings ,
211
+ ignore_unserializable_fields = ignore_unserializable_fields ,
198
212
),
199
213
)
200
214
@@ -207,6 +221,7 @@ def load(
207
221
valid_namespaces : Optional [list [str ]] = None ,
208
222
secrets_from_env : bool = True ,
209
223
additional_import_mappings : Optional [dict [tuple [str , ...], tuple [str , ...]]] = None ,
224
+ ignore_unserializable_fields : bool = False ,
210
225
) -> Any :
211
226
"""Revive a LangChain class from a JSON object.
212
227
@@ -225,12 +240,18 @@ def load(
225
240
additional_import_mappings: A dictionary of additional namespace mappings
226
241
You can use this to override default mappings or add new mappings.
227
242
Defaults to None.
243
+ ignore_unserializable_fields: Whether to ignore unserializable fields.
244
+ Defaults to False.
228
245
229
246
Returns:
230
247
Revived LangChain objects.
231
248
"""
232
249
reviver = Reviver (
233
- secrets_map , valid_namespaces , secrets_from_env , additional_import_mappings
250
+ secrets_map ,
251
+ valid_namespaces ,
252
+ secrets_from_env ,
253
+ additional_import_mappings ,
254
+ ignore_unserializable_fields = ignore_unserializable_fields ,
234
255
)
235
256
236
257
def _load (obj : Any ) -> Any :
0 commit comments