Discussion:
Serializing pydantic enums
(too old to reply)
Larry Martell
2024-05-28 12:48:22 UTC
Permalink
Just getting started with pydantic. I have this example code:

class FinishReason(Enum):
stop = 'stop'

class Choice(BaseModel):
finish_reason: FinishReason = Field(...)


But I cannot serialize this:

json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable


I get the object not the value:

(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=<FinishReason.stop: 'stop'>)


Also tried it with .value, same result.

What am I missing here?
Left Right
2024-05-28 13:23:49 UTC
Permalink
Most Python objects aren't serializable into JSON. Pydantic isn't
special in this sense.

What can you do about this? -- Well, if this is a one-of situation,
then, maybe just do it by hand?

If this is a recurring problem: json.dumps() takes a cls argument that
will be used to do the serialization. Extend json.JSONEncoder and
implement the encode() method for the encoder class you are passing. I
believe that the official docs have some information about this too.

On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
Post by Larry Martell
stop = 'stop'
finish_reason: FinishReason = Field(...)
json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable
(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=<FinishReason.stop: 'stop'>)
Also tried it with .value, same result.
What am I missing here?
--
https://mail.python.org/mailman/listinfo/python-list
Larry Martell
2024-05-29 19:27:04 UTC
Permalink
On Tue, May 28, 2024 at 11:46 AM Left Right via Python-list
Post by Left Right
Most Python objects aren't serializable into JSON. Pydantic isn't
special in this sense.
What can you do about this? -- Well, if this is a one-of situation,
then, maybe just do it by hand?
If this is a recurring problem: json.dumps() takes a cls argument that
will be used to do the serialization. Extend json.JSONEncoder and
implement the encode() method for the encoder class you are passing. I
believe that the official docs have some information about this too.
Yeah, I know I can do this, but I seem to recall reading that pydantic
handled serialization. Guess not.
Post by Left Right
On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
Post by Larry Martell
stop = 'stop'
finish_reason: FinishReason = Field(...)
json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable
(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=<FinishReason.stop: 'stop'>)
Also tried it with .value, same result.
What am I missing here?
Larry Martell
2024-05-29 22:29:28 UTC
Permalink
Post by Larry Martell
On Tue, May 28, 2024 at 11:46 AM Left Right via Python-list
Post by Left Right
Most Python objects aren't serializable into JSON. Pydantic isn't
special in this sense.
What can you do about this? -- Well, if this is a one-of situation,
then, maybe just do it by hand?
If this is a recurring problem: json.dumps() takes a cls argument that
will be used to do the serialization. Extend json.JSONEncoder and
implement the encode() method for the encoder class you are passing. I
believe that the official docs have some information about this too.
Yeah, I know I can do this, but I seem to recall reading that pydantic
handled serialization. Guess not.
Actually it's as simple as adding this to any model that uses an enum model:

class Config:
use_enum_values = True
Post by Larry Martell
Post by Left Right
On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
Post by Larry Martell
stop = 'stop'
finish_reason: FinishReason = Field(...)
json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable
(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=<FinishReason.stop: 'stop'>)
Also tried it with .value, same result.
What am I missing here?
Mats Wichmann
2024-05-29 23:02:08 UTC
Permalink
Post by Larry Martell
On Tue, May 28, 2024 at 11:46 AM Left Right via Python-list
Post by Left Right
Most Python objects aren't serializable into JSON. Pydantic isn't
special in this sense.
What can you do about this? -- Well, if this is a one-of situation,
then, maybe just do it by hand?
If this is a recurring problem: json.dumps() takes a cls argument that
will be used to do the serialization. Extend json.JSONEncoder and
implement the encode() method for the encoder class you are passing. I
believe that the official docs have some information about this too.
Yeah, I know I can do this, but I seem to recall reading that pydantic
handled serialization. Guess not.
Pydantic devotes some of its documentation to serialization.

https://docs.pydantic.dev/latest/concepts/serialization/

As noted elsewhere, some Python objects are easy to serialize, some you
need to provide some help. Consider pickling: if you write a class that
isn't obviously pickleable, the getstate dunder method can be defined to
help out. For Pydantic, there's a couple of ways... aliases in
particular seem designed to help: there's a serialization_alias argument
to the Field function.

Loading...