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:
- ๐ Architecture
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
712f5e54a0e7dfa61c20e90c4bd0bc86497f5f541a2d593564b4a7cbc834d452
|
|
| MD5 |
3f89c2c667fa8d628e3a363f76cac3e0
|
|
| BLAKE2b-256 |
532c76e9a0350af381aa1facb16791a137675b129d2135c21fe3bed12df890c9
|
Provenance
The following attestation bundles were made for dspy_nomenclator-0.1.0.tar.gz:
Publisher:
ci.yaml on talmago/nomenclator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dspy_nomenclator-0.1.0.tar.gz -
Subject digest:
712f5e54a0e7dfa61c20e90c4bd0bc86497f5f541a2d593564b4a7cbc834d452 - Sigstore transparency entry: 2206270596
- Sigstore integration time:
-
Permalink:
talmago/nomenclator@238be6b502dc603f17d3c6cfca9ff5835082f403 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/talmago
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@238be6b502dc603f17d3c6cfca9ff5835082f403 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dspy_nomenclator-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dspy_nomenclator-0.1.0-py3-none-any.whl
- Upload date:
- Size: 43.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
022193575e8b4c957c0e1aa165b44e9bfc3822d7f64ee1889196a476254a5986
|
|
| MD5 |
76839cfec9e384de14280cc35600424d
|
|
| BLAKE2b-256 |
c3e51061991faa45e8ed8cf3f50dcaeb97d0db79dbcc8ca281aee489a6d308ed
|
Provenance
The following attestation bundles were made for dspy_nomenclator-0.1.0-py3-none-any.whl:
Publisher:
ci.yaml on talmago/nomenclator
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dspy_nomenclator-0.1.0-py3-none-any.whl -
Subject digest:
022193575e8b4c957c0e1aa165b44e9bfc3822d7f64ee1889196a476254a5986 - Sigstore transparency entry: 2206270627
- Sigstore integration time:
-
Permalink:
talmago/nomenclator@238be6b502dc603f17d3c6cfca9ff5835082f403 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/talmago
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@238be6b502dc603f17d3c6cfca9ff5835082f403 -
Trigger Event:
push
-
Statement type: