153 questions
1
vote
1
answer
30
views
Generate Open API spec from Pydantic with references to discriminators
I am trying to create a set of POJOs from an OpenAPI specification that is generated from a pydantic data model. The problem I'm hitting is with discriminators. I am currently using the following ...
Advice
0
votes
1
replies
37
views
Pydantic v2 RootModel - Complex data structure
Im trying to use Pydantic's RootModel to map a complex data structure.
relations = dict(
r2 = dict(
index = 2,
conditions = [
dict(
...
0
votes
0
answers
84
views
verify Lazy fetch with Pydantic
I am creating a API to return pydantic backed object, with optional / "fetchable" fields that might not be fetched based on the API parameter passed in.
when accessing these optional data ...
3
votes
3
answers
88
views
How to handle custom date format (dd/mm/yyyy) in query params with inherited model?
I'm building a FastAPI app (actually a small package) that defines a base model for a webhook endpoint. Other developers can inherit from this model and extend it for their own use cases.
The webhook ...
2
votes
0
answers
113
views
In Pydantic 2, how do allow missing fields during deserialisation/validation, but require it when creating an instance?
I use pydantic to be able to serialise and deserialise json data stored in a large database. Each of these json strings represent a pydantic model MyModel.
class MyModel(BaseModel):
attr1: int
I ...
1
vote
0
answers
75
views
How can I use ImportString in Pydantic to get an instance of a class?
I am trying to use an ImportString to get an instance of a given object:
from __future__ import annotations
from pydantic.types import ImportString
from pydantic import Field, BaseModel
from typing ...
1
vote
0
answers
151
views
In what order do model_validators run in Pydantic?
The Pydantic documentation explicitly describes the order of field validators, but what about model_validators?
In what order do model_validators run?
Specifically, how are they ordered in an ...
5
votes
1
answer
756
views
Pydantic: How to return user friendly validation error messages?
Is there any way to change the validation messages from pydantic? The problem is this: I want to return these validation messages to my frontend but not all of the users prefer the language english ...
1
vote
0
answers
117
views
Pydantic v2 + Python 3.13: PEP 695 type-alias of a generic model returns TypeAliasType (no model_rebuild()) — how can I keep a real BaseModel?
I’m experimenting with Python 3.13 and Pydantic v2.11.7.
My goal is to avoid writing
GenericTraining[Discriminate[L1LossConfig]]
over and over—I’d like a terse alias such that I can do
TrainingLike[...
1
vote
0
answers
146
views
How to define a Pydantic model that extracts nested attribute values from an ORM object when using from_attributes=True?
I'm using Pydantic v2 to define different response models depending on the use case. Some models reflect the structure of the ORM model exactly, and .model_validate(obj) works fine in those cases.
...
2
votes
0
answers
252
views
Resolving $ref $defs to get properties from Pydantic model_jason_schema
I'm using a library that utilizes Pydantic v2 and .model_json_schema()['properties'] to pull information and metadata about the class properties (like those set by Field json_schema_extra). I've found ...
1
vote
0
answers
156
views
How can I extend an annotated type
Using Python 3.10, I have a base type definition that I use in many models:
Alpha = Annotated[str, Field(pattern=r'[A-Za-z]')]
I want to create additional specialised types based on this type, for ...
2
votes
1
answer
124
views
How do conditionally apply field constraints based on value type in pydantic v2?
I have this pydantic model with a field, and this field could be either an int or a non numeric value like a str or list.
from pydantic import BaseModel, Field
class Foo(BaseModel):
bar: str | ...
0
votes
1
answer
61
views
Set initial value of a Pydantic 2 field using validator
Say I have the following Pydantic 2.10.6 model, where x is dynamically calculated from another field, y:
from pydantic import BaseModel, Field, field_validator, ValidationInfo
class Foo(BaseModel):
...
1
vote
2
answers
990
views
How can I handle initial settings with Pydantic Settings?
I have an app that is largely configured by environment variables.
I use Pydantic Settings to define the settings available, and validate them.
I have an initial set of settings, and the regular app ...
1
vote
0
answers
22
views
Change required fields to non-required fields [duplicate]
We are using pydantic to validate our API request payload. We have insert and update endpoint.
Both endpoint have same payload requirements. We created model for that:
from pydantic import Field
from ...
2
votes
1
answer
348
views
How to make Pydantic's non-strict, coercive mode apply to integer literals?
I'm validating inputs to a function using Pydantic's @validate_call as follows:
from typing import Literal
from pydantic import validate_call
@validate_call
def foo(a: Literal[0, 90, 180, 270]) -> ...
0
votes
0
answers
2k
views
pydantic version update : Field defined on a base class was overridden by a non-annotated attribute
I updated the pydantic version and code is breaking with the error
Field 'allowed_type' defined on a base class
was overridden by a non-annotated attribute. All field
definitions, including overrides, ...
0
votes
2
answers
132
views
Can I have an `Optional` constrained type with default `None` in pydantic V2
maybe someone can help me out with a pydantic issue. I have been reading up on some reports of this, but I still do not understand how to circumvent the problem:
https://docs.pydantic.dev/latest/...
0
votes
0
answers
55
views
pydantic v2 - aggregate errors when using mix of native and custom types with Annotated
I have the following code:
from .consts import BASE_URL, HOSTNAME_PATTERN
class GenericParseException(Exception):
pass
def validate_hostname(value: str) -> str:
if value.startswith("...
1
vote
0
answers
476
views
Pydantic User Error when Importing `OpenAIEmbeddings` from `langchain_openai`
I am encountering an error when trying to import OpenAIEmbeddings from langchain_openai. Here is the exact import statement I am using:
from langchain_openai import OpenAIEmbeddings
When I run this ...
2
votes
1
answer
127
views
How to modify serialization of models recursively in pydantic V2
in V1 I was able to recursively add information to the BaseModel.dict method. Is something similar possible in V2?
In V1 this was possible:
from typing import Optional
from pydantic.v1 import ...
0
votes
1
answer
487
views
How to remove $defs and $ref when creating a nested JSON Schema using Pydantic?
I am trying to create the following JSON Schema
from pydantic import BaseModel, Field
class VendorInfo(BaseModel):
vendor_name: str = Field("", description= "Vendor Name")
...
3
votes
2
answers
982
views
Implementing a Lazy evaluated field for Pydantic v2
I'm trying to implement a Lazily evaluated generic field type for Pydantic v2. This is the simple implementation I have. You can assign either a value, a function or an async function to the lazy ...
-1
votes
1
answer
251
views
pydantic field_validator invoking problem
someone before asked this question but the answers didnt help in my case, im trying to use an app called powershell-empire starkiller on kali linux, running it from terminal looks like: sudo ...