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

Is there a better way to serve and encode a large SQLAlchemy dataset from FastAPI?

I want to return a large dataset using FastAPI StreamingResponse and in the repository/logic layer, after doing my query stuff, I'm returning the data in this way: for record in query.yield_per(DATA_RECORDS_LIMIT): yield…
Andrea Grandi
  • 925
  • 11
  • 18
5
votes
1 answer

FastApi/Pydantic access many to one relationship from parent table

I have a structure like this: SqlAlchemy models class MPrueba(Base): __tablename__ = 'M_pruebas' idpruebas = Column(Integer, primary_key=True) idfuentes = Column(ForeignKey('M_fuentes.idfuentes', ondelete='RESTRICT',…
Master Yoda
  • 489
  • 1
  • 9
  • 18
5
votes
1 answer

How to add images to models (tables) and save it to database in FastAPI framework?

I deal with FastAPI and can't figure out how to make simple (from the point of view of my experience with django) thing - add a product with an image to the database. As I understood it, the image should be stored in a static folder and only a link…
Jekson
  • 2,892
  • 8
  • 44
  • 79
5
votes
1 answer

Validating Pydantic field while setting value

A Pydantic class that has confloat field cannot be initialised if the value provided for it is outside specified range. But when setting this field at later stage (my_object.constrained_field = ) the new value is not validated. from…
MaciekS
  • 401
  • 8
  • 18
5
votes
1 answer

FastAPI File(s) must be put before form in function parameters

I have an endpoint that takes a file and a string parameter that I pass through a form body. But I noticed when debugging that: import uvicorn from fastapi import FastAPI, File, Form app = FastAPI() @app.post('/test') def test(test_item: str =…
Ned U
  • 401
  • 2
  • 7
  • 15
5
votes
1 answer

What is the best way to handle conditionally required arguments in a FastAPI app?

I am developing a FastAPI application. I have with the following schema class Address(BaseModel): address_string: str = Field(None) address_street: str = Field(None) addres_number: str = Field(None) I like to have the field…
Nicolas Martinez
  • 719
  • 1
  • 6
  • 23
5
votes
1 answer

How to use `from_orm` if the pydantic model defines aliases?

Though the pydantic's ORM mode is documented here, unfortunately there is no documentation for usage with aliases. How to use from_orm if the pydantic model defines aliases? It seems that the from_orm factory forgets about all non-aliased names if…
Min-Soo Pipefeet
  • 2,208
  • 4
  • 12
  • 31
5
votes
3 answers

Get the field name of a class without hardcoding

I have a Pydantic model class like this class User(BaseModel): username: str firstname: str lastname: str Now, I want to be able to reference "username" without hardcoding (like how I was able to reference the class…
MD Luffy
  • 536
  • 6
  • 18
5
votes
1 answer

Usage of pydantic with mypy

I'm trying to write an application using FastAPI which intensively uses pydantic. Also I would like to type-check my code using mypy. How can I use type annotations for pydantic and mypy without conflict? I know about type: ignore comments but in my…
Andrey Semakin
  • 2,032
  • 1
  • 23
  • 50
5
votes
3 answers

pydantic and subclasses of abstract class

I am trying to use pydantic with a schema that looks as the following: class Base(BaseModel, ABC): common: int class Child1(Base): child1: int class Child2(Base): child2: int class Response(BaseModel): events: List[Base] events…
Apostolos
  • 7,763
  • 17
  • 80
  • 150
4
votes
0 answers

Pydantic 2.0 ignores Optional in schema and requires the field to be available

Pydantic 2.0 seems to have drastically changed. Previously with FastAPI and Pydantic 1.X I could define the schema like this, where receipt is optional: class VerifyReceiptIn(BaseModel): device_id: str device_type: DeviceType receipt:…
Houman
  • 64,245
  • 87
  • 278
  • 460
4
votes
2 answers

Pydantic model fields with typing.Optional[] vs. typing.Optional[] = None

What is the distinction between implicitly setting an optional attribute to None with typing.Optional[] versus explicitly assigning typing.Optional[] = None when creating Pydantic models? In both cases, the attribute will eventually have a value of…
4
votes
3 answers

TypeError: issubclass() arg 1 must be a class when importing langchain in Flask

I am doing a microservice with a document loader, and the app can't launch at the import level, when trying to import langchain's UnstructuredMarkdownLoader $ flask --app main run --debug Traceback (most recent call last): File "venv/bin/flask",…
prout
  • 317
  • 5
  • 18
4
votes
1 answer

How to set a Pydantic field value depending on other fields

from pydantic import BaseModel class Grafana(BaseModel): user: str password: str host: str port: str api_key: str | None = None GRAFANA_URL = f"http://{user}:{password}@{host}:{port}" API_DATASOURCES =…
An old man in the sea.
  • 1,169
  • 1
  • 13
  • 30
4
votes
2 answers

How to make case insensitive choices using Python's enum and FastAPI?

I have this application: import enum from typing import Annotated, Literal import uvicorn from fastapi import FastAPI, Query, Depends from pydantic import BaseModel app = FastAPI() class MyEnum(enum.Enum): ab = "ab" cd = "cd" class…
Amin Ba
  • 1,603
  • 1
  • 13
  • 38