# HDF5 bindings in Mojo!

**URL:** https://forum.modular.com/t/hdf5-bindings-in-mojo/2910
**Category:** Community Showcase
**Created:** [March 31, 2026, 5:18pm UTC](https://forum.modular.com/t/hdf5-bindings-in-mojo/2910 "2026-03-31T17:18:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Photon](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@Photon](https://forum.modular.com/u/Photon)
#### Post date: [March 31, 2026, 5:18pm UTC](https://forum.modular.com/t/hdf5-bindings-in-mojo/2910/1 "2026-03-31T17:18:54Z")

</div>

**[hdf5-mojo](https://github.com/shivasankarka/hdf5-mojo): High level HDF5 bindings for Mojo (first release) 🎉**

I’ve been working on porting a lot of my scientific stack and well-known computational & physics libraries to Mojo (look forward to some more library spam from me xD), and HDF5 is a core dependency in that workflow, so this happened 🙂

It’s honestly been really cool to see how much Mojo has evolved over the past ~2 years. Early on, porting scientific libraries was… painful 😅. Now it’s _almost_ smooth sailing, with a few hiccups here and there. Super excited for what’s coming next!!!

This is an early release of **[hdf5-mojo](https://github.com/shivasankarka/hdf5-mojo)**, a set of high-level bindings to the HDF5 C library with a Mojo-friendly API. It’s usable today, but still evolving.

* * *

### What this is right now

A simple two-layer design:

- **Low-level (`hdf5/bindings.mojo`)**  
Thin FFI wrapper over the HDF5 C API (`HDF5Lib`) for cases where you need full control.

- **High-level (`hdf5/api.mojo`)**  
Ergonomic interface (`H5File`, `NDArray`) for most use cases.

In practice, you’ll almost always use the high-level API.

* * *

### What works now

- Read 1D / 2D datasets without knowing shapes ahead of time
- Read scalar attributes from groups/datasets
- Create files and write datasets (row-major)
- Safe group creation via `require_group`
- Automatic HDF5 library discovery via `$CONDA_PREFIX` (pixi-friendly)

* * *

### ✨ Example

Check out the [examples](https://github.com/shivasankarka/hdf5-mojo/tree/main/examples) for a more complete walkthrough with sample data.

```mojo
from hdf5 import H5File

def main() raises:
    var f = H5File("data.h5")

    # Read scalar attributes
    var emin = f.read_scalar_attr[DType.float64]("/group", "min_energy")
    var nnodes = f.read_scalar_attr[DType.int32]("/group", "number_energy_nodes")

    # Read 1-D and 2-D datasets (sizes discovered automatically)
    var xs = f.read_1d[DType.float64]("/group/dataset")
    var mat = f.read_2d[DType.float64]("/group/matrix")

    print(xs[0], mat[0, 0])

    xs.free()
    mat.free()
    f.close()
```

---

<div class="post-metadata">

### Author: ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)
#### Post date: [March 31, 2026, 5:29pm UTC](https://forum.modular.com/t/hdf5-bindings-in-mojo/2910/2 "2026-03-31T17:29:31Z")

</div>

Wow, this is really cool! I’m thrilled to see integration into the HDF world. I suspect Mojo is really useful to many folks in the space. Do you have an idea of what a cool demo built on top of HDF support could be?

---

<div class="post-metadata">

### Author: ![Photon](https://avatars.discourse-cdn.com/v4/letter/p/b5a626/32.png) [@Photon](https://forum.modular.com/u/Photon)
#### Post date: [March 31, 2026, 5:54pm UTC](https://forum.modular.com/t/hdf5-bindings-in-mojo/2910/3 "2026-03-31T17:54:20Z")

</div>

Thanks! Really appreciate it! Mojo has made this a lot more practical than it used to be.

I’m mainly using HDF5 right now to port some particle physics simulation libraries and other scientific libraries (I will be posting updates on this soon! some cool gsl stuff!) that depend heavily on it. On top of that, we just added some foundational layers for GPU support in NuMojo! So I think a pretty compelling demo would be:

- loading HDF5 datasets directly in Mojo.
- reading them straight into a NuMojo `NDArray` buffer
- running processing on CPU/GPU seamlessly, and writing results back to HDF5.

HDF5 still has to materialize data from disk, but since it reads into a user-provided buffer, we can skip intermediate representations and write directly into our array memory. That keeps the pipeline pretty tight after I/O. Another direction I’m interested in is streaming large datasets (very common in particle physics) in chunks and applying GPU ops on the fly.

Still exploring, but definitely feels like there’s some fun stuff to build here!

---

<div class="post-metadata">

### Author: ![Curious](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/curious/32/955_2.png) [@Curious](https://forum.modular.com/u/Curious)
#### Post date: [April 1, 2026, 10:04pm UTC](https://forum.modular.com/t/hdf5-bindings-in-mojo/2910/4 "2026-04-01T22:04:55Z")

</div>

Ideally one day we would have an xarray like API so that [calculations like mine](https://github.com/itsgifnotjiff/tke-mojo/tree/main/tke-mojo) written in Mojo could take advantage of parallel IO as well.
