skillfed

optree

Optimized PyTree Utilities.

optree v0.19.1 10.2M downloads/30d#1,471 on PyPI214
Permissive license Apache-2.0 Active released

What it is and what it does

OpTree is a library for working with PyTrees—arbitrarily nested Python containers and custom objects—by providing efficient flatten, unflatten, and map operations. It treats built-in types like dict, list, tuple, namedtuple, and OrderedDict as non-leaf nodes that can be traversed, while custom objects are leaves unless explicitly registered. The core operations are tree flattening (which extracts leaves in deterministic order and captures structure) and unflattening (which reconstructs the tree from leaves and structure), with tree_map built from these primitives.

The package is written in C++ with Python bindings for performance, supports registration of custom container types, and includes a namespace system (optree.pytree) for creating library-specific PyTree utilities. It is actively maintained, has no known vulnerabilities, and provides prebuilt wheels for modern Python versions across multiple platforms, making installation straightforward in most cases.

Use it for:

  • Flatten nested model parameters or optimizer states in machine learning frameworks for batch processing.
  • Traverse and transform deeply nested configuration dictionaries or JSON-like structures uniformly.
  • Register custom container types (e.g., dataclasses) to participate in tree operations without manual recursion.
  • Build functional programming patterns that operate on entire tree structures with a single map call.
  • Implement library-specific PyTree utilities via the reexport API for downstream packages.

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

OpTree provides optimized utilities for flattening, unflattening, and mapping over nested Python container structures (PyTrees) like dicts, lists, tuples, and custom types.

Yes. OpTree is worth installing if you work with nested Python structures in machine learning, scientific computing, or functional programming contexts. It is actively maintained, has no security issues, and provides significant convenience and performance over manual tree traversal. The permissive Apache-2.0 license poses no restriction. Medium install friction is acceptable given the availability of prebuilt wheels for common platforms.

Install

optree on PyPI

pip

pip install optree

uv

uv add optree

poetry

poetry add optree

Installing optree

Before you install

Medium install friction due to C++ extension compilation from source; however, prebuilt wheels are available for Python 3.9–3.15 across macOS, Linux, and Windows. Active maintenance with recent releases and no known vulnerabilities.

License in practice

Apache-2.0 is permissive, allowing commercial and private use with minimal restrictions; attribution and license notice are required.

Quickstart

pip install optree

import optree

tree = {'b': (2, [3, 4]), 'a': 1}
leaves, spec = optree.tree_flatten(tree)
result = optree.tree_map(lambda x: x**2, tree)

Requires Python 3.9+; C++ compiler and cmake needed only if building from source rather than using prebuilt wheels.

Verify before relying

  • Performance characteristics compared to other PyTree implementations (e.g., JAX's tree_util).
  • Memory overhead of PyTreeSpec objects for very large or deeply nested structures.
  • Compatibility guarantees for custom type registration across OpTree versions.

Package facts

License Apache-2.0 (permissive)
Python support supports the current Python release (>=3.9)
Install friction medium — platform-specific wheel
Runtime dependencies 1 — typing-extensions
Maintenance actively maintained — 100 days since the last release
Last repo commit
First released
Downloads 10,193,965/month — #1,471 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: optree-0.19.1-cp310-cp310-macosx_10_9_x86_64.whl; optree-0.19.1-cp310-cp310-macosx_11_0_arm64.whl; optree-0.19.1-cp310-cp310-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl; optree-0.19.1-cp310-cp310-manylinux_2_26_i686.manylinux_2_28_i686.whl; optree-0.19.1-cp310-cp310-manylinux_2_26_ppc64le.manylinux_2_28_ppc64le.whl; optree-0.19.1-cp310-cp310-manylinux_2_26_s390x.manylinux_2_28_s390x.whl; optree-0.19.1-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl; optree-0.19.1-cp310-cp310-manylinux_2_39_riscv64.whl; optree-0.19.1-cp310-cp310-win32.whl; optree-0.19.1-cp310-cp310-win_amd64.whl; optree-0.19.1-cp311-cp311-macosx_10_9_x86_64.whl; optree-0.19.1-cp311-cp311-macosx_11_0_arm64.whl; optree-0.19.1-cp311-cp311-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl; optree-0.19.1-cp311-cp311-manylinux_2_26_i686.manylinux_2_28_i686.whl; optree-0.19.1-cp311-cp311-manylinux_2_26_ppc64le.manylinux_2_28_ppc64le.whl; optree-0.19.1-cp311-cp311-manylinux_2_26_s390x.manylinux_2_28_s390x.whl; optree-0.19.1-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl; optree-0.19.1-cp311-cp311-manylinux_2_39_riscv64.whl; optree-0.19.1-cp311-cp311-win32.whl; optree-0.19.1-cp311-cp311-win_amd64.whl

Keywords: PyTree, Tree Manipulation, Tree Traversal, Functional Programming

Development Status :: 4 - BetaIntended Audience :: DevelopersIntended Audience :: EducationIntended Audience :: Science/ResearchOperating System :: AndroidOperating System :: MacOSOperating System :: Microsoft :: WindowsOperating System :: POSIX :: LinuxOperating System :: iOSProgramming Language :: C++Programming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.15Programming Language :: Python :: 3.9Programming Language :: Python :: Free Threading :: 4 - ResilientProgramming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Utilities

Tags

pytree flatten unflattennested container traversaltree map functional programmingpython tree utilitiesrecursive structure manipulationpytree registry custom typestree structure specification
tree-traversalfunctional-programmingml-infrastructure

More Utilities packages