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
12
votes
5 answers

pydantic BaseModel not found in Fastapi

I have python3 3.6.9 on Kubuntu 18.04. I have installed fastapi using pip3 install fastapi. I'm trying to test drive the framework through its official documentation and I'm in the relational database section of its guide. In schemas.py: from typing…
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
12
votes
3 answers

Pydantic validator to remove blank strings?

I have the following Pydantic model: class Report(BaseModel): id: int name: str grade: float = None proportion: float = None @validator('*', pre=True) def blank_strings(cls, v): print(v) if v == "": …
pyjamas
  • 4,608
  • 5
  • 38
  • 70
11
votes
2 answers

How should I specify default values on Pydantic fields with "Validate Always" to satisfy type checkers?

My type checker moans at me when I use snippets like this one from the Pydantic docs: from datetime import datetime from pydantic import BaseModel, validator class DemoModel(BaseModel): ts: datetime = None # Expression of type "None" cannot…
LondonRob
  • 73,083
  • 37
  • 144
  • 201
11
votes
1 answer

Pydantic add field to model after a model validation (add filed to incoming model)

I have an incoming pydantic User model. To add field after validation I'm converting it to dict and adding a field like for a regular dict. user = employee.dict() user.update({'invited_by': 'some_id'}) db.save(user) Is there a shorter AND CLEANEST…
salius
  • 918
  • 1
  • 14
  • 30
11
votes
4 answers

How to map values from nested dict to Pydantic Model?

I am trying to map a value from a nested dict/json to my Pydantic model. For me, this works well when my json/dict has a flat structure. However, I am struggling to map values from a nested structure to my Pydantic Model. Lets assume I have a…
azo91
  • 209
  • 1
  • 6
  • 15
11
votes
1 answer

Pydantic: How to use one field's value to set values for other fields?

What I Have Dictionary: user_dict = {'user': {'field1': 'value1', 'field2': 'value2'}, 'admin':{'field1': 'value3', 'field2': 'value4'}} Pydantic Model: class User(BaseModel): …
Manas Sambare
  • 1,158
  • 2
  • 11
  • 22
11
votes
2 answers

How to use discriminated union types in FastAPI body validation? (Union on models)

I know a concept from Typescript called Discriminated unions. That's a thing where you put 2 struct (classes, etc) and the type is decided depending on a struct's values. I'm trying to achieve similar thing in FastAPI with Pydantic validation. There…
Honza Sedloň
  • 358
  • 10
  • 27
11
votes
4 answers

How to delete empty field?

I have a BaseModel like this from pydantic import BaseModel class TestModel(BaseModel): id: int names: str = None While I validate some data like this TestModel(id=123).dict() I got result {'id': 123, 'name': None} But what I expect…
fallthrough
  • 113
  • 1
  • 1
  • 5
11
votes
1 answer

Pydantic model for array of jsons

I am using FastAPI to write a web service. It is good and fast. FastAPI is using pydantic models to validate input and output data, everything is good but when I want to declare a nested model for array of jsons like below: [ { "name":…
kamal
  • 240
  • 1
  • 2
  • 9
10
votes
2 answers

How to persist LangChain conversation memory (save and load)?

I'm creating a conversation like so: llm = ChatOpenAI(temperature=0, openai_api_key=OPENAI_API_KEY, model_name=OPENAI_DEFAULT_MODEL) conversation = ConversationChain(llm=llm, memory=ConversationBufferMemory()) But what I really want is to be able…
Neil C. Obremski
  • 18,696
  • 24
  • 83
  • 112
10
votes
2 answers

Using different Pydantic models depending on the value of fields

I have 2 Pydantic models (var1 and var2). The input of the PostExample method can receive data either for the first model or the second. The use of Union helps in solving this issue, but during validation it throws errors for both the first and the…
Александр
  • 103
  • 1
  • 1
  • 5
10
votes
3 answers

How to build a self-referencing model in Pydantic with dataclasses?

I am building an API using FastAPI and pydantic. As I follow DDD / clean architecture, which separates the definition of the model from the definition of the persistence layer, I use standard lib dataclasses in my model and then map them to…
Lionel Hamayon
  • 1,240
  • 15
  • 25
10
votes
3 answers

Distinguishing between Pydantic Models with same fields

I'm using Pydantic to define hierarchical data in which there are models with identical attributes. However, when I save and load these models, Pydantic can no longer distinguish which model was used and picks the first one in the field type…
twhughes
  • 456
  • 6
  • 17
10
votes
2 answers

Best way to flatten and remap ORM to Pydantic Model

I am using Pydantic with FastApi to output ORM data into JSON. I would like to flatten and remap the ORM model to eliminate an unnecessary level in the JSON. Here's a simplified example to illustrate the problem. original output: {"id": 1,…
STEM FabLab
  • 380
  • 3
  • 13
10
votes
2 answers

fastapi + sqlalchemy + pydantic → how to process many-to-many relations

I have editors and articles. Many editors may be related to many articles and many articles may have many editors at same time. My DB tables are Article id subject text 1 New Year Holidays In this year... etc etc…
rzlvmp
  • 7,512
  • 5
  • 16
  • 45