Serializing pydantic enums

Liste des GroupesRevenir à cl python 
Sujet : Serializing pydantic enums
De : larry.martell (at) *nospam* gmail.com (Larry Martell)
Groupes : comp.lang.python
Date : 28. May 2024, 13:48:22
Autres entêtes
Message-ID : <mailman.50.1716900518.2909.python-list@python.org>
References : 1
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?

Date Sujet#  Auteur
28 May 24 o Serializing pydantic enums1Larry Martell

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal