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

Why I cannot create standalone object of HttpURL in pydantic?

from pydantic import BaseModel, Field, HttpUrl from typing import Optional class TestClass(BaseModel): url:Optional[HttpUrl] = None Creating object TestClass with url="https://www.test.com" works. Here the imported HttpUrl or BaseModel are…
avs_2823
  • 75
  • 1
  • 5
4
votes
2 answers

How to automatically create an html form from an pydantic model?

Is there a way to create an Html form from a pydantic model? Let's start with an easy example. I have a model User: from pydantic import BaseModel class User(BaseModel): id: int name: str = 'Jane Doe' and some magic function that…
user7638008
  • 137
  • 9
4
votes
0 answers

Sqlalchemy hybrid_property how to pass parameters during query?

I have sqlalchemy model which represents a 'post' a user has made class Post(Base): id = Column(Integer, primary_key=True) caption = Column(String, nullable=False) user_id = Column(Integer, ForeignKey("instagram_user.id")) user =…
shadowByte
  • 93
  • 1
  • 1
  • 6
4
votes
2 answers

Pydantic validation error for BaseSettings model with local ENV file

I'm developing a simple FastAPI app and I'm using Pydantic for storing app settings. Some settings are populated from the environment variables set by Ansible deployment tools but some other settings are needed to be set explicitly from a separate…
ruslaniv
  • 458
  • 1
  • 6
  • 14
4
votes
4 answers

How to change the default Pydantic error message using FastAPI?

Is there any way to change the default response from Pydantic so that "msg" is "message"? { "detail": [ { "loc": [ "body", "password" ], "msg": "Password should at least…
dataviews
  • 2,466
  • 7
  • 31
  • 64
4
votes
2 answers

Getting error "value is not a valid dict" when using Pydantic models in FastAPI for model-based predictions

I'm trying to use Pydantic models with FastAPI to make multiple predictions (for a list of inputs). The problem is that one can't pass Pydantic models directly to model.predict() function, so I converted it to a dictionary, however, I'm getting the…
Legna
  • 125
  • 1
  • 1
  • 10
4
votes
1 answer

parse json file to Pydantic model

I created a model of Pydantic. But it does not convert and outputs an error Please tell me,what is wrong. classDTO: from pydantic import BaseModel,Field from typing import List,Dict from datetime import date class OurBaseModel(BaseModel): pass …
Никита
  • 41
  • 1
  • 1
  • 3
4
votes
1 answer

Python Pydantic Error: TypeError: __init__() takes exactly 1 positional argument (2 given)

i am currenty working on a python fastapi project for university. Every time i run my authorization dependencies i get the following error: ERROR: Exception in ASGI application Traceback (most recent call last): File…
Zito
  • 78
  • 1
  • 1
  • 6
4
votes
0 answers

Use custom error classes with constrained types in Pydantic

The documentation specifies that: you can also define your own error classes, which can specify a custom error code, message template, and context. In the example from the link above, a validator is defined to validate an attribute and the custom…
joel314
  • 1,060
  • 1
  • 8
  • 22
4
votes
0 answers

Pydantic preprocessing field value

I have data in which dates are represented as ISO strings. Example: {"name": "John", "last_active": "2021-10-13T17:16:49-04:00"} My Pydantic model: from datetime import datetime from pydantic import BaseModel class User(BaseModel): name: str …
Elgin Cahangirov
  • 1,932
  • 4
  • 24
  • 45
4
votes
1 answer

Pydantic inconsistent and automatic conversion between float and int

I am using pydantic python package in FastAPI for a web app, and I noticed there is some inconsistent float-int conversions with different typing checks. For example: class model(BaseModel): data: Optional[Union[int, float]] = None m =…
Sammy Cui
  • 108
  • 1
  • 6
4
votes
2 answers

Pydantic set variables from a list

Is there a way to set a pydantic model from a list? I tried this and it didn't work for me. If it's not possible with pydantic, what is the best way to do this if I still need type validation and conversion, constraints, etc.? Order is important…
Superbman
  • 787
  • 1
  • 8
  • 24
4
votes
4 answers

Create a field which is immutable after being set

Is it possible to create a Pydantic field that does not have a default value and this value must be set on object instance creation and is immutable from then on? e.g. from pydantic import BaseModel class User(BaseModel): user_id: int name:…
KOB
  • 4,084
  • 9
  • 44
  • 88
4
votes
1 answer

Pydantic: Save nested settings

I'm currently trying to automatically save a pydantic.BaseSettings-object to a json-file on change. The model is loaded out of the json-File beforehand. A minimal working example of the saving procedure is as follows: import json from pydantic…
H. Müller
  • 109
  • 1
  • 9
4
votes
1 answer

How to upload both file and JSON data using FastAPI?

This is my Pydantic model: class Base(BaseModel): name: str point: Optional[float] = None is_accepted: Optional[bool] = False This is the endpoint: def create_base( base: Base = Form(...), file: List[UploadFile] =…
gr0gu3
  • 555
  • 1
  • 6
  • 11