-
-
Notifications
You must be signed in to change notification settings - Fork 181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pydantic>=2.5 classes can't be serialized #650
Comments
I also encountered this issue. Not sure if the issue is on This the minimum code required to reproduce the error with import dill
from pydantic import BaseModel
class MyModel(BaseModel):
pass
dill.loads(dill.dumps(MyModel())) and the error:
This only occurs if the |
If earlier versions of |
Thanks for the info @mmckerns. I'll do some more tests and try to figure out what's happening. |
Also facing this issue — we use pydantic for our run configs in ML experiments, and pickling is essential since we do distributed training. Any updates would be super helpful! Thank you |
Any updates here? Also facing the issue. Does anyone know how to solve this? Thanks! |
my suggestion is to set |
ofcourse, here is a minimal reproduce. from pydantic import BaseModel
import dill
class A(BaseModel):
foo: str
dill.detect.trace(True)
dill.dumps(A) output: ┬ T2: <class '__main__.A'>
├┬ ABC: <class '__main__.A'>
│└ # ABC
├┬ F2: <function _create_type at 0x101ef7f60>
│└ # F2 [30 B]
├┬ T4: <class 'pydantic._internal._model_construction.ModelMetaclass'>
│└ # T4 [60 B]
├┬ T4: <class 'pydantic.main.BaseModel'>
│└ # T4 [30 B]
├┬ D2: <dict object at 0x0102058f80>
│├┬ D2: <dict object at 0x0101392cc0>
││├┬ T1: <class 'str'>
│││├┬ F2: <function _load_type at 0x101ef7ec0>
││││└ # F2 [17 B]
│││└ # T1 [27 B]
││└ # D2 [36 B]
│├┬ D2: <dict object at 0x010128cfc0>
││└ # D2 [2 B]
│├┬ D2: <dict object at 0x0102058f40>
││└ # D2 [2 B]
│├┬ T4: <class 'pydantic._internal._decorators.DecoratorInfos'>
││└ # T4 [52 B]
│├┬ D2: <dict object at 0x01020ffd40>
││├┬ D2: <dict object at 0x01017d8d40>
│││└ # D2 [2 B]
││├┬ D2: <dict object at 0x010128f500>
│││└ # D2 [2 B]
││├┬ D2: <dict object at 0x0102075540>
│││└ # D2 [2 B]
││├┬ D2: <dict object at 0x01020fc980>
│││└ # D2 [2 B]
││├┬ D2: <dict object at 0x01020fc940>
│││└ # D2 [2 B]
││├┬ D2: <dict object at 0x01020fc900>
│││└ # D2 [2 B]
││├┬ D2: <dict object at 0x01020fc8c0>
│││└ # D2 [2 B]
││└ # D2 [145 B]
│├┬ D2: <dict object at 0x01020fce00>
││└ # D2 [36 B]
│├┬ D2: <dict object at 0x01020fcac0>
││├┬ T4: <class 'pydantic.fields.FieldInfo'>
│││└ # T4 [32 B]
││├┬ D2: <dict object at 0x01018e8c40>
│││├┬ D2: <dict object at 0x01020ffe80>
││││└ # D2 [11 B]
│││└ # D2 [419 B]
││└ # D2 [463 B]
│├┬ D2: <dict object at 0x0102176ec0>
││├┬ T4: <class '__main__.A'>
/opt/miniconda3/lib/python3.12/site-packages/dill/_dill.py:422: PicklingWarning: Cannot locate reference to <class '__main__.A'>.
StockPickler.save(self, obj, save_persistent_id)
/opt/miniconda3/lib/python3.12/site-packages/dill/_dill.py:422: PicklingWarning: Cannot pickle <class '__main__.A'>: __main__.A has recursive self-references that trigger a RecursionError.
StockPickler.save(self, obj, save_persistent_id)
│││└ # T4 [6 B]
││├┬ D2: <dict object at 0x0102176e40>
│││├┬ D2: <dict object at 0x0102176e00>
││││├┬ D2: <dict object at 0x0102176dc0>
│││││├┬ D2: <dict object at 0x0102176d40>
││││││└ # D2 [7 B]
│││││├┬ D2: <dict object at 0x0102176d00>
││││││└ # D2 [2 B]
│││││└ # D2 [33 B]
││││└ # D2 [38 B]
│││└ # D2 [87 B]
││├┬ D2: <dict object at 0x0102176cc0>
│││└ # D2 [7 B]
││├┬ D2: <dict object at 0x0102176c80>
│││├┬ Me1: <bound method BaseModel.__get_pydantic_json_schema__ of <class '__main__.A'>>
││││├┬ T1: <class 'method'>
│││││└ # T1 [19 B]
││││├┬ F1: <function BaseModel.__get_pydantic_json_schema__ at 0x10195c9a0>
│││││├┬ F2: <function _create_function at 0x101f00040>
││││││└ # F2 [23 B]
│││││├┬ Co: <code object __get_pydantic_json_schema__ at 0x10136a5d0, file "/opt/miniconda3/lib/python3.12/site-packages/pydantic/main.py", line 704>
││││││├┬ F2: <function _create_code at 0x101f00540>
│││││││└ # F2 [19 B]
││││││└ # Co [1 MiB]
│││││├┬ D4: <dict object at 0x010122be80>
││││││└ # D4 [24 B]
│││││├┬ D2: <dict object at 0x01020fcd80>
││││││└ # D2 [2 B]
│││││├┬ D2: <dict object at 0x01021085c0>
││││││├┬ D2: <dict object at 0x010194f580>
│││││││└ # D2 [71 B]
││││││└ # D2 [98 B]
│││││└ # F1 [1 MiB]
││││└ # Me1 [1 MiB]
│││└ # D2 [1 MiB]
││└ # D2 [1 MiB]
│├┬ T4: <class 'pydantic_core._pydantic_core.SchemaValidator'>
││└ # T4 [51 B]
│├┬ D2: <dict object at 0x01020fe380>
││└ # D2 [7 B]
│├┬ T4: <class 'pydantic_core._pydantic_core.SchemaSerializer'>
││└ # T4 [52 B]
│├┬ T4: <class 'pydantic._internal._utils.LazyClassAttribute'>
││└ # T4 [51 B]
│├┬ D2: <dict object at 0x01020ffec0>
││├┬ T1: <class 'functools.partial'>
│││└ # T1 [20 B]
││├┬ F2: <function generate_pydantic_signature at 0x10195aca0>
│││└ # F2 [64 B]
││├┬ D2: <dict object at 0x01022f3000>
│││├┬ F2: <function BaseModel.__init__ at 0x10195be20>
││││└ # F2 [25 B]
│││└ # D2 [64 B]
││└ # D2 [185 B]
│├┬ D2: <dict object at 0x01020fdac0>
││└ # D2 [2 B]
│└ # D2 [3 MiB]
└ # T2 [3 MiB] environment:
|
@zhc7: ... and I assume it throws the same error noted earlier upon load? What if you dump with |
from pydantic import BaseModel
import dill
dill.settings['recurse'] = True
class A(BaseModel):
foo: str
dill.detect.trace(True)
dill.dumps(A)
The warning still exists. |
@zhc7: Are you seeing an error upon |
same example works with
cloudpickle
The text was updated successfully, but these errors were encountered: