Skip to main content
Many kinds of data are hierarchies: taxonomies, file systems, websites, codebases, org charts, biological ontologies, LLM skills and moderation policies. Hierarchical classification walks a hierarchy from the root to the correct leaf node; that leaf is the final classification. We start at the root, ask a System One Choice question over its children, follow the most probable child, and repeat until we reach a leaf (Greedy Search). Because System One questions can run in parallel, we can also follow several paths at once with Beam Search, so one wrong early choice doesn’t decide the result. At each level, beam search scores every path by the geometric mean of its edge probabilities, product(edge_probabilities) ** (1 / decisions), keeps the best K, and prunes the rest. The geometric mean, unlike the raw product, doesn’t penalize a deep leaf just for taking more decisions. Asking one question per node also helps you observe and test the classifier:
  • Observability
    • identify which nodes your misclassifications occur most in
    • measure the number of times each node and edge is traversed
  • Testability
    • unit test and measure the impact of hierarchy updates on classification performance

Hierarchies used in this cookbook

  • CPC 2026.05: patent subject matter, from broad technology sections to narrow inventions. Published by the European Patent Office and the US Patent and Trademark Office.
  • Shopify 2026-02: retail product categories, from store departments to specific product types. From Shopify’s Product Taxonomy (MIT).
  • MeSH 2026: biomedical subjects from broad domains to specific conditions. MeSH is a DAG, so one descriptor can appear under several parents; we give each of its official tree numbers a separate path. Source: U.S. National Library of Medicine. This is the January 2026 build of MeSH 2026 and may not be the most current MeSH data.
Attribution and licences for the three are in DATA_LICENSE.md.

Methods

  • Greedy search: choose the highest-probability child and discard every alternative. One early mistake cannot be recovered.
  • Beam search: retain K plausible paths and classify every frontier in parallel. When an early choice is close, a clearer decision further down can put the right path back on top. The leaf of the path with the highest geometric-mean probability is the final classification.
  • System One Choice questions: each node asks one Choice question over its children, and the answer’s probabilities become the edge probabilities. The beam’s K questions at each level run in parallel, so beam search takes one round of calls per level, as greedy search does.
  • Formula:
    • path_score = product(edge_probabilities) ** (1 / decisions)
      • used for pruning and comparing paths
      • for hierarchies deeper than about 10 levels, compute it as exp(mean(log(probs))) to avoid floating-point precision loss
    • separation = top_path_score / second_path_score
      • reported only, not used for pruning
      • near 1×, the top two paths are almost tied; the further above 1×, the clearer the winner

Setup

Run in Colab

Runs as published without an API key.

Python file

This page as one .py file, to run locally.
Requires Python 3.10 or later.
With uv, uv run taxonomy_beam_search.py installs the dependencies for you. The code replays saved answers, so it runs as published without an API key. To change a request, set TYPESAFE_API_KEY; in Colab, add it in the Secrets panel. The first run downloads the three hierarchies (about 28 MB) into datasets/. Numbers below came from jev-1.14.0 on 2026-11-03.

Load and visualize the example hierarchies

We download a pinned version of each taxonomy, turn it into a tree of direct children, and draw each search as a static SVG.
Each node’s children become one Choice question. Both strategies record every distribution they receive, so the diagrams can label each edge with its probability.

Compare the methods

Run both strategies on three labeled examples, compare their leaves against the expected classifications, and visualize the routes they explored.

Results

Beam search matched 3 of 3 expected leaves; greedy search matched 2 of 3. Keeping three paths recovered the expected classification for Shopify products. The diagrams show why the methods differ.

CPC patents

Shopify products

MeSH biomedical subjects