Questions tagged [pydantic]

Pydantic is a library for data validation and settings management based on Python type hinting and variable annotations. You can use Pydantic for defining schemas of complex structures in Python.

Pydantic is a library for data validation and settings management based on Python type hinting (PEP484) and variable annotations (PEP526). It allows for defining schemas in Python for complex structures.

1612 questions
35
votes
6 answers

How to add both file and JSON body in a FastAPI POST request?

Specifically, I want the below example to work: from typing import List from pydantic import BaseModel from fastapi import FastAPI, UploadFile, File app = FastAPI() class DataConfiguration(BaseModel): textColumnNames: List[str] idColumn:…
Abdullah
  • 535
  • 1
  • 5
  • 10
31
votes
6 answers

Is it possible to change the output alias in pydantic?

Setup: # Pydantic Models class TMDB_Category(BaseModel): name: str = Field(alias="strCategory") description: str = Field(alias="strCategoryDescription") class TMDB_GetCategoriesResponse(BaseModel): categories:…
Rechu
  • 617
  • 1
  • 4
  • 14
31
votes
2 answers

TypedDict does not allow optional keys?

I wanted to have a schema validation using pydantic, and also TypedDict to define the part of a nested dict schema. However, I realised that the Optional does not work if it is specified within the TypedDict class. I read that this class will render…
Jake
  • 2,482
  • 7
  • 27
  • 51
31
votes
2 answers

Why do I get "AttributeError: __fields_set__" when subclassing a Pydantic BaseModel?

I have this project where my base class and my sub-classes implement pydantic.BaseModel: from pydantic import BaseModel from typing import List from dataclasses import dataclass @dataclass class User(BaseModel): id: int @dataclass class…
Kendrick Lamar
  • 781
  • 2
  • 8
  • 18
29
votes
5 answers

How to validate more than one field of a Pydantic model?

I want to validate three model Fields of a Pydantic model. To do this, I am importing root_validator from pydantic, however I am getting the error below: from pydantic import BaseModel, ValidationError, root_validator Traceback (most recent call…
samba
  • 869
  • 4
  • 12
  • 20
28
votes
1 answer

Assigning Pydantic Fields not by alias

How can I create a pydantic object, without useing alias names? from pydantic import BaseModel, Field class Params(BaseModel): var_name: int = Field(alias='var_alias') Params(var_alias=5) # works Params(var_name=5) # does not work
Erysol
  • 424
  • 1
  • 4
  • 14
27
votes
2 answers

FastAPI - GET request results in typeerror (value is not a valid dict)

this is my database schema. I defined my Schema like this: from pydantic import BaseModel class Userattribute(BaseModel): name: str value: str user_id: str id: str This is my model: class Userattribute(Base): __tablename__ =…
Data Mastery
  • 1,555
  • 4
  • 18
  • 60
27
votes
2 answers

How to convert a list of Pydantic BaseModels to Pandas Dataframe

I can't seem to find any built-in way of simply converting a list of Pydantic BaseModels to a Pandas Dataframe. from pydantic import BaseModel import pandas as pd class SomeModel(BaseModel): col1: str col2: str data = [SomeModel(**{'col1':…
Jerry
  • 563
  • 1
  • 6
  • 22
26
votes
5 answers

Initializing a pydantic dataclass from json

I'm in the process of converting existing dataclasses in my project to pydantic-dataclasses, I'm using these dataclasses to represent models I need to both encode-to and parse-from json. Here's an example of my current approach that is not good…
hagaiw
  • 301
  • 1
  • 3
  • 6
26
votes
5 answers

Query parameters from pydantic model

Is there a way to convert a pydantic model to query parameters in fastapi? Some of my endpoints pass parameters via the body, but some others pass them directly in the query. All this endpoints share the same data model, for example: class…
cglacet
  • 8,873
  • 4
  • 45
  • 60
26
votes
2 answers

How can I set max string field length constraint in Pydantic?

So, I have pydantic model with a string field: class MyPydanticModel(BaseModel): name: Optional[str] And I want to set the max length for this field to 10. How can I do this?
Ezerzez
  • 381
  • 1
  • 3
  • 3
24
votes
1 answer

SQLAlchemy models vs Pydantic models

I'm following this tutorial to adapt it to my needs, in this case, to perform a sql module where I need to record the data collected by a webhook from the gitlab issues. For the database module I'm using SQLAlchemy library and PostgreSQL as…
user3105
  • 359
  • 1
  • 2
  • 7
24
votes
4 answers

Short way to get all field names of a pydantic class

Minimal example of the class: from pydantic import BaseModel class AdaptedModel(BaseModel): def get_all_fields(self, alias=False): return list(self.schema(by_alias=alias).get("properties").keys()) class TestClass(AdaptedModel): …
5th
  • 2,097
  • 3
  • 22
  • 41
24
votes
4 answers

"pydantic\validators.py" : no validator found for

Below DataFrame of pandas is not validated by pydantic. How to handle this? from pydantic.dataclasses import dataclass @dataclass class DataFrames: dataframe1: pd.DataFrame = None dataframe2: pd.DataFrame = None This throws the following…
24
votes
5 answers

How to change date format in pydantic

How to change date format in pydantic for validation and serialization? For validation I am using @validator. Is there an solution for both cases?
jonsbox
  • 472
  • 1
  • 6
  • 13