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

How to exclude Optional unset values from a Pydantic model using FastAPI?

I have this model: class Text(BaseModel): id: str text: str = None class TextsRequest(BaseModel): data: list[Text] n_processes: Union[int, None] So I want to be able to take requests like: {"data": ["id": "1", "text": "The text…
NineWasps
  • 2,081
  • 8
  • 28
  • 45
8
votes
1 answer

Inheritance/subclassing issue in Pydantic

I came across a code snippet for declaring Pydantic Models. The inheritance used there has me confused. class RecipeBase(BaseModel): label: str source: str url: HttpUrl class RecipeCreate(RecipeBase): label: str source: str url:…
AKG
  • 598
  • 6
  • 18
8
votes
1 answer

Argument parser from a Pydantic model

How do I create an argument parser (argparse.ArgumentParser) from a Pydantic model? I have a Pydantic model: from pydantic import BaseModel, Field class MyItem(BaseModel): name: str age: int color: str = Field(default="red",…
miksus
  • 2,426
  • 1
  • 18
  • 34
8
votes
2 answers

Conditional call of a FastAPI Model

I have a multilang FastAPI connected to MongoDB. My document in MongoDB is duplicated in the two languages available and structured this way (simplified example): { "_id": xxxxxxx, "en": { "title": "Drinking Water Composition", …
c24b
  • 5,278
  • 6
  • 27
  • 35
8
votes
1 answer

How To Get Pydantic To Discriminate On A Field Within List[Union[TypeA, TypeB]]?

I am trying to use Pydantic to validate a POST request payload for a Rest API. A list of applicants can contain a primary and optional other applicant. So far, I have written the following Pydantic models listed below, to try and reflect this. The…
anon_dcs3spp
  • 2,342
  • 2
  • 28
  • 62
8
votes
1 answer

How can I decode a JSON string into a pydantic model with a dataframe field?

I am using MongoDB to store the results of a script into a database. When I want to reload the data back into python, I need to decode the JSON (or BSON) string into a pydantic basemodel. With a pydantic model with JSON compatible types, I can just…
Tom McLean
  • 5,583
  • 1
  • 11
  • 36
8
votes
3 answers

How to prevent Pydantic from throwing an exception on ValidationError

How to prevent Pydantic from throwing an exception on ValidationError? from pydantic import BaseModel, NonNegativeInt class Person(BaseModel): name: str age: NonNegativeInt details: None p1: Person = Person(name='Alice', age=30,…
RaamEE
  • 3,017
  • 4
  • 33
  • 53
8
votes
2 answers

Allow null in JSON schema from Pydantic

I'm trying to allow null in the JSON schema for this object: from pydantic import BaseModel from typing import Optional class NextSong(BaseModel): song_title: Optional[str] = ... but the schema which results is as follows: { "title":…
aleph-null
  • 443
  • 4
  • 11
8
votes
2 answers

Why does FastAPI execute the Pydantic constructor twice when returning from the route function?

I have a use case where I want to create some values inside my class on the construction of the model. However, when I return the class to FastAPI for converting to JSON when I call the API, the constructor gets run again and I can get differing…
Ryan Goss
  • 113
  • 1
  • 6
8
votes
4 answers

Pydantic Model: Convert UUID to string when calling .dict()

Thank you for your time. I'm trying to convert UUID field into string when calling .dict() to save to a monogdb using pymongo. I tried with .json() but seems like mongodb doesn't like it TypeError: document must be an instance of dict, bson.son.SON,…
qangdev
  • 336
  • 5
  • 15
8
votes
2 answers

How to include non-pydantic classes in fastapi responses?

I want to include a custom class into a route's response. I'm mostly using nested pydantic.BaseModels in my application, so it would be nice to return the whole thing without writing a translation from the internal data representation to what the…
Arne
  • 17,706
  • 5
  • 83
  • 99
8
votes
4 answers

How to inflect from snake case to camel case post the pydantic schema validations

I can able to find a way to convert camelcase type based request body to snake case one by using Alias Generator, But for my response, I again want to inflect snake case type to camel case type post to the schema validation. Is there any way I can…
saravana kumar
  • 255
  • 1
  • 3
  • 10
8
votes
3 answers

Writing a pydantic object into a sqlalchemy json column

I'm looking for a way to have a pydantic object stored in a sqlalchemy json column. My attempts so far are being tripped up by a datetime field in the pydantic object. I feel like I'm missing something obvious. My first attempt was to simply…
Philip Couling
  • 13,581
  • 5
  • 53
  • 85
8
votes
2 answers

How to define lists in python dot env file?

In Fast API documentation it is recommended to use .env to load the config. Only that it only supports string as far as I understand it. from fastapi import FastAPI from pydantic import BaseSettings class Settings(BaseSettings): api_tokens =…
Houman
  • 64,245
  • 87
  • 278
  • 460
8
votes
3 answers

How to disable schema checking in FastAPI?

I am migrating a service from Flask to FastAPI and using Pydantic models to generate the documentation. However, I'm a little unsure about the schema check. I'm afraid there will be some unexpected data (like a different field format) and it will…
Felipe Nunes
  • 83
  • 1
  • 5