# Nanobind throws bad\_cast after dolfinx and nanobind update

**URL:** <https://fenicsproject.discourse.group/t/nanobind-throws-bad-cast-after-dolfinx-and-nanobind-update/18447>\
**Category:** dolfinx\
**Created:** [December 2, 2025, 9:08pm UTC](https://fenicsproject.discourse.group/t/nanobind-throws-bad-cast-after-dolfinx-and-nanobind-update/18447 "2025-12-02T21:08:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![23982f89sdf389w2](https://avatars.discourse-cdn.com/v4/letter/2/258eb7/32.png) [@23982f89sdf389w2](https://fenicsproject.discourse.group/u/23982f89sdf389w2)\
**Post date:** [December 2, 2025, 9:08pm UTC](https://fenicsproject.discourse.group/t/nanobind-throws-bad-cast-after-dolfinx-and-nanobind-update/18447/1 "2025-12-02T21:08:07Z")

</div>

I just updated dolfinx and everything else in my conda environment. They’re now 0.10.0 and 2.9.2, respectively. Previous versions were installed this summer. With them, the following C++ code, applied to the `_cpp_object` inside the Python-side `Function`, used to work:

```c++
    typedef Function<double> Function_f64;

    const Function_f64* cast_Function_f64(const PyObject* obj) {
        auto handle = nanobind::handle(obj);
        return nanobind::cast<const Function_f64*>(handle);
    }

```

Now, after the update, it throws `std::bad_cast`.

Anybody have an idea what has changed? Is there any other way to get the C++ object pointer to use in my code.

The following code, added to the above function, confirms that `obj` is a `Function_float64`.

```c++
        const char* cstr = PyUnicode_AsUTF8(PyType_GetName(obj->ob_type));
        std::cout << cstr << std::endl;

```

I’m using direct `PyObject`s here, because my own bindings are Python ← pyo3 → Rust – cxx → C++, with the main part of my own abstract code written in Rust, and some fenics-extensibility in Python. But I also need some higher performance parts that need the C++ pointer.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [December 3, 2025, 4:43am UTC](https://fenicsproject.discourse.group/t/nanobind-throws-bad-cast-after-dolfinx-and-nanobind-update/18447/2 "2025-12-03T04:43:17Z")

</div>

I would probably ask the nanobind developers at

> **[wjakob/nanobind](https://github.com/wjakob/nanobind/issues?q=is%3Aissue%20state%3Aclosed%20bad_cast)**
>
> nanobind: tiny and efficient C++/Python bindings. Contribute to wjakob/nanobind development by creating an account on GitHub.

as this seems very nanobind specific. Do you have a minimal reproducible example?

---

<div class="post-metadata">

**Author:** ![23982f89sdf389w2](https://avatars.discourse-cdn.com/v4/letter/2/258eb7/32.png) [@23982f89sdf389w2](https://fenicsproject.discourse.group/u/23982f89sdf389w2)\
**Post date:** [December 3, 2025, 3:57pm UTC](https://fenicsproject.discourse.group/t/nanobind-throws-bad-cast-after-dolfinx-and-nanobind-update/18447/3 "2025-12-03T15:57:33Z")

</div>

I’ve posted the question to the nanobind people as well at github/jakob/nanobind/discussions/1234. (Sorry, this doesn’t allow links.)

Downgrading to nanobind 2.8.0 and dolfinx 0.9.0 from conda-forge, the problem disappears. Did something significant happen between those versions, either in dolfinx or nanobind? `._cpp_object` is still the nanobind-generated Python-side container for the C++ `Function<double>`? Did something move in the dolfinx headers? I’ve read that nanobind could be sensitive to the correct things being `#include`d.
