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

How do you export a pydantic model to yaml using anchors?

I would like to export a Pydantic model to YAML, but avoid repeating values and using references (anchor+aliases) instead. Here's an example: from typing import List from ruamel.yaml import YAML # type: ignore import yaml from pydantic import…
user936580
  • 1,223
  • 1
  • 12
  • 19
6
votes
3 answers

Declaring computed python-level property in pydantic

I have a class deriving from pydantic.BaseModel and would like to create a "fake" attribute, i.e. a computed property. The propery keyword does not seem to work with Pydantic the usual way. Below is the MWE, where the class stores value and defines…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
6
votes
1 answer

How to use a reserved keyword in pydantic model

I need to create a schema but it has a column called global, and when I try to write this, I got an error. class User(BaseModel): id:int global:bool I try to use another name, but gives another error when try to save in db.
Matheus
  • 139
  • 4
  • 13
6
votes
0 answers

Equivalent of Marshmallow dump_only fields for Pydantic/FastAPI without multiple schemas

Question Is it possible to replicate Marshmallow's dump_only feature using pydantic for FastAPI, so that certain fields are "read-only", without defining separate schemas for serialization and deserialization? Context At times, a subset of the…
William Daly
  • 172
  • 1
  • 10
6
votes
1 answer

pydantic initialize numpy ndarray

how can I initialize a ndarray when using pydantic? This code throws a ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() from pydantic.dataclasses import dataclass import numpy as…
juerg
  • 479
  • 5
  • 16
6
votes
1 answer

How we can migrate database in sqlmodel?

class Hero(SQLModel, table=True): id: int = Field(primary_key=True) name: str age: int = Field(default=None) status:str I created the table using SQLModel.metadata.create_all(engine), when I changed the type of status to str and run…
faris404
  • 343
  • 1
  • 3
  • 11
6
votes
4 answers

How to use Arrow type in FastAPI response schema?

I want to use Arrow type in FastAPI response because I am using it already in SQLAlchemy model (thanks to sqlalchemy_utils). I prepared a small self-contained example with a minimal FastAPI app. I expect that this app return product1 data from…
Karol Zlot
  • 2,887
  • 2
  • 20
  • 37
6
votes
2 answers

FastAPI Multipart/form data error when uploading File with JSON data

My Pydantic model looks like ths: class Banner: title: str text: str My route looks like this: @router.post('', status_code=201) async def create_banner( banner: Banner, photo: UploadFile = File(...) # multipart/form-data ): …
Hahan't
  • 481
  • 1
  • 4
  • 11
6
votes
2 answers

FastApi - receive list of objects in body request

I need to create an endpoint that can receive the following JSON and recognize the objects contained in it: {​ "data": [ {​ "start": "A", "end": "B", "distance": 6 }​, {​ "start": "A", "end": "E", "distance": 4 }​ …
Reissel Reis
  • 63
  • 1
  • 1
  • 4
6
votes
1 answer

Problems with ABC/Interfaces using pydantic + Mixins pattern

Im trying to implement Mixin patter while Im using Pydantics BaseClass to facilitate the instantiation and validation of data from my class. The problem is that my Mixins cannot inhirit from my base classes (actually, the dependency is the opposite…
Tâmer Cuba
  • 2,373
  • 2
  • 11
  • 26
6
votes
4 answers

encode Pydantic field using the Enum name instead of the value

I have a Enum class: class Group(enum.Enum): user = 0 manager = 1 admin = 2 I have a pydantic model: class User(BaseModel): id: int username: str group: Group It generated serialised to json following: { "id": 5, …
Martin Fischer
  • 469
  • 1
  • 7
  • 21
6
votes
1 answer

Is there a way to exclude Pydantic models from FastAPI's auto-generated documentation?

Is there a way for a FastAPI application to not display a model in its schema documentation? I have some models which are slight variations of others, and with this duplication occurring for each model, the schema documentation is quite…
aleph-null
  • 443
  • 4
  • 11
6
votes
3 answers

FastAPI: How to specify possible values for a field in Pydantic's Basemodel?

I have a model like : # Imports from pydantic import BaseModel # Data Models class MyModel(BaseModel): a: str b: str c: str @app.post('/endpoint_to_post') async def post_log(my_model: MyModel): I want to specify some constraint on…
PicxyB
  • 596
  • 2
  • 8
  • 27
6
votes
1 answer

Why doesn't Pydantic validate field assignments?

I want to use Pydantic to validate fields in my object, but it seems like validation only happens when I create an instance, but not when I later modify fields. from pydantic import BaseModel, validator class MyStuff(BaseModel): name: str …
Robert
  • 7,394
  • 40
  • 45
  • 64
6
votes
4 answers

Python [Pydantic] - default values based other object in the class

I would like to set default email in case email not provided, i.e: name = a last_name = b email = None email will become "a_b@email.com" I tried something like that but obviously didn't work as the name,last_name not define in function. class…
AviC
  • 354
  • 1
  • 5
  • 15