#4239·pybind11

pybind11 + pydantic

Author: mariannasbCreated Oct 13, 2022Updated Jun 8, 2026
Labelstriage

Required prerequisites

Problem description

I want to use classes generated through pybind11 for pydantic basemodels but I'm having trouble getting that to work.

pydantic seems to have support for using NamedTuples or TypedDicts, but my classes dont seem to be either, I dont know if it is possible to get pybind11 to make them like that?

And I have also found people doing it by casting their classes as pydantic dataclasses: https://stackoverflow.com/questions/68651305/create-a-pydantic-basemodel-definition-with-external-class-or-dictionary

But that approach didn't work either for me, I get pydantic models but they seem to be empty, no variables. So I expect that is something missing from the generated classes, or just not the way pydantic expects it to be.

Reproducible example code

No response