Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

194
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda