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

Pydantic - Validation Does not Happen

I am quite new to using Pydantic. The Issue I am facing right now is that the Model Below is not raising the Expected Exception when the value is out of range. For example, if you pass -1 into this model it should ideally raise an HTTPException. but…
5
votes
1 answer

Pydantic: How to init a model by a dict with alias field names?

I have a model: class Cars(BaseModel): numberOfCars: int = Field(0,alias='Number of cars') I have a dict with: { "Number of cars":3 } How can I create an instance of Cars by using this model?` Is there something like 'by_alias' when using this?
FishingIsLife
  • 1,972
  • 3
  • 28
  • 51
5
votes
3 answers

Value Error Missing when using POST with FastAPI

I'm building a simple API with FastAPI using the official documentation, but when I try to test it with postman I get the same error: pydantic.error_wrappers.ValidationError Here is my model: class Owner(BaseModel): name: str address: str …
Erika
  • 151
  • 3
  • 12
5
votes
1 answer

use Pydantic to create CSV line

We use Pydantic to set the "Domain Models" and use them across the application layer. import datetime from decimal import Decimal from pydantic import BaseModel class Person(BaseModel): name: str birth_date: datetime.date height:…
greenkey
  • 383
  • 3
  • 11
5
votes
4 answers

Retrieve JSON string from pydantic Json type

I have a pydantic model as follows. from pydantic import Json, BaseModel class Foo(BaseModel): id: int bar: Json Foo.bar can parse JSON string as input and store it as a dict which is nice. foo = Foo(id=1, bar='{"foo": 2, "bar":…
Jey
  • 179
  • 1
  • 1
  • 6
5
votes
1 answer

Why does pydantic not encode my bool in json?

I want to have Pydantic write booleans as either "y" or "n". The Pydantic documentation on encoders suggests that I can configure a way to encode values based on their data type, in my case bool. from pydantic import BaseModel class…
Robert
  • 7,394
  • 40
  • 45
  • 64
5
votes
1 answer

Conflict between pydantic constr and mypy checking

I'm using pydantic to validate a Json/Dict input. But I'm also using mypy to validate the type integrity of the code. When using the pydantic.constr type, which, among other things, validates if a given string respects a regex, I get a mypy…
Cristiano Araujo
  • 1,632
  • 2
  • 21
  • 32
5
votes
3 answers

Python type hints: how to do a literal range

I use type hints with pydantic to set return schema for my Python API. I would like to write a literal type that allows the numbers 0 to 100. This is easy typing it out manually: from typing import Literal MyType = Literal[0, 1, 2, ... , 99,…
Student
  • 522
  • 1
  • 6
  • 18
5
votes
1 answer

Overriding FastAPI's HTTPException response body

I'm currently writing a few end points for an API in fastAPI. I'm defining classes that extend fastapi's HTTPException. The problem is that HTTPException returns a response body with an attribute called detail which will either be a string or a json…
neo-devnull
  • 51
  • 1
  • 3
5
votes
1 answer

FastAPI: datetime with timezone in request doesn't work

from fastapi import FastAPI from datetime import datetime from ..models import Contact from ..database import Database app = FastAPI() # Dependency def get_db(): db = Database() try: yield db finally: …
Pei
  • 11,452
  • 5
  • 41
  • 45
5
votes
1 answer

How to generate a strict json schema with pydantic?

I recently started to use pydandic to generate JSON schemas for validating data but I found the by default the generated schema does not complain about unknown keys inside my BaseModel. Example: class Query(BaseModel): id: str name:…
sorin
  • 161,544
  • 178
  • 535
  • 806
5
votes
1 answer

Integer and string cannot be distinguished with pydantic

from pydantic import BaseModel class AuthenticationResponseSchema(BaseModel): type: str schema = AuthenticationResponseSchema(type=1) Now I'm changing marshmallow to pydantic for schema, model ... But pydantic schema did not validated type…
MyungHun
  • 77
  • 1
  • 8
5
votes
1 answer

Can I create a Unix Time type which automatically converts to datetime in Pydantic?

I receive a JSON response like with a unix timestamp, e.g.: {"protocol": "http", "isPublic": false, "startTime": 1607586354631} The simplest way to read this data with Pydantic is to annotate startTime as int. I would like to annotate it as…
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
5
votes
1 answer

Pydantic - Upgrading object to another model

I have a NewUser model that is something that the end user inputs, I want to update the object to a UserInDB so that I can pass it to my db engine (DynamoDB, which expects a dict) At the moment I'm calling .dict twice, which doesn't feel like the…
Mojimi
  • 2,561
  • 9
  • 52
  • 116
5
votes
3 answers

FastAPI and Pydantic RecursionError Causing Exception in ASGI application

Description I've seen similar issues about self-referencing Pydantic models causing RecursionError: maximum recursion depth exceeded in comparison but as far as I can tell there are no self-referencing models included in the code. I'm just just…
TheDataFox
  • 172
  • 1
  • 2
  • 15