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

fastapi: mapping sqlalchemy database model to pydantic geojson feature

I just started playing with FastAPI, SQLAlchemy, Pydantic and I'm trying to build a simple API endpoint to return the rows in a postgis table as a geojson feature collection. This is my sqlalchemy model: class Poi(Base): __tablename__ = 'poi' …
dlqmap
  • 101
  • 3
10
votes
2 answers

Constructor and Pydantic

I want to create a Pydantic class with a constructor that does some math on inputs and set the object variables accordingly: class PleaseCoorperate(BaseModel): self0: str next0: str def __init__(self, page: int, total: int, size: int): …
Jenia Ivanov
  • 2,485
  • 3
  • 41
  • 69
10
votes
2 answers

How do I use FastAPI's "response_model_exclude_none=True" to exclude "None" values from nested models?

FastAPI shows that you can set response_model_exclude_none=True in the decorator to leave out fields that have a value of None: https://fastapi.tiangolo.com/tutorial/response-model/#use-the-response_model_exclude_unset-parameter I would like to do…
maccam912
  • 792
  • 1
  • 7
  • 22
10
votes
1 answer

What is the Purpose of Pydantic's SecretStr?

I am learning the Pydantic module, trying to adopt its features/benefits via a toy FastAPI web backend as an example implementation. I chose to use Pydantic's SecretStr to "hide" passwords. I know it is not really secure, and I am also using passlib…
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
10
votes
3 answers

How to extend a Pydantic object and change some fields' type?

There are two similar pydantic object like that. The only difference is some fields are optionally. How can I just define the fields in one object and extend into another one? class ProjectCreateObject(BaseModel): project_id: str …
woody
  • 422
  • 1
  • 4
  • 12
9
votes
1 answer

Python Enum and Pydantic : accept enum member's composition

I have an enum : from enum import Enum class MyEnum(Enum): val1 = "val1" val2 = "val2" val3 = "val3" I would like to validate a pydantic field based on that enum. from pydantic import BaseModel class MyModel(BaseModel): …
jossefaz
  • 3,312
  • 4
  • 17
  • 40
9
votes
2 answers

POST request response 422 error {'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]}

My POST request continues to fail with 422 response, even though valid JSON is being sent. I am trying to create a web app that receives an uploaded text file with various genetic markers and sends it to the tensorflow model to make a cancer…
ahutchful
  • 95
  • 1
  • 1
  • 6
9
votes
2 answers

How to properly handle join results from SQLAlchemy with Pydantic/FastAPI

I would like a piece of advice on handling the result of a join operation performed in SQLAlchemy and do the serialization with Pydantic (in FastAPI). If I am not mistaken, the result of the join on two table leads to a list of tuples of SQLAlchemy…
Flavien Lambert
  • 690
  • 1
  • 9
  • 22
9
votes
2 answers

Private attributes in `pydantic`

I'm trying to get the following behavior with pydantic.BaseModel: class MyClass: def __init__(self, value: T) -> None: self._value = value # Maybe: @property def value(self) -> T: return self._value # Maybe: …
mkl
  • 635
  • 1
  • 6
  • 16
9
votes
1 answer

Error Value not declarable with JSON Schema for PurePath with Pydantic and FastAPI

I'm trying to add a field path of type PureWindowsPath to my model. After implementing a custom validator as suggested here https://github.com/samuelcolvin/pydantic/issues/2089#issuecomment-890018075 I get the following error when trying to access…
Teharez
  • 501
  • 8
  • 25
9
votes
1 answer

pydantic.error_wrappers.ValidationError: 1 validation error for B

When I try to parse JSON object with Pydantic, my IDE returns error... Code: from pydantic import BaseModel, Field class A(BaseModel): a: str = Field(None, alias="А") class B(BaseModel): b: dict[str, A] = Field(None, alias="Б") j = { …
Azat
  • 139
  • 1
  • 1
  • 5
9
votes
2 answers

Initialize a Literal enum in a pydantic model

pydantic supports regular enums just fine, and one can initialize an enum-typed field using both an enum instance and an enum value: from enum import Enum from pydantic import BaseModel class MyEnum(Enum): FOO = 'foo' BAR = 'bar' class…
Florian Brucker
  • 9,621
  • 3
  • 48
  • 81
9
votes
3 answers

Is there any post_load in pydantic?

Previously I used the marshmallow library with the Flask. Some time ago I have tried FastAPI with Pydantic. At first glance pydantic seems similar to masrhmallow but on closer inspection they differ. And for me the main difference between them is…
egvo
  • 1,493
  • 18
  • 26
9
votes
2 answers

List of object attributes in pydantic model

I use Fast API to create a web service. There are following sqlAlchemy models: class User(Base): __tablename__ = 'user' account_name = Column(String, primary_key=True, index=True, unique=True) email = Column(String, unique=True,…
Dmitry Ezhov
  • 315
  • 2
  • 3
  • 10
8
votes
2 answers

Import vaex error: PydanticImportError: `BaseSettings` has been moved to the `pydantic-settings` package

I am using Sagemaker notebook and when importing vaex, I am getting the below error. the version of vaex I'm using is 4.16.0 PydanticImportError: BaseSettings has been moved to the pydantic-settings package. See…
Kailash M S
  • 81
  • 1
  • 2