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

Tortoise ORM for Python no returns relations of entities (Pyndantic, FastAPI)

I was making a sample Fast Api server with Tortoise ORM as an asynchronous orm library, but I just cannot seem to return the relations I have defined. These are my relations: # Category from tortoise.fields.data import DatetimeField from…
8
votes
2 answers

When I use fastapi and pydantic to build POST API, appear a TypeError: Object of type is not JSON serializable

I use FastAPi and Pydantic to model the requests and responses to an POST API. I defined three class: from pydantic import BaseModel, Field from typing import List, Optional, Dict class RolesSchema(BaseModel): roles_id: List[str] class…
ppoozine
  • 83
  • 2
  • 7
8
votes
3 answers

FastAPI - GET Request with Pydantic List field

I'm new to FastAPI (migrating from Flask) and I'm trying to create a Pydantic model for my GET route: from fastapi import APIRouter,Depends from pydantic import BaseModel from typing import Optional,List router = APIRouter() class…
Avish021
  • 81
  • 1
  • 3
8
votes
5 answers

Pydantic and "constructors"

I'm new to Pydantic and trying to understand how/if I can create a new class instance. I've read through the Pydantic documentation and can't find an example doing anything similar. My python code (prior to Pydantic) looks like: class Person: …
user2558918
  • 103
  • 1
  • 2
  • 4
8
votes
3 answers

Is it possible to change the pydantic error messages in fastAPI?

In the FastAPI framework, the pydantic error messages are showing like below. {"detail": [ { "loc": [ "body", "location", "name" ], "msg": "field required", "type": "value_error.missing" }, { "loc": [ …
user12747053
  • 333
  • 2
  • 4
  • 9
7
votes
3 answers

Allow positional arguments for BaseModel pydantic

I have a class with all the necessary parameters. But, for init function, it asks for keyword arguments, and does not accept positional arguments. So, my question is: is there something I can change in config of pydantic.BaseModel to allow…
user13699857
7
votes
3 answers

pydantic.error_wrappers.ValidationError: 11 validation errors for For Trip type=value_error.missing

Im getting this error with my pydantic schema, but oddly it is generating the object correctly, and sending it to the SQLAlchemy models, then it suddenly throws error for all elements in the model. response -> id field required…
7
votes
1 answer

Pydantic: Transform a value before it is assigned to a field?

I have the following model class Window(BaseModel): size: tuple[int, int] and I would like to instantiate it like this: fields = {'size': '1920x1080'} window = Window(**fields) Of course this fails since the value of 'size' is not of the…
trivicious
  • 186
  • 1
  • 8
7
votes
2 answers

Pydantic set attribute/field to model dynamically

According to the docs: allow_mutation whether or not models are faux-immutable, i.e. whether setattr is allowed (default: True) Well I have a class : class MyModel(BaseModel): field1:int class Config: allow_mutation = True If I…
jossefaz
  • 3,312
  • 4
  • 17
  • 40
7
votes
2 answers

Pydantic settings management + FastAPI: how to ignore a .env file during tests with pytest?

I'm using Pydantic settings management in a FastAPI-based project. I have a Settings class like this one: class Settings(BaseSettings): FOO: str = '' BAR: int = 0 class Config: env_file = "path/to/.my_env_file") …
floatingpurr
  • 7,749
  • 9
  • 46
  • 106
7
votes
2 answers

Pydantic Model Field Of Type Union[int, float] : How To Prevent Field From Rounding To int When Initialised With float Value?

I have the following pydantic model which contains a result typed as Union[int,float] as listed below. from typing import Union from pydantic import BaseModel class Calculation(BaseModel): arg1: int arg2: int class…
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62
7
votes
1 answer

Pydantic is confused with my types and their union

I have following Pydantic model type scheme specification: class RequestPayloadPositionsParams(BaseModel): """ Request payload positions parameters """ account: str = Field(default="COMBINED ACCOUNT") fields: List[str] =…
Martin Fischer
  • 469
  • 1
  • 7
  • 21
7
votes
1 answer

how to return only one column from database in pydantic model as a list?

I have a simple database relationship that stores which user liked which post. But in liked attribute, it's returning a list of dictionaries that contains post_id but I want to return the list of app_ids. Here is my pydantic model: class…
7
votes
1 answer

How to pass pydantic create_model config and base arguments

It seems that pydantic does not allow passing both base and config arguments to create_model function, to avoid confusion. What I tried to do is: from pydantic import BaseModel, create_model class Config: orm_mode = True E = create_model('E',…
eran helzer
  • 145
  • 1
  • 5
7
votes
2 answers

How to write a Pydantic model to accept a Dictionary of Dictionaries

The code below is modified from the Pydantic documentation I would like to know how to change BarModel and FooBarModel so they accept the input assigned to m1. I have tried using __root__ and syntax such as Dict[str, BarModel] but have been unable…
snow6oy
  • 678
  • 1
  • 7
  • 16