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
23
votes
7 answers

Can I override fields from a Pydantic parent model to make them optional?

I have two pydantic classes like this. class Parent(BaseModel): id: int name: str email: str class ParentUpdate(BaseModel): id: Optional[int] name: Optional[str] email: Optional[str] Both of these are practically the same…
Shiladitya Bose
  • 893
  • 2
  • 13
  • 31
22
votes
6 answers

How to parse ObjectId in a pydantic model?

I am trying to parse MongoDB records to a pydantic model but failing to do so for ObjectId From what I understood, I need to setup validator for ObjectId and did try to both extend ObjectId class and add the validator decorator to my class using…
roshii
  • 507
  • 2
  • 4
  • 10
21
votes
4 answers

How to use values from list as pydantic validator?

I would like to create pydantic model to validate users form. one of my model values should be validated from a list of names. I succeed to create the model using enum as follow: from enum import Enum class Fruit(str, Enum): APPLE = 'apple' …
OrFeldman
  • 283
  • 1
  • 3
  • 6
21
votes
3 answers

How do add an assembled field to a Pydantic model

Say I have model class UserDB(BaseModel): first_name: Optional[str] = None last_name: Optional[str] = None How do I make another model that is constructed from this one and has a field that changes based on the fields in this model? For…
20
votes
3 answers

Pydantic validations for extra fields that not defined in schema

I am using pydantic for schema validations and I would like to throw an error when any extra field is added to a schema that isn't defined. from typing import Literal, Union from pydantic import BaseModel, Field, ValidationError class…
mc-user
  • 1,769
  • 4
  • 14
  • 25
19
votes
2 answers

FastApi returns "field required" and "value_error.missing" for one of my parameters, but the field is there

I do a Post with some parameters, but one of them returns "field required" and "value_error.missing", but the field is there and it has a value. See the output of Postman. In schemas.py the fields are defined as follows: class Message(BaseModel): …
zappfinger
  • 497
  • 2
  • 5
  • 15
18
votes
3 answers

Can I get incoming extra fields from Pydantic?

I have defined a pydantic Schema with extra = Extra.allow in Pydantic Config. Is it possible to get a list or set of extra fields passed to the Schema separately. For ex: from pydantic import BaseModel as pydanticBaseModel class…
Jyotirmay
  • 1,533
  • 3
  • 21
  • 41
18
votes
3 answers

Syntax Error with flake8 and Pydantic Constrained Types: constr(regex=)

I use in Python the package pydantic and the linker Flake8. I want to use constr from pydantic with a regular Experssion. Only certain Characters should be passed. (a-z, A-Z, 0-9 and _) The regular Experssion "^[a-zA-Z0-9_]*$" works, but flake8…
Phil997
  • 575
  • 5
  • 15
18
votes
4 answers

How to use nested pydantic models for sqlalchemy in a flexible way

from fastapi import Depends, FastAPI, HTTPException, Body, Request from sqlalchemy import create_engine, Boolean, Column, ForeignKey, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import Session,…
j-gimbel
  • 181
  • 1
  • 1
  • 4
18
votes
1 answer

FastAPI - ENUM type models not populated

Below is my fastAPI code from typing import Optional, Set from fastapi import FastAPI from pydantic import BaseModel, HttpUrl, Field from enum import Enum app = FastAPI() class Status(Enum): RECEIVED = 'RECEIVED' CREATED = 'CREATED' …
VamsiKrishna
  • 751
  • 6
  • 14
  • 29
18
votes
3 answers

Python Pydantic - how to have an "optional" field but if present required to conform to not None value?

I am trying to validate an object that has "optional" fields in the sense that they may or may not be present. But when they are present, the fields should conform to a specific type definition (not None). In the example below, the "size" field is…
mgcdanny
  • 1,082
  • 1
  • 13
  • 20
17
votes
3 answers

How can mypy accept pydantic's constr() types?

I have this code: from pydantic import BaseModel, constr DeptNumber = constr(min_length=6, max_length=6) class MyStuff(BaseModel): dept: DeptNumber ms = MyStuff(dept = "123456") deptnr.py:6: error: Variable "deptnr.DeptNumber" is not valid…
Robert
  • 7,394
  • 40
  • 45
  • 64
17
votes
3 answers

Alter field after instantiation in Pydantic BaseModel class

With a Pydantic class as follows, I want to transform the foo field by applying a replace operation: from typing import List from pydantic import BaseModel class MyModel(BaseModel): foo: List[str] my_object =…
Jivan
  • 21,522
  • 15
  • 80
  • 131
17
votes
2 answers

Generate dynamic model using pydantic

I am trying to create a dynamic model using Python's pydantic library. My input data is a regular dict. However, the content of the dict (read: its keys) may vary. I am wondering how to dynamically create a pydantic model which is dependent on the…
Andi
  • 3,196
  • 2
  • 24
  • 44
17
votes
4 answers

FastAPI / Pydantic circular references in separate files

I would love to use a schema that looks something like the following in FastAPI: from __future__ import annotations from typing import List from pydantic import BaseModel class Project(BaseModel): members: List[User] class User(BaseModel): …
Nils Ziehn
  • 4,118
  • 6
  • 26
  • 40