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

Pydantic autocompletion in VS Code

When i use pydantic in VS Code, the code snippet shows User(**data: Any). Is there any way for VS Code to show correct documentation? like User(name: str, email: str)
Nicolas Acosta
  • 742
  • 5
  • 12
14
votes
2 answers

How to make custom data class subscriptable

Consider this data class derived from the pydantic package: from typing import List from pydantic import BaseModel class Bucket(BaseModel): setting: List[str] fight_1: List[int] cause_1: List[str] let my_bucket be an instance of…
user189035
  • 5,589
  • 13
  • 52
  • 112
14
votes
1 answer

Read a body JSON list with FastAPI

The body of an HTTP PUT request is a JSON list - like this: [item1, item2, item3, ...] I can't change this. (If the root was a JSON object rather than a list there would be no problem.) Using FastAPI I seem to be unable to access this content in…
Ian Goldby
  • 5,609
  • 1
  • 45
  • 81
13
votes
2 answers

How to get Alembic to recognise SQLModel database model?

Using SQLModel how to get alembic to recognise the below model? from sqlmodel import Field, SQLModel class Hero(SQLModel, table=True): id: int = Field(default=None, primary_key=True) name: str secret_name: str age: Optional[int] =…
Greg
  • 8,175
  • 16
  • 72
  • 125
13
votes
1 answer

How to write tests for Pydantic models in FastAPI?

I just started using FastAPI but I do not know how do I write a unit test (using pytest) for a Pydantic model. Here is a sample Pydantic model: class PhoneNumber(BaseModel): id: int country: str country_code: str number: str …
PercySherlock
  • 371
  • 1
  • 2
  • 12
13
votes
2 answers

FastAPI/Pydantic accept arbitrary post request body?

I want to create a FastAPI endpoint that just accepts an arbitrary post request body and returns it. If I send {"foo" : "bar"} , I want to get {"foo" : "bar"} back. But I also want to be able to send {"foo1" : "bar1", "foo2" : "bar2"} and get that…
Jabrove
  • 718
  • 5
  • 13
13
votes
4 answers

Test Pydantic settings in FastAPI

Suppose my main.py is like this (this is a simplified example, in my app I use an actual database and I have two different database URIs for development and testing): from fastapi import FastAPI from pydantic import BaseSettings app =…
Matteo Silvestro
  • 430
  • 1
  • 7
  • 16
13
votes
1 answer

Pydantic: How do I use a keyword field name?

I'm trying to model an API request in Pydantic. I have to model a field called "from". Since "from" is a keyword in python, Pydantic throws an error. Model class MyRequest(BaseModel): foo: str abc: int from: int Error thrown by…
shrshank
  • 131
  • 1
  • 3
13
votes
3 answers

Creating Pydantic Model Schema with Dynamic Key

I'm trying to implement Pydantic Schema Models for the following JSON. { "description": "Best Authors And Their Books", "authorInfo": { "KISHAN": { "numberOfBooks": 10, "bestBookIds": [0, 2, 3,…
navule
  • 3,212
  • 2
  • 36
  • 54
12
votes
1 answer

FastAPI shows 'msg': 'field required', 'type': 'value_error.missing'

Good evening everyone. I'm trying to make a request to add new user to my database using FastAPI. When I try to do this through the python console app, FastAPI shows me this message: { 'detail': [ { 'loc': ['body',…
Cross
  • 497
  • 1
  • 3
  • 13
12
votes
4 answers

pydantic exclude multiple fields from model

In pydantic is there a cleaner way to exclude multiple fields from the model, something like: class User(UserBase): class Config: exclude = ['user_id', 'some_other_field'] I am aware that following works, but I was looking…
muon
  • 12,821
  • 11
  • 69
  • 88
12
votes
1 answer

How to make a pydantic Field accept subclasses using Type?

I'm trying to have a field in one Pydantic model accept any of a set of BaseModel-derived classes or subclasses that I define separately. Reading the docs here, I naively did the below, which failed; I then realised that I'd misread the docs and…
Winawer
  • 671
  • 8
  • 26
12
votes
1 answer

Pydantic input model for partial updates

I am using Pydantic to validate data inputs in a server. Pydantic models: User: for common fields UserIn: user input data to create new account UserInDB: to hash password and include extra fields like created_at UserOut: to output created/updated…
Sami Al-Subhi
  • 4,406
  • 9
  • 39
  • 66
12
votes
2 answers

Unhashable type in FastAPI request

I am writing a post-api using fastapi. The required request-format is: { "leadid":LD123, "parties":[ { "uid":123123, "cust_name":"JOhn Doe", }, ...]} The fastapi code in python is: class Customer(BaseModel): …
Ajeet Mishra
  • 342
  • 2
  • 8
12
votes
3 answers

Update SQLAlchemy ORM existing model from posted Pydantic model in FastAPI?

I want to expose an API method that receives data in a POST request (for a beta signup API), and upsert with new values when there's already a matching model. What's the best way to achieve this? Currently I'm doing this (somewhat simplified): My…
Dhiraj Gupta
  • 9,704
  • 8
  • 49
  • 54