# pybind/pybind11 architecture

> Architecture diagram of the GitHub repository pybind/pybind11, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pybind/pybind11
- Repository: https://github.com/pybind/pybind11

## Overview

#### Purpose and Consumer Entry Points
pybind11 is a header-only C++11+ library for building Python extension modules and embedding/interacting with Python. Consumers include its headers in C++ binding code; no pybind11 runtime library is linked. Primary public APIs are `include/pybind11/pybind11.h`, `include/pybind11/embed.h`, and conversion/feature headers such as `include/pybind11/stl.h` and `include/pybind11/numpy.h`.

The installed Python package supports build-system discovery and integration: `pybind11/__init__.py`, `pybind11/__main__.py`, and `pybind11/setup_helpers.py`.

#### Binding Definition and Python Object Layer
`include/pybind11/pybind11.h` provides module, function, class, enum, and attribute binding declarations. It builds on Python C-API-oriented object wrappers in `include/pybind11/pytypes.h` and shared API/ABI definitions in `include/pybind11/common.h`.

At extension initialization, user binding declarations register metadata and callable wrappers with CPython. Python calls enter generated wrapper functions, which marshal Python arguments, invoke native C++, and convert return values back to Python objects.

#### Type Conversion and Call Dispatch
The central boundary is Python objects ↔ native C++ types. `include/pybind11/cast.h` supplies generic type-caster dispatch; `include/pybind11/detail/type_caster_base.h` handles registered/custom class types; `include/pybind11/detail/argument_vector.h` supports argument collection and overload resolution.

Feature-specific casters extend this mechanism without requiring a separate runtime: `include/pybind11/stl.h`, `include/pybind11/chrono.h`, and `include/pybind11/eigen.h`. NumPy arrays and buffer protocol exchange are handled through `include/pybind11/numpy.h` and `include/pybind11/buffer_info.h`.

#### Class Lifetime, Inheritance, and Virtual Dispatch
C++ class bindings use instance registration, holder ownership, and Python/C++ inheritance machinery concentrated in `include/pybind11/detail/class.h`, `include/pybind11/detail/value_and_holder.h`, and `include/pybind11/trampoline_self_life_support.h`.

This layer governs reference ownership, smart pointers, Python subclass trampolines, and cross-language virtual calls. Smart-holder support is a distinct ownership model in `include/pybind11/detail/struct_smart_holder.h` and `include/pybind11/detail/using_smart_holder.h`.

#### Interpreter State, Errors, and Concurrency
Per-interpreter/shared internal registries and Python type metadata reside in `include/pybind11/detail/internals.h`. C++ exceptions are translated at the language boundary through `include/pybind11/detail/exception_translation.h`; public exception-related APIs are exposed by `include/pybind11/pybind11.h`.

GIL management and newer synchronization facilities are explicit APIs: `include/pybind11/gil.h`, `include/pybind11/critical_section.h`, and `include/pybind11/subinterpreter.h`. These form the principal correctness boundary for threaded code and multiple Python interpreters.

#### Build, Packaging, and Distribution Integration
CMake is the main native integration route: `CMakeLists.txt` defines project/install behavior, while `tools/pybind11Config.cmake.in` and `tools/pybind11Tools.cmake` expose imported targets and helper commands to downstream projects. `CMakePresets.json` provides repository build presets.

Python packaging is defined in `pyproject.toml`; the distributable package primarily ships headers plus CMake integration. Supported external runtimes are CPython 3.9+, PyPy, and GraalPy; optional interoperability targets include NumPy and Eigen.

#### Verification and Delivery Boundaries
The test architecture compiles C++ binding modules and drives them from Python/pytest, rooted at `tests/CMakeLists.txt`, `tests/pybind11_tests.cpp`, and `tests/conftest.py`. This validates the compiled-extension boundary rather than a standalone service runtime.

CI and release delivery use GitHub Actions (`.github/workflows/ci.yml`, `.github/workflows/tests-cibw.yml`) and AppVeyor (`.appveyor.yml`) across Python implementations, OSes, compilers, and wheel builds. Documentation is built from `docs/index.rst` and the Sphinx configuration `docs/conf.py`.

