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
87
votes
8 answers

How to parse list of models with Pydantic

I use Pydantic to model the requests and responses to an API. I defined a User class: from pydantic import BaseModel class User(BaseModel): name: str age: int My API returns a list of users which I retrieve with requests and convert into a…
Nymous
  • 971
  • 1
  • 7
  • 8
82
votes
8 answers

How to read body as any valid json?

I haven't found the docs for that use case. How can I get the request body, ensure it's a valid JSON (any valid JSON, including numbers, string, booleans, and nulls, not only objects and arrays) and get the actual JSON. Using Pydantic forces the…
caeus
  • 3,084
  • 1
  • 22
  • 36
73
votes
4 answers

Pydantic enum field does not get converted to string

I am trying to restrict one field in a class to an enum. However, when I try to get a dictionary out of class, it doesn't get converted to string. Instead it retains the enum. I checked pydantic documentation, but couldn't find anything relevant to…
ierdna
  • 5,753
  • 7
  • 50
  • 84
54
votes
1 answer

Is it bad practice to include non-validating methods in a pydantic model?

I'm using pydantic 1.3 to validate models for an API I am writing. Is it common/good practice to include arbitrary methods in a class that inherits from pydantic.BaseModel? I need some helper methods associated with the objects and I am trying to…
ccred
  • 543
  • 4
  • 5
52
votes
2 answers

How to give a Pydantic list field a default value?

I want to create a Pydantic model in which there is a list field, which left uninitialized has a default value of an empty list. Is there an idiomatic way to do this? For Python's built-in dataclass objects you can use field(default_factory=list),…
Salim Fadhley
  • 6,975
  • 14
  • 46
  • 83
52
votes
2 answers

Pydantic: dataclass vs BaseModel

What are the advantages and disadvantages of using Pydantic's dataclass vs BaseModel? Are there any performance issues or is it easier to Pydantic's dataclass in the other python module?
sacherus
  • 1,614
  • 2
  • 20
  • 27
49
votes
11 answers

Make every field as optional with Pydantic

I'm making an API with FastAPI and Pydantic. I would like to have some PATCH endpoints, where 1 or N fields of a record could be edited at once. Moreover, I would like the client to only pass the necessary fields in the payload. Example: class…
nolwww
  • 1,355
  • 1
  • 15
  • 33
48
votes
3 answers

How to pass the default value to a variable if None was passed?

Can I make a default value in Pydantic if None is passed in the field? I have the following code, but it seems to me that the validator here only works on initialization of the model and not otherwise. My Code: class User(BaseModel): name:…
Manas Sambare
  • 1,158
  • 2
  • 11
  • 22
46
votes
7 answers

Generate pydantic model from a dict

Is there a straight-forward approach to generate a Pydantic model from a dictionary? Here is a sample of the data I have. { 'id': '424c015f-7170-4ac5-8f59-096b83fe5f5806082020', 'contacts': [{ 'displayName': 'Norma Fisher', …
Dillon Miller
  • 763
  • 1
  • 6
  • 18
44
votes
4 answers

How to add a custom decorator to a FastAPI route?

I want to add an auth_required decorator to my endpoints. (Please consider that this question is about decorators, not middleware) So a simple decorator looks like this: def auth_required(func): def wrapper(*args, **kwargs): if…
sashaaero
  • 2,618
  • 5
  • 23
  • 41
44
votes
3 answers

How to require predefined string values in python pydantic basemodels?

Is there any in-built way in pydantic to specify options? For example, let's say I want a string value that must either have the value "foo" or "bar". I know I can use regex validation to do this, but since I use pydantic with FastAPI, the users…
Gabriel Petersson
  • 8,434
  • 4
  • 32
  • 41
42
votes
7 answers

How to use a Pydantic model with Form data in FastAPI?

I am trying to submit data from HTML forms and validate it with a Pydantic model. Using this code from fastapi import FastAPI, Form from pydantic import BaseModel from starlette.responses import HTMLResponse app = FastAPI() @app.get("/form",…
shanmuga
  • 4,329
  • 2
  • 21
  • 35
41
votes
4 answers

pydantic convert to jsonable dict (not full json string)

I'd like to use pydantic for handling data (bidirectionally) between an api and datastore due to it's nice support for several types I care about that are not natively json-serializable. It has better read/validation support than the current…
some bits flipped
  • 2,592
  • 4
  • 27
  • 42
37
votes
4 answers

Pydantic: Make field None in validator based on other field's value

I'm using the pydantic BaseModel with a validator like this: from datetime import date from typing import List, Optional from pydantic import BaseModel, BaseConfig, validator class Model(BaseModel): class Config(BaseConfig): …
Dániel Nagy
  • 11,815
  • 9
  • 50
  • 58
36
votes
3 answers

Pydantic: Detect if a field value is missing or given as null

I want to allow users to selectively update fields using PUT calls. On the pydantic model, I have made the fields Optional. In the FastAPI handler if the model attribute is None, then the field was not given and I do not update it. The problem with…
Jeremy
  • 1,397
  • 2
  • 13
  • 20
1
2 3
99 100