pybind / pybind/pybind11

pybind11 + pydantic

Open
#4,239 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the pybind11 documentation and the reported Pydantic integration approaches. Build a minimal reproducible example, since none is included, and determine whether bound classes can expose the structure Pydantic requires. Done should be a clear compatibility result with documented behavior or a narrowly defined implementation path.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.