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

pydantic Settings: TypeError: cannot pickle '_thread.lock' object

I am facing an issue with pydantic.BaseSettings and prometheus_client.Summary. The snippet below throws an Exception when trying to be executed: from prometheus_client import Summary from pydantic import BaseSettings class Settings(BaseSettings): …
Gunar Maiwald
  • 141
  • 1
  • 4
6
votes
2 answers

Python pydantic validate dates

My question is pretty simple and I'm surprised no one has asked it so far: How can I validate dates in pydantic? For example, I only want to accept dates in the range 1980.1.1-2000.1.1.
salius
  • 918
  • 1
  • 14
  • 30
6
votes
3 answers

Using Pandas Data Frame as a Type in Pydantic

I'm using pydantic and want to create classes which contain pandas dataframes. I was looking for this online for quite a time and did not find anything. My code for the custom types looks as following. I named the type for dataframes pd.DataFrame…
mafehx
  • 363
  • 2
  • 6
  • 14
6
votes
3 answers

Pydantic - Dynamically create a model with multiple base classes?

From the pydantic docs I understand this: import pydantic class User(pydantic.BaseModel): id: int name: str class Student(pydantic.BaseModel): semester: int # this works as expected class Student_User(User, Student): building:…
cknoll
  • 2,130
  • 4
  • 18
  • 34
6
votes
3 answers

Can I make a default value in pydantic if None is passed in the field without using validators?

Can I make a default value in pydantic if None is passed in the field without using validators? I have the following code, but it seems to me that the validator here is superfluous for contract_ndfl. Is there any way to do without a validator? My…
Fyzzys
  • 756
  • 1
  • 7
  • 13
6
votes
1 answer

Is it possible to have arbitrary key names in pydantic?

I am trying to emulate a similar behavior to typescripts interface with arbitrary key names for a pydantic model, but am running in to some issues. Consider the following in TS: export interface SNSMessageAttributes { [name: string]:…
securisec
  • 3,435
  • 6
  • 36
  • 63
6
votes
1 answer

REST API in Python with FastAPI and pydantic: read-only property in model

Assume a REST API which defines a POST method on a resource /foos to create a new Foo. When creating a Foo the name of the Foo is an input parameter (present in the request body). When the server creates a Foo it assigns it an ID. This ID is…
DrP3pp3r
  • 803
  • 9
  • 33
6
votes
2 answers

Dataclasses: Require at least one value to be set in grouping of model fields

How can one require at least one field in a group of fields on a dataclass to be set to a truthy value? Does this require a custom root validator method as it requires looking at many fields at once? For example, consider the following…
Joe Bane
  • 1,556
  • 1
  • 15
  • 30
6
votes
1 answer

Use regex in pydantic model that works with set of strings

I'm using a pydantic model to represent a request body in fastapi. from pydantic import BaseModel, Schema class Files(BaseModel): '''Class for file URLs''' urls: Set[str] = Schema(..., title='Urls to files') This will restrict the request…
Val
  • 6,585
  • 5
  • 22
  • 52
6
votes
2 answers

How to validate JSON field with name "from"

I want to validate JSON object (it is in Telegram Bot API) which contains from field (which is reserved word in Python) by using pydantic validator. So my model should look like the following: class Message(BaseModel): message_id: int from:…
likern
  • 3,744
  • 5
  • 36
  • 47
5
votes
1 answer

Excluding fields on a pydantic model when it is the nested child of another model

I have a pydantic model that I want to dynamically exclude fields on. I can do this by overriding the dict function on the model so it can take my custom flag, e.g.: class MyModel(BaseModel): field: str def dict(self, **kwargs): if…
rbhalla
  • 869
  • 8
  • 32
5
votes
1 answer

What does Pydantic ORM mode exactly do?

According to the docs, Pydantic "ORM mode" (enabled with orm_mode = True in Config) is needed to enable the from_orm method in order to create a model instance by reading attributes from another class instance. If ORM mode is not enabled, the…
rrobby86
  • 1,356
  • 9
  • 14
5
votes
1 answer

Pydantic returns 'field required (type=value_error.missing)' on an Optional field with a custom model

I'm trying to build a custom field in Fastapi-users pydantic schema as follows: class UserRead(schemas.BaseUser[uuid.UUID]): twitter_account: Optional['TwitterAccount'] On UserRead validation Pydantic returns field required…
Jakub Królikowski
  • 403
  • 1
  • 7
  • 16
5
votes
3 answers

How to override "env_file" during tests?

I'm reading env variables from .prod.env file in my config.py: from pydantic import BaseSettings class Settings(BaseSettings): A: int class Config: env_file = ".prod.env" env_file_encoding = "utf-8" settings =…
Ruuza
  • 185
  • 7
5
votes
1 answer

Why doesn't FastAPI handle types derived from int and Enum correctly?

With the following FastAPI backend: from enum import Enum from fastapi import FastAPI class MyNumber(int, Enum): ONE = 1 TWO = 2 THREE = 3 app = FastAPI() @app.get("/add/{a}/{b}") async def get_model(a: MyNumber, b: MyNumber): …
JHK
  • 639
  • 1
  • 7
  • 14