Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
How can I create a Model that will take any other model or standard dict as a property using Pydantic's BaseModel?

Given a FastAPI Pydantic model MessageWrapper *untested:

class MessageWrapper(BaseModel):
    id: str = ids.get_time_sequential_uuid(datetime.datetime.utcnow())
    body: Union[DataMin, DataMax] = None
    created_by_app: Optional[AnyStr]
    created_by_app_version: Optional[AnyStr]
    created_at_utc: datetime = datetime.datetime.utcnow()

The MessageWrapper.body property expects a DataMin or DataMax model. How can I change this to take either an unvalidated standard Dict or ANY model (not just DataMin or DataMax).

My initial attempts ran into quite a few scenarios where Pydantic threw warnings for not properly defining the type of the body property. I expect to either pass standard dicts or models through this wrapper prior to the data being supplied through an API endpoint.

Ultimately the compiled nested dict would be received by the requestor and they could always expect to access the top level fields, while the body field could be any nested dictionary.

I am interested in functionality similar to Maxwell's Daemon where json data is inserted into a standard dictionary that includes metadata about the message itself.

It seems there would be times when it is OK to designate a property with Type (from typing import Type) to avoid validation, and other times where we'd always validate on all properties.

How can I create a Model that will take any other model or standard dict as a property using Pydantic's BaseModel?

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!