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

Parsing different time formats with pydantic json_decoders not working as expected

Can somebody please explain me the behaviour of the following pydantic model. from datetime import datetime from pydantic import BaseModel first_format = {'time': '2018-01-05T16:59:33+00:00',} second_format = {'time':…
Florian D.
  • 170
  • 1
  • 10
7
votes
1 answer

Pydantic model with field names that have non-alphanumeric characters

I'm using FastAPI and want to build a pydantic model for the following request data json: { "gas(euro/MWh)": 13.4, "kerosine(euro/MWh)": 50.8, "co2(euro/ton)": 20, "wind(%)": 60 } I defined the model like this: class…
user9195416
7
votes
1 answer

Pydantic prevent conversion of incorrect type

Pydantic appears to perform automatic type conversion when the type of a property is not what is expected. I believe this is why (conveniently) it is possible to assign the value of an int enum property of a class via its raw int value. However I…
tomfumb
  • 3,669
  • 3
  • 34
  • 50
7
votes
1 answer

Initialize pydantic Literal from array of strings

I would like to inizialize a pydantic Literal from an array of strings from typing import Literal from pydantic import BaseModel CLASS_NAME_VALUES = ["primary", "secondary", "success", "danger", "warning", "info", "dark"] ICON_NAME_VALUES =…
Silvio Messi
  • 123
  • 1
  • 2
  • 9
7
votes
1 answer

How to modify pydantic field when another one is changed?

I have a pydantic class such as: from pydantic import BaseModel class Programmer(BaseModel): python_skill: float stackoverflow_skill: float total_score: float = None Now I am calculating the total_score according to the other…
OrFeldman
  • 283
  • 1
  • 3
  • 6
7
votes
1 answer

Pydantic does not validate when assigning a number to a string

When assigning an incorrect attribute to a Pydantic model field, no validation error occurs. from pydantic import BaseModel class pyUser(BaseModel): username: str class Config: validate_all = True validate_assignment =…
phil0s0pher
  • 525
  • 10
  • 21
7
votes
5 answers

convert to pydantic model from tuple

I want to map a tuple(list) to a pydantic model. Is there a best practice to map tuple indexes to attributes in the following cases? cryptwatch from pydantic import BaseModel class Ohlc(BaseModel): close_time: float open_time: float …
sasano8
  • 73
  • 1
  • 4
7
votes
3 answers

How to parse unix timestamp into datetime without timezone in Fast API

Assume I have a pydantic model class EventEditRequest(BaseModel): uid: UUID name: str start_dt: datetime end_dt: datetime I send request with body b'{"uid":"a38a7543-20ca-4a50-ab4e-e6a3ae379d3c","name":"test…
sashaaero
  • 2,618
  • 5
  • 23
  • 41
7
votes
1 answer

How to create children with uuid with pydantic

I try to create children of Foo, each should have its own uuid. In the real code no Instance of Foo will be created only it's children. The children will be saved in a database later, the uuid is to retrieve right objects from the database. In the…
J B
  • 95
  • 1
  • 1
  • 8
7
votes
2 answers

FastAPI Single Parameter Body cause Pydantic Validation Error

I have a POST FastAPI method. I do not want to construct a class nor query string. So, I decide to apply Body() method. @app.post("/test-single-int") async def test_single_int( t: int = Body(...) ): pass This is the request POST…
Pranithan T.
  • 323
  • 1
  • 6
  • 14
6
votes
2 answers

What is the new way to declare Mongo ObjectId with PyDantic v2.0^?

This week, I started working with MongoDB and Flask, so I found a helpful article on how to use them together by using PyDantic library to define MongoDB's models. However, the article is somewhat outdated, mostly could be updated to new PyDantic's…
6
votes
5 answers

FastAPI - "TypeError: issubclass() arg 1 must be a class" with modular imports

When working with modular imports with FastAPI and SQLModel, I am getting the following error if I open /docs: TypeError: issubclass() arg 1 must be a class Python 3.10.6 pydantic 1.10.2 fastapi 0.85.2 sqlmodel 0.0.8 macOS 12.6 Here is a…
Felix
  • 167
  • 1
  • 8
6
votes
1 answer

pydantic.error_wrappers.ValidationError : value is not a valid list (type=type_error.list)

New to FastAPI Getting a "value is not a valid list (type=type_error.list)" error Whenever I try to return {"posts": post} @router.get('', response_model = List[schemas.PostResponseSchema]) def get_posts(db : Session = Depends(get_db)): …
schneider99
  • 65
  • 1
  • 5
6
votes
2 answers

OpenAPI is missing schemas for some of the Pydantic models in FastAPI app

I am building a FastAPI application, which has a lot of Pydantic models. Even though the application is working just fine, as expected the OpenAPI (Swagger UI) docs do not show the schema for all of these models under the Schemas section. Here are…
Rohit
  • 3,659
  • 3
  • 35
  • 57
6
votes
2 answers

Pydantic inherit generic class

New to python and pydantic, I come from a typescript background. I was wondering if you can inherit a generic class? In typescript the code would be as follows interface GenericInterface { value: T } interface ExtendsGeneric extends…
Syed Jafri
  • 416
  • 8
  • 17