Main entry points: [`include/pybind11/pybind11.h`](https://github.com/pybind/pybind11/blob/master/include/pybind11/pybind11.h) (Binding declarations), [`include/pybind11/cast.h`](https://github.com/pybind/pybind11/blob/master/include/pybind11/cast.h) (Caster and overload dispatch), [`include/pybind11/detail/class.h`](https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/class.h) (Class lifetime and inheritance), [`tools/pybind11Config.cmake.in`](https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in) (CMake integration)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_public["Consumer Interfaces"]
  node_cpp_consumer["C++ binding code<br/>consumer source"]
  node_python_runtime(("Python runtimes"))
  node_binding_api["Binding declarations<br/>public header<br/>[pybind11.h]"]
  node_embedding_api["Embedding API<br/>public header<br/>[embed.h]"]
end

subgraph group_core["Header-Only Binding Core"]
  node_object_api["Python object wrappers<br/>core header<br/>[pytypes.h]"]
  node_common_abi["Shared API and ABI<br/>core header<br/>[common.h]"]
  node_call_dispatch["Caster and overload dispatch<br/>conversion layer<br/>[cast.h]"]
  node_custom_classes["Custom class casters<br/>conversion internals<br/>[type_caster_base.h]"]
  node_argument_resolution["Argument resolution<br/>dispatch internals<br/>[argument_vector.h]"]
  node_feature_casters["STL, chrono, and Eigen<br/>feature casters<br/>[stl.h]"]
  node_numpy_buffers["NumPy and buffers<br/>array interop<br/>[numpy.h]"]
  node_class_lifetime["Class lifetime and inheritance<br/>class internals<br/>[class.h]"]
  node_smart_holder["Smart-holder ownership<br/>ownership model"]
  node_interpreter_state["Interpreter registries<br/>runtime internals<br/>[internals.h]"]
  node_errors_concurrency["Errors and concurrency<br/>correctness boundary<br/>[gil.h]"]
end

subgraph group_integration["Distribution Integration"]
  node_python_package["Python integration package<br/>build package<br/>[__init__.py]"]
  node_cmake_integration["CMake integration<br/>downstream build API"]
  node_package_release["Package and install metadata<br/>distribution config<br/>[pyproject.toml]"]
end

node_cpp_consumer -->|"includes"| node_binding_api
node_cpp_consumer -.->|"uses when embedding"| node_embedding_api
node_binding_api -->|"builds on"| node_object_api
node_binding_api -->|"uses"| node_common_abi
node_binding_api -->|"generates wrappers through"| node_call_dispatch
node_python_runtime -->|"initializes and invokes"| node_binding_api
node_call_dispatch -->|"dispatches registered classes"| node_custom_classes
node_call_dispatch -->|"collects overload arguments"| node_argument_resolution
node_feature_casters -->|"extends"| node_call_dispatch
node_numpy_buffers -->|"extends"| node_call_dispatch
node_binding_api -->|"registers class bindings"| node_class_lifetime
node_class_lifetime -->|"supports"| node_smart_holder
node_class_lifetime -->|"registers instances in"| node_interpreter_state
node_errors_concurrency -->|"coordinates interpreter access"| node_interpreter_state
node_binding_api -->|"exposes boundary APIs"| node_errors_concurrency
node_cmake_integration -->|"configures downstream builds"| node_cpp_consumer
node_python_package -->|"supports build discovery"| node_cmake_integration
node_package_release -->|"packages"| node_python_package
node_package_release -->|"ships"| node_cmake_integration

click node_binding_api "https://github.com/pybind/pybind11/blob/master/include/pybind11/pybind11.h"
click node_embedding_api "https://github.com/pybind/pybind11/blob/master/include/pybind11/embed.h"
click node_object_api "https://github.com/pybind/pybind11/blob/master/include/pybind11/pytypes.h"
click node_common_abi "https://github.com/pybind/pybind11/blob/master/include/pybind11/common.h"
click node_call_dispatch "https://github.com/pybind/pybind11/blob/master/include/pybind11/cast.h"
click node_custom_classes "https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/type_caster_base.h"
click node_argument_resolution "https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/argument_vector.h"
click node_feature_casters "https://github.com/pybind/pybind11/blob/master/include/pybind11/stl.h"
click node_numpy_buffers "https://github.com/pybind/pybind11/blob/master/include/pybind11/numpy.h"
click node_class_lifetime "https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/class.h"
click node_smart_holder "https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/struct_smart_holder.h"
click node_interpreter_state "https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/internals.h"
click node_errors_concurrency "https://github.com/pybind/pybind11/blob/master/include/pybind11/gil.h"
click node_python_package "https://github.com/pybind/pybind11/blob/master/pybind11/__init__.py"
click node_cmake_integration "https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in"
click node_package_release "https://github.com/pybind/pybind11/blob/master/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cpp_consumer,node_python_runtime,node_binding_api,node_embedding_api toneBlue
class node_object_api,node_common_abi,node_call_dispatch,node_custom_classes,node_argument_resolution,node_feature_casters,node_numpy_buffers,node_class_lifetime,node_smart_holder,node_interpreter_state,node_errors_concurrency toneAmber
class node_python_package,node_cmake_integration,node_package_release toneMint
```

## Components

### Consumer Interfaces

- **C++ binding code**: consumer source · Downstream code defines bindings and native implementations.
- **Python runtimes**: runtime · CPython, PyPy, or GraalPy loads generated extensions.
- [Binding declarations](https://github.com/pybind/pybind11/blob/master/include/pybind11/pybind11.h): public header · Modules, functions, classes, enums, and attributes. · `include/pybind11/pybind11.h`
- [Embedding API](https://github.com/pybind/pybind11/blob/master/include/pybind11/embed.h): public header · `include/pybind11/embed.h`

### Header-Only Binding Core

- [Python object wrappers](https://github.com/pybind/pybind11/blob/master/include/pybind11/pytypes.h): core header · `include/pybind11/pytypes.h`
- [Shared API and ABI](https://github.com/pybind/pybind11/blob/master/include/pybind11/common.h): core header · `include/pybind11/common.h`
- [Caster and overload dispatch](https://github.com/pybind/pybind11/blob/master/include/pybind11/cast.h): conversion layer · Marshals Python arguments and native return values. · `include/pybind11/cast.h`
- [Custom class casters](https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/type_caster_base.h): conversion internals · `include/pybind11/detail/type_caster_base.h`
- [Argument resolution](https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/argument_vector.h): dispatch internals · `include/pybind11/detail/argument_vector.h`
- [STL, chrono, and Eigen](https://github.com/pybind/pybind11/blob/master/include/pybind11/stl.h): feature casters · Optional native-type conversion extensions. · `include/pybind11/stl.h`
- [NumPy and buffers](https://github.com/pybind/pybind11/blob/master/include/pybind11/numpy.h): array interop · NumPy arrays and buffer protocol exchange. · `include/pybind11/numpy.h`
- [Class lifetime and inheritance](https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/class.h): class internals · Instance registration, holders, trampolines, and virtual dispatch. · `include/pybind11/detail/class.h`
- [Smart-holder ownership](https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/struct_smart_holder.h): ownership model · `include/pybind11/detail/struct_smart_holder.h`
- [Interpreter registries](https://github.com/pybind/pybind11/blob/master/include/pybind11/detail/internals.h): runtime internals · Per-interpreter state and Python type metadata. · `include/pybind11/detail/internals.h`
- [Errors and concurrency](https://github.com/pybind/pybind11/blob/master/include/pybind11/gil.h): correctness boundary · Exception translation, GIL, critical sections, and subinterpreters. · `include/pybind11/gil.h`

### Distribution Integration

- [Python integration package](https://github.com/pybind/pybind11/blob/master/pybind11/__init__.py): build package · Exposes discovery, CLI, and setup helpers. · `pybind11/__init__.py`
- [CMake integration](https://github.com/pybind/pybind11/blob/master/tools/pybind11Config.cmake.in): downstream build API · Exports targets and helper commands for consumers. · `tools/pybind11Config.cmake.in`
- [Package and install metadata](https://github.com/pybind/pybind11/blob/master/pyproject.toml): distribution config · Ships headers and CMake integration. · `pyproject.toml`

## Connections

- C++ binding code → Binding declarations: includes
- C++ binding code → Embedding API: uses when embedding
- Binding declarations → Python object wrappers: builds on
- Binding declarations → Shared API and ABI: uses
- Binding declarations → Caster and overload dispatch: generates wrappers through
- Python runtimes → Binding declarations: initializes and invokes
- Caster and overload dispatch → Custom class casters: dispatches registered classes
- Caster and overload dispatch → Argument resolution: collects overload arguments
- STL, chrono, and Eigen → Caster and overload dispatch: extends
- NumPy and buffers → Caster and overload dispatch: extends
- Binding declarations → Class lifetime and inheritance: registers class bindings
- Class lifetime and inheritance → Smart-holder ownership: supports
- Class lifetime and inheritance → Interpreter registries: registers instances in
- Errors and concurrency → Interpreter registries: coordinates interpreter access
- Binding declarations → Errors and concurrency: exposes boundary APIs
- CMake integration → C++ binding code: configures downstream builds
- Python integration package → CMake integration: supports build discovery
- Package and install metadata → Python integration package: packages
- Package and install metadata → CMake integration: ships
