Skip to main content

Production-grade DSPy library for explainable HS code classification.

Project description

DSPy Nomenclator

DSPy Nomenclator is a Python library for explainable Harmonized System (HS) code classification built on DSPy.

It combines deterministic retrieval over the official HS nomenclature with structured LLM reasoning to produce accurate, transparent, and legally grounded classifications.

The library uses a multi-stage retrieval and reasoning pipeline to progressively narrow the search spaceโ€”from product analysis, through chapter and heading retrieval, to final HS code classification with supporting rationale.

Features

  • ๐ŸŒณ Parses the official HS 2022 nomenclature into a structured tree
  • ๐Ÿ”Ž Two-stage hybrid retrieval over chapters and headings (semantic + BM25)
  • ๐Ÿค– Multi-stage DSPy pipeline for product analysis, research, and classification
  • โš–๏ธ Applies chapter notes and the General Rules for Interpretation (GIR) during classification
  • ๐Ÿ“Š Built-in benchmarking framework for evaluating classification quality
  • ๐Ÿงฉ Modular architecture with typed intermediate models and independently testable components

Installation

pip install dspy-nomenclator

Configure an OpenAI-compatible API key before running DSPy.

For example:

export OPENAI_API_KEY=...

Quick Start

Install the package and classify a product directly from the command line:

nomenclator "Men's cotton knitted shirts"

Example output:

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Nomenclator (openai/gpt-4.1-mini)                                      โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Product
  Men's cotton knitted shirts

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Best Classificationโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ HS Code       6105.10                                                  โ”‚
โ”‚ Confidence    1.00                                                     โ”‚
โ”‚ Description   Men's or boys' shirts, knitted or crocheted โ€” Of cotton  โ”‚
โ”‚ Chapter       6101-2022E                                               โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Reasoning
โ”œโ”€โ”€ Product is a knitted cotton shirt for men, matching heading 61.05 and subheading 6105.10 criteria.
โ”œโ”€โ”€ Chapter 61 notes specify applicability to knitted or crocheted garments, confirming the correct chapter.
โ”œโ”€โ”€ Heading 61.05 is the most specific heading for men's knitted shirts, consistent with GIR 3(a).
โ”œโ”€โ”€ Other headings, including those for woven shirts or women's garments, do not apply.
โ””โ”€โ”€ No competing heading provides a more specific classification.

โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Performance โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Prompt tokens          18,053                              โ”‚
โ”‚ Completion tokens      648                                 โ”‚
โ”‚ Total tokens           18,701                              โ”‚
โ”‚ Estimated cost        $0.00826                             โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

The CLI also accepts input from standard input:

echo "Fresh bananas" | nomenclator

Run nomenclator --help to see all available options.


How it works

Q: "Men's cotton knitted shirts"
        โ”‚
        โ–ผ
โ”Œโ”€ Product Analyst โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  normalized: men's cotton knitted shirts                       โ”‚
โ”‚  category:   textile apparel                                   โ”‚
โ”‚  attrs:      type=shirt ยท material=cotton ยท knit               โ”‚
โ”‚  keywords:                                                     โ”‚
โ”‚    โ€ข cotton shirt                                              โ”‚
โ”‚    โ€ข knitted shirt                                             โ”‚
โ”‚    โ€ข knitted apparel                                           โ”‚
โ”‚    โ€ข textile apparel                                           โ”‚
โ”‚    โ€ข garments                                                  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚
                             โ–ผ
โ”Œโ”€ Chapter Retriever (1st retrieval) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  hybrid search over HS chapters                                โ”‚
โ”‚                                                                โ”‚
โ”‚    Ch.61  Articles of apparel, knitted or crocheted            โ”‚
โ”‚    Ch.62  Articles of apparel, not knitted or crocheted        โ”‚
โ”‚    โ€ฆ                                                           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚
                             โ–ผ
โ”Œโ”€ Research Analyst โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  analyze candidate chapters                                    โ”‚
โ”‚                                                                โ”‚
โ”‚    1. Ch.61  Primary pathway                                   โ”‚
โ”‚    2. Ch.62  Alternative pathway                               โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚
                             โ–ผ
