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

How to customize Pydantic's from_orm() constructor?

Check out these Pydantic models, AuthorSchema and BookSchema from typing import List, Optional from pydantic import BaseModel class AuthorSchema(BaseModel): id: int name: str blurb: Optional[str] class Config: orm_mode =…
Ben
  • 20,038
  • 30
  • 112
  • 189
4
votes
1 answer

pydantic custom data type for phone number : value_error.missing

mysql schema for user user_id binary(16) first_name varchar(32) last_name varchar(32) email varchar(255) phone varchar(32) role enum('member','admin','operator') created_on datetime updated_on datetime id …
Naveen
  • 91
  • 1
  • 7
4
votes
1 answer

Fastapi Pydantic optional field

Currently, I'm learning Python and Fastapi and I can't figure out what typing.Optional is for. class Post(BaseModel): # default value rating: int = None # typing.Optional rating: Optional[int] = None Both work. I don't understand…
JeanDevFR
  • 61
  • 1
  • 2
  • 3
4
votes
2 answers

Python Pydantic use multiple field alias

Is there any way to use multiple field alias without using root_validator? My current code is this: class GetInfo(BaseModel): name: str = Field(alias='name1') name2: str = Field(alias='name2') @root_validator def…
mark12345
  • 233
  • 1
  • 4
  • 10
4
votes
2 answers

Setting a default value based on another value

I have the following Pydantic model: from pydantic import BaseModel import key class Wallet(BaseModel): private_key: str = Field(default_factory=key.generate_private_key) address: str I want address to have a default_factory as a function…
KOB
  • 4,084
  • 9
  • 44
  • 88
4
votes
1 answer

How can I unpack a Pydantic BaseModel into kwargs?

I am trying to make a function that takes a pydantic BaseModel as an input to run another function. I need to unpack the BaseModel into kwargs. I tried doing this: def run_routing_from_job(job): return run_routing( …
Tom McLean
  • 5,583
  • 1
  • 11
  • 36
4
votes
1 answer

Send pathlib.Path data to FastAPI: PosixPath is not JSON serializable

I have built an API using FastAPI and am trying to send data to it from a client. Both the API and the client use a similar Pydantic model for the data that I want to submit. This includes a field that contains a file path, which I store in a field…
CodingCat
  • 4,999
  • 10
  • 37
  • 59
4
votes
1 answer

How to set type hint on field when dynamically creating a class

For reasons beyond the scope of this question, I'd like to create a dynamic Python-Pydantic class. I'm close, but am not aware of how to add the type hint. Given: class MyClass(BaseModel): class Config: extra = Extra.allow …
SteveJ
  • 3,034
  • 2
  • 27
  • 47
4
votes
1 answer

How to make Pydantic raise an exception right away

I wrote a Pydantic model to validate API payload. The payload has 2 attributes emailId as list and role as str { "emailId": [], "role":"Administrator" } I need to perform two validation on attribute email - emailId must not be…
Jeet Patel
  • 1,140
  • 18
  • 51
4
votes
3 answers

Triggering a function on creation of an pydantic object

is there a clean way of triggering a function call whenever I create/ instantiate a pydantic object? Currently I am "misusing" the root_validator for this: from pydantic import BaseModel class PydanticClass(BaseModel): name: str …
rayon
  • 490
  • 7
  • 16
4
votes
1 answer

How to validate a complex nested data structure with Pydantic?

I had complex and nested data structure like below: { 0: { 0: {'S': 'str1', 'T': 4, 'V': 0x3ff}, 1: {'S': 'str2', 'T': 5, 'V': 0x2ff}}, 1: { 0: {'S': 'str3', 'T': 8, 'V': 0x1ff}, 1: {'S': 'str4', 'T': 7, 'V':…
Dave Hwang
  • 49
  • 1
  • 6
4
votes
1 answer

how can i make a key dynamic in a pydantic model

i have an api entrypoint: @app.get('/dealers_get_users/', response_model = schemas.SellSideUserId, status_code=200) def getdata(db: database.SessionLocal = _Depends(database.get_db)): result = {} i = db.query(models.sellSideUser).all() …
avnav99
  • 532
  • 5
  • 16
4
votes
3 answers

How to dict or data check keys in pydantic

class mail(BaseModel): mailid: int email: str class User(BaseModel): id: int name: str mails: List[mail] data1 = { 'id': 123, 'name': 'Jane Doe', 'mails':[ {'mailid':1,'email':'aeajhs@gmail.com'}, …
Rahul
  • 87
  • 1
  • 1
  • 12
4
votes
1 answer

Pydantic get a fields type hint

I want to store metadata for my ML models in pydantic. Is there a proper way to access a fields type? I know you can do BaseModel.__fields__['my_field'].type_ but I assume there's a better way. I want to make it so that if a BaseModel fails to…
this_josh
  • 333
  • 2
  • 11
4
votes
1 answer

How to convert a Pydantic model in FastAPI to a Pandas DataFrame?

I am trying to convert a Pydantic model to a Pandas DataFrame, but I am getting various errors. Here is the code: from typing import Optional from fastapi import FastAPI from pydantic import BaseModel import pickle import sklearn import pandas as…
Gotey
  • 449
  • 4
  • 15
  • 41