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
4
votes
2 answers

FastAPI: How to customise 422 exception for specific route?

How to replace 422 standard exception with custom exception only for one route in FastAPI? I don't want to replace for the application project, just for one route. I read many docs, and I don't understand how to do this. Example of route that I need…
Octoloper
  • 205
  • 2
  • 9
4
votes
1 answer

Make Pydantic BaseModel fields optional including sub-models for PATCH

As already asked in similar questions, I want to support PATCH operations for a FastApi application where the caller can specify as many or as few fields as they like, of a Pydantic BaseModel with sub-models, so that efficient PATCH operations can…
NeilG
  • 3,886
  • 2
  • 22
  • 30
4
votes
2 answers

Cannot determine if type of field in a Pydantic model is of type List

I am trying to automatically convert a Pydantic model to a DB schema. To do that, I am recursively looping through a Pydantic model's fields to determine the type of field. As an example, I have this simple model: from typing import List from…
rbhalla
  • 869
  • 8
  • 32
4
votes
1 answer

zsh: no matches found - trying to install pydantic[email]

After activating my venv and installing Pydantic through pip install pydantic I tried creating a new file as follows: from pydantic import Basemodel, EmailStr class Person(BaseModel): id: int name: str email: EmailStr and was…
jonsdope
  • 51
  • 1
  • 3
4
votes
1 answer

What is the difference between pydantic Field and Annotated?

Those two concepts Field and Annotated seem very similar in functionality. For example, I can define the same variable in any way as: temperature: float = Field(0.0, ge=0, le=1) temperature: Annotated[confloat(ge=0, le=1),...] = 0.0 Is there any…
gench
  • 1,063
  • 1
  • 11
  • 17
4
votes
1 answer

Pydantic from_orm to load Django model with related list field

I have the following Django models: from django.db import models class Foo(models.Model): id: int name = models.TextField(null=False) class Bar(models.Model): id: int foo = models.ForeignKey( Foo, …
Abhishek Kumar
  • 165
  • 1
  • 17
4
votes
1 answer

How to use two variable types in a pydantic.BaseModel with typing.Union?

I need my model to accept either a bytes type variable or a string type variable and to raise an exception if any other type was passed. from typing import Union from pydantic import BaseModel class MyModel(BaseModel): a: Union[bytes,…
Mirrah
  • 125
  • 2
  • 9
4
votes
1 answer

Dynamically Generating Pydantic Model from a Schema JSON File

I want to dynamically generate a Pydantic model at runtime. I can do this by calling create_model. For example, from pydantic import create_model create_model("MyModel", i=(int,...), s=(str...)) does this same thing as from pydantic import…
W.P. McNeill
  • 16,336
  • 12
  • 75
  • 111
4
votes
2 answers

Updating multiple Pydantic fields that are validated together

How do you update multiple properties on a pydantic model that are validated together and dependent upon each other? Here is a contrived but simple example: from pydantic import BaseModel, root_validator class Example(BaseModel): a: int b:…
4
votes
1 answer

Interaction between Pydantic models/schemas in the FastAPI Tutorial

I follow the FastAPI Tutorial and am not quite sure what the exact relationship between the proposed data objects is. We have the models.py file: from sqlalchemy import Boolean, Column, ForeignKey, Integer, String from sqlalchemy.orm import…
patrick
  • 45
  • 1
  • 6
4
votes
1 answer

return deeply nested json objects with response_model fastAPI and Pydantic

This is my schema file from pydantic import BaseModel from typing import Optional class HolidaySchema(BaseModel): year: int month: int country: str language: str class HolidayDateSchema(BaseModel): name: str date: str …
4
votes
2 answers

How to download a large file using FastAPI?

I am trying to download a large file (.tar.gz) from FastAPI backend. On server side, I simply validate the filepath, and I then use Starlette.FileResponse to return the whole file—just like what I've seen in many related questions on…
Yuqi Wang
  • 135
  • 1
  • 2
  • 8
4
votes
1 answer

How to update pydantic model from dictionary?

I have a pydantic model: from pydantic import BaseModel class MyModel(BaseModel): value : str = 'Some value' And I need to update this model using a dictionary (not create). I tried updating the model using class.__dict__, but after updating…
4
votes
1 answer

Pydantic - Allow missing field

I have a pydantic model. One of its fields must be supplied by user, however, the second one can be present but it is totally okay if it is missing. In case of missing age, I don't want it to be present on pydantic model instance at all. My…
Fusion
  • 5,046
  • 5
  • 42
  • 51
4
votes
1 answer

Pydantic model copied when passing it to another model

Pydantic copies a model when passing it to the constructor of another model. This fails: from pydantic import BaseModel class Child(BaseModel): pass class Parent(BaseModel): child: Child child = Child() parent =…
miksus
  • 2,426
  • 1
  • 18
  • 34