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
5
votes
1 answer

How to set range and value out of range in Pydantic field using FastAPI?

So I have the following line of code: item: Optional[int] = Field(None, ge=1, le=168) and I would like to have possibility to set -1 value as well. So, I need to exclude zero values, but I would like to allow a -1 value and values from 1 to 168. Is…
SimonZen
  • 131
  • 1
  • 11
5
votes
1 answer

Pydantic created at and updated at fields

I'm new to using Pydantic and I'm using it to set up the models for FastAPI to integrate with my postgres database. I want to make a model that has an updated_at and created_at field which store the last datetime the model was updated and the…
bballboy8
  • 400
  • 6
  • 25
5
votes
1 answer

Using pydantic with xml

I am working on a project that uses a lot of xml, and would like to use pydantic to model the objects. In this case I simplified the xml but included an example object.
Tippecanoe
  • 188
  • 2
  • 10
5
votes
4 answers

How to filter out NaN by pydantic

How to filter out NaN in pytdantic float validation? from pydantic import BaseModel class MySchema(BaseModel): float_value: float
Ryabchenko Alexander
  • 10,057
  • 7
  • 56
  • 88
5
votes
1 answer

How to serialize custom type that extends builtin type in pydantic?

currently I'm working with FastAPI and pydantic as serializer. Problem is, we're using snowflake id on the server side, which means we need to convert those ids to string before sending to client (javascript) because the id is larger than JS's MAX…
Vu Viet Dung
  • 343
  • 1
  • 3
  • 6
5
votes
2 answers

How to go through all Pydantic validators even if one fails, and then raise multiple ValueErrors in a FastAPI response?

Is it possible to call all validators to get back a full list of errors? @validator('password', always=True) def validate_password1(cls, value): password = value.get_secret_value() min_length = 8 if len(password) < min_length: …
dataviews
  • 2,466
  • 7
  • 31
  • 64
5
votes
1 answer

pydantic's update_forward_refs raises typing NameError

Python 3.9 - I have the following module: from __future__ import annotations from typing import TYPE_CHECKING from pydantic import BaseModel if TYPE_CHECKING: from typing import Optional class A(BaseModel): id: int class Config: …
ishefi
  • 480
  • 4
  • 12
5
votes
2 answers

How to generate Pydantic model for multiple different objects

I need to have a variable covars that contains an unknown number of entries, where each entry is one of three different custom Pydantic models. In this case, each entry describes a variable for my application. Specifically, I want covars to have the…
svedel
  • 79
  • 1
  • 1
  • 4
5
votes
0 answers

Why does Pydantic hide the exception-raising line?

I'm using Pydantic and there's something that makes me curious, exceptions raised from Pydantic hides the line raising the exception. For example with this code: from typing import Any from pydantic import BaseModel from pydantic.error_wrappers…
what the
  • 398
  • 3
  • 10
5
votes
2 answers

Pydantic params validation with file upload

I'm trying upload user data with file. I wanna do something like this, validate user data and attach file class User(BaseModel): user: str name: str @router.post("/upload") async def create_upload_file(data: User, file: UploadFile =…
5
votes
1 answer

how to type a variable in FastApi-SwaggerUI with hyphen in its name?

If I send a request to this API: from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Response(BaseModel): var_name: str @app.put("/", response_model=Response) def simple_server(a: str): response =…
MKMS
  • 117
  • 7
5
votes
1 answer

How Pydantic handles unknown dictionary keys

I have a json object that I want to validate using Pydantic. The problem I am facing is that: 1 - I don't know how many fields I will have in the JSON. The example below has 2 keys\fields: "225_5_99_0" and "225_5_99_1" 2 - I don't know the names of…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
5
votes
3 answers

pydantic : duplicate validator function

I used the code below: It shows duplicated validator. Why cannot use both? How do I create an alias in the @validator if I cannot use Field? from pydantic import BaseModel, validator, Field import datetime class MultiSourceInput(BaseModel): …
slashie
  • 127
  • 1
  • 10
5
votes
2 answers

Fastapi custom response model

I have a router that fetches all data from the database. Here is my code: @router.get('/articles/', response_model=List[articles_schema.Articles]) async def main_endpoint(): query =…
5
votes
2 answers

pydantic with removal of white space

I am trying to remove white space on the first name and last name field, as well as the email field. I am unable to get it to work. from pydantic import BaseModel, UUID4, SecretStr, EmailStr, constr class UserCreate(BaseModel): email:…
dataviews
  • 2,466
  • 7
  • 31
  • 64