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

ListError when mapping Pydantic class to csv

I am trying to use pydantic classes to represent records of a CSV. Some fields in this CSV represent things like numbers, dates, encoded lists that are better handled as such. So I assign the appropriate type to the coresponding pydantic field and…
Jessica
  • 191
  • 5
0
votes
1 answer

autodoc_pydantic: Show fields from parent model

I'm using the autodoc_pydantic plugin for Sphinx to document my pydantic.BaseModel classes. However, there are cases where I have something like class Foo(pydantic.BaseModel): '''Foo class''' x: str = pydantic.Field(description='The…
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
0
votes
1 answer

Autodoc failing with class that has nested pydantic model

As my MRE, I've got the following file: blah.py '''Blah module''' import pydantic class Foo: '''Foo class''' class Bar(pydantic.BaseModel): '''Bar class''' x: str = pydantic.Field(description='The x.') …
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
0
votes
1 answer

Sanic request validation using Pydantic: Exception occurred while handling uri:

I have followed the example at https://sanic.dev/en/plugins/sanic-ext/validation.html#validation-with-pydantic to implement validation on my Sanic routes using Pydantic. If I make a request to the route with valid request paramaters, the code…
Scratcha
  • 1,359
  • 1
  • 14
  • 22
0
votes
1 answer

How to parse pydantic.SecretStr based on whether an API parameter is true

So I have a model class as follows: class Model(BaseModel): token: SecretStr username: str This model will be the response_model for my API. I'd like my API to accept an optional parameter def api(reveal: bool = False). If the reveal value…
bipster
  • 404
  • 1
  • 8
  • 20
0
votes
1 answer

Pydantic: how to parse non dict objects

The following data is input data = { 'campaigns': [ { 'title': 'GBP', 'geo_segment': 'WW', 'ac_type': 'Value', 'conversion': 'soft', 'asset_type': 'ALL', 'date':…
Jekson
  • 2,892
  • 8
  • 44
  • 79
0
votes
0 answers

Filtering in SQLAlchemy with Lists

I am trying to filter in SQLAlchemy some posts. They can have multiple tags, for instance: 'news', 'tech', 'fintech'. I want the user to be able to search by filtering them (so he can select the tags). In postgres the type is a text[]. What I tried…
Pitis
  • 173
  • 5
0
votes
1 answer

Pydantic validation errors with None values

I have a returned result from a webservice, whose values come as they are (see example). The keys are not Optional and must be included. 2 validation errors for Result: cause - str type expected (type=type.error.str) urls - str type expected…
Semo
  • 783
  • 2
  • 17
  • 38
0
votes
0 answers

Validate pydantic field against a list of values

I would like to query the Meals database table to obtain a list of meals (i.e. 1= breakfast, 2= lunch, 3= dinner, etc.), and validate the Recipe meal_id contains one of these values. Is this possible? Any suggestions how? # Models class…
user18457796
0
votes
1 answer

How to make Pydantic recognize attributes from non-model parent class?

I have a normal Python class: class NormalClass: a: str b: bool I don't want NormalClass to inherit from pydantic.BaseModel class, but I still want another class with the same attributes as NormalClass and it being a Pydantic model. So here's…
Gnut
  • 541
  • 1
  • 5
  • 19
0
votes
1 answer

AttributeError: module 'dataclasses' has no attribute 'is_dataclass'

I am trying to create a dynamic model using pydantic but it seems it can't get even the basic example: from pydantic import BaseModel, create_model MyModel = create_model('MyModel', foo="foo") The error is: MyModel = create_model('MyModel',…
derek
  • 9,358
  • 11
  • 53
  • 94
0
votes
0 answers

How do I inherit from union'd pydantic models

Currently I have quite a few models defined and I want a single endpoint that can receive and return them. My models are all different, e.g. class A(BaseModel): foo: str class B(BaseModel): bar: str All = Union[A, B] Considering the case…
rbhalla
  • 869
  • 8
  • 32
0
votes
1 answer

Reuse function that validates file size [fastapi]

I'm very new to FastAPI. I want to validate the file type and the file size of an uploaded file and raise Exception if it's above the size and doesn't match the type. This file will be uploaded to S3 This is what my code looks…
Koushik Das
  • 9,678
  • 3
  • 51
  • 50
0
votes
1 answer

Multiple checks for custom type in Pydantic

Is there any way to achieve a multiple Exception raise call for a custom type? from pydantic import BaseModel class CustomType(object): @classmethod def validate_first(cls, *args, **kwargs): raise ValueError("first") return…
Marat Idrisov
  • 390
  • 6
  • 8
0
votes
0 answers

FastAPI Crud design

I'm working on a Restful API using FastAPI and pydantic and postgres. I'm looking for schema design that will allow me to get data from user in a POST\PUT requests, enrich the data (default values that shouldn't be set by user or other columns that…
Amirfel
  • 57
  • 1
  • 4