optree
Optimized PyTree Utilities.
Decision gist · record as of 2026-08-14
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.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires Python 3.9+; C++ compiler and cmake needed only if building from source rather than using prebuilt wheels.
- 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 · maintenance · safety
Apache-2.0 (permissive) — Apache-2.0 is permissive, allowing commercial and private use with minimal restrictions; attribution and license notice are required.
last release 2026-05-06 (100 days) · last repo commit 2026-08-11 · 214 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 10,193,965 downloads/mo, #1,471 on PyPI
Alternatives
Verify before relying
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)- 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.
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 on it.
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
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.
Requires Python 3.9+; C++ compiler and cmake needed only if building from source rather than using prebuilt wheels.
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)
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 packagetyping-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 |
| Classifiers | 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 |
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
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “pytree flatten unflatten”
- optreeOpTree provides optimized utilities for flattening, unflattening, and…
- json-flattenConverts nested JSON objects into flat key-value dictionaries and…
- flatten-jsonConverts nested JSON objects into flat key-value dictionaries and…
Give your agent the search over MCP, or paste the wish link into any chat.
More Utilities packages
Converts domain names between Unicode and ASCII-compatible encoding (Punycode) according to IDNA 2008 and Unicode Technical Standard 46, with security validation and broader script coverage than the standard library.
Install it if you work with internationalized domain names, need to validate domains, or use HTTP clients that depend on it transitively.
Detects and normalizes text encoding from unknown or ambiguous sources, supporting all IANA character sets that Python's core library provides codecs for, with the ability to register custom codecs.
Setuptools is a Python build backend and package management tool that handles building, distributing, and installing Python packages, including support for C/C++ extension modules.
Pluggy provides a plugin system that lets you define hook specifications and register implementations to be called in sequence, enabling extensible Python applications without tight coupling.
Install it if you're building an extensible application or framework.
Pygments is a syntax highlighter that colorizes source code and text in over 500 languages and formats, outputting to HTML, LaTeX, RTF, SVG, images, or ANSI terminal sequences.
Install it if you need to display or transform source code.
Six provides utility functions to write Python code that runs on both Python 2.7 and Python 3.3+, smoothing over language differences between the two versions.
See also dm-tree · flatten-dict · flatdict · json-tools-rs · llist · nutree · fields · treelib · json-flatten · jax-dataclasses