โ”Œโ”€ Heading Retriever (2nd retrieval) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  hybrid search over heading chunks                             โ”‚
โ”‚                                                                โ”‚
โ”‚    Ch.61  61.05 โ†’ 6105.10                                      โ”‚
โ”‚    Ch.62  62.05 โ†’ 6205.20                                      โ”‚
โ”‚    chapter notes + heading context                             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚
                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                 โ”‚  GIR Rules (fixed)    โ”‚
                 โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ–ผ
โ”Œโ”€ Classification Analyst โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  rank HS classification candidates                             โ”‚
โ”‚                                                                โ”‚
โ”‚  1. 6105.10  Men's or boys' shirts, knitted, of cotton         โ”‚
โ”‚     score 1.00                                                 โ”‚
โ”‚                                                                โ”‚
โ”‚  2. 6205.20  Men's or boys' shirts, of cotton                  โ”‚
โ”‚     score 0.30                                                 โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                             โ”‚
                             โ–ผ
โ”Œโ”€ Classification Result โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  โœ“ Selected: 6105.10                                           โ”‚
โ”‚                                                                โ”‚
โ”‚  Confidence: High                                              โ”‚
โ”‚  Alternatives: 6205.20                                         โ”‚
โ”‚  Reasoning:                                                    โ”‚
โ”‚    โ€ข Product is a knitted cotton shirt.                        โ”‚
โ”‚    โ€ข Chapter 61 is more specific than Chapter 62.              โ”‚
โ”‚    โ€ข GIR 3(a) favors the most specific heading.                โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Development

Install the project dependencies:

poetry install

Optionally, install Poe the Poet globally for shorter commands:

pipx install poethepoet

Common development tasks:

poe format       # Format and auto-fix the source code
poe lint         # Run static analysis
poe typecheck    # Run type checking
poe test         # Execute the unit test suite
poe integration  # Execute the integration test suite
poe check        # Run linting, type checking, and unit tests

Coding agents working in this repository follow the rules described in AGENTS.md

Architecture

For a detailed overview of the system design, pipeline flow, retrieval strategy, and agent responsibilities, see:

Roadmap

Completed:

  • โœ… Two-stage hybrid retrieval (chapter and heading levels)
  • โœ… Multiple embedding backends (Sentence Transformers and FastEmbed)
  • โœ… Classification benchmarking and evaluation framework

Planned improvements:

  • DSPy optimization using evaluation datasets
  • Interactive terminal UI (TUI) for product classification and result exploration
  • Optional audit stage for reviewing classification results
  • Support for additional customs nomenclatures (e.g. TARIC, HTSUS)

License

MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dspy_nomenclator-0.1.0.tar.gz (36.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dspy_nomenclator-0.1.0-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

Details for the file dspy_nomenclator-0.1.0.tar.gz.

File metadata

  • Download URL: dspy_nomenclator-0.1.0.tar.gz
  • Upload date:
  • Size: 36.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for dspy_nomenclator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 712f5e54a0e7dfa61c20e90c4bd0bc86497f5f541a2d593564b4a7cbc834d452
MD5 3f89c2c667fa8d628e3a363f76cac3e0
BLAKE2b-256 532c76e9a0350af381aa1facb16791a137675b129d2135c21fe3bed12df890c9

See more details on using hashes here.

Provenance

The following attestation bundles were made for dspy_nomenclator-0.1.0.tar.gz:

Publisher: ci.yaml on talmago/nomenclator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dspy_nomenclator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dspy_nomenclator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 022193575e8b4c957c0e1aa165b44e9bfc3822d7f64ee1889196a476254a5986
MD5 76839cfec9e384de14280cc35600424d
BLAKE2b-256 c3e51061991faa45e8ed8cf3f50dcaeb97d0db79dbcc8ca281aee489a6d308ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for dspy_nomenclator-0.1.0-py3-none-any.whl:

Publisher: ci.yaml on talmago/nomenclator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page