Skip to main content
A lot of data exists as structured hierarchies, such as a taxonomies, filesystem hierarchies, website structures, codebases, org charts, biological ontologies, LLM skills, moderation policies, etc. The goal of Hierarchical Classification is to traverse the hierarchy to the correct leaf node, which is the final classification. This is a perfect fit for typesafe’s Choice primitive. We find the most probable leaf by classifying the document at each node (starting at the root), and then iteratively proceeding to the next most-probable node until we end at a leaf (Greedy Search). Additionally, we can also take advantage of the parallel nature of the API by exploring multiple paths with parallel questions using Beam Search to improve performance. In this cookbook, each TypeSafe API call simultaneously evaluates K paths of the hierarchy. Beam search keeps the best K paths by a geometric-mean edge probability: product(edge_probabilities) ** (1 / decisions), and prunes the rest. The probability is length-normalized so that shallow and deep leaves are compared fairly. We note that this is an example of a structural decomposition of a problem, and there are many non-trivial benefits such as:
  • Observability
    • identify which nodes your misclassifications occur most in
    • measure how often nodes and edges are traversed
  • Testability
    • unit test and measure the impact of hierarchy updates on classification performance
  • this is the way

Hierarchies used in this cookbook

  • CPC 2026.05: patent subject matter, from broad technology sections to narrow inventions.
  • Shopify 2026-02: retail product categories, from store departments to specific product types.
  • MeSH 2026: biomedical subjects from broad domains to specific conditions. MeSH is a DAG, so one descriptor can appear under multiple parents; this demo expands its official tree-number paths.
  • CookSafe files: TypeSafe’s cookbook repository hierarchy, searched from folders to source files.

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. Deeper evidence can repair an ambiguous early decision. The leaf of the path with the highest geometric-mean probability is the final classification.
  • TypeSafe Choice: every node is a Choice whose full probability distribution is its edges. Each path of the beam runs as parallel questions, so extra exploration adds little wall-clock latency.
  • Formula:
    • path_score = product(edge_probabilities) ** (1 / decisions)
      • used for pruning and comparing paths
    • separation = top_path_score / second_path_score
      • useful metric, but not used for pruning
      • the ratio compares the top path’s geometric mean against its nearest rival.
        • Near is ambiguous
        • A large ratio means clear separation.
  • Notes on metrics:
    • a different metric such as min(top_prob/second_top_prob) which would optimize for paths that have very clear decisions at every node.
    • use exp(mean(log(probs))) instead of product(edge_probabilities) ** (1 / decisions) to avoid precision errors for hierarchies that are very deep (eg >10 layers)

Load and visualize the example hierarchies

These helpers download pinned taxonomy sources, parse them into direct-child trees, and render each search traversal as a static SVG.
The next section turns each sibling set into one Choice, implements both traversal strategies, and keeps the probabilities needed for the static diagrams.

Compare the methods

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

Results

Each example has a known expected leaf. Beam search matched 4 of 4 expected leaves; greedy search matched 2 of 4. Keeping three paths recovered the expected classification for CPC patents, Shopify products. The diagrams show why the methods differ. Orange marks the greedy route, green marks the winning beam route, purple marks other retained paths, and dashed edges were pruned.

CPC patents

Shopify products

MeSH biomedical subjects

CookSafe files