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

431
Views
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 where my endpoint accepts a body of the form All, and returns a new resource of that same form. I want to be able to specify that the return type All is All plus an extra id field.

i.e.

RequestAll = Union[A,B]

class ResponseAll(RequestAll):
  id = str

However, when I do this, I get an error:

__init__() takes 2 positional [but more were given]

Another approach I could take is to have a RequestA and ResponseA equivalent for every type, then union Requests and Responses respectively. However, I would prefer to avoid this given the amount of types I actually have.

Is there a neat way to address this problem?

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!