Skip to content

Tag: pydantic

Pydantic Checking if list field is unique

Currently, I am trying to create a pydantic model for a pandas dataframe. I would like to check if a column is unique by the following I would now like to run a validation here and have it fail since address is not unique. The following returns what I need but i want to be able to reuse this validator

Pydantic validation errors with None values

I have a returned result from a webservice, whose values come as they are (see example). The keys are not Optional and must be included. 2 validation errors for Result: cause – str type expected (type=type.error.str) urls – str type expected (type=type.error.str) First questions is, what is the co…

Short way to get all field names of a pydantic class

Minimal example of the class: The way it works: Is there a way to make it work without giving the class again? Desired way to get all field names: It would also work if the field names are assigned to an attribute. Just any way to make it make it more readable Answer What about just using __fields__: Output: …

Conditional call of a FastAPI Model

I have a multilang FastAPI connected to MongoDB. My document in MongoDB is duplicated in the two languages available and structured this way (simplified example): I therefore implemented two models DatasetFR and DatasetEN, each one makeS references with specific external Models (Enum) for category and tags in…

Pydantic/SQLAlchemy: How to work with enums?

What is the best way to convert a sqlalchemy model to a pydantic schema (model) if it includes an enum field? Sqlalchemy Pydantic Conversion This leads to the error value is not a valid enumeration member; permitted: ‘CREATED’, ‘UPDATED’ (type=type_error.enum; enum_values=[<StateEnu…

Distinguishing between Pydantic Models with same fields

I’m using Pydantic to define hierarchical data in which there are models with identical attributes. However, when I save and load these models, Pydantic can no longer distinguish which model was used and picks the first one in the field type annotation. I understand that this is expected behavior based …