Product Attribute Normalization and Sorting Using DSPy for Large-Scale E-commerce

zoro_uk · E-commerce · 2024 · source Agentic: no · promotional bias: low · source-backed · extracted with prompt 0.5.6 (schema v2)

Generated from extraction.json — do not edit by hand. Every claim cites verbatim source evidence; stated means the quote directly asserts the claim, implied means it supports it indirectly.

Problem

Domain: E-commerce product attribute normalization and sorting

Zoro UK needed to normalize and logically sort product attributes across a massive supplier-driven catalog so customers could search and filter products in meaningful order instead of seeing inconsistent strings and units.

Constraints:

Architecture

Zoro built a DSPy-backed product attribute sorting pipeline. A scheduled job pulls attribute values from MongoDB for a given attribute type and sends them to an NLM Sort Service. The service first uses a smaller model, described as Mistral 8B in the summary, to decide whether normal alphanumeric sorting is enough. If it is, Python performs the sort; if not, the service routes the values to GPT-4 for semantic/logical ordering. DSPy handles prompt and example optimization around task-specific metrics, allowing the team to switch models and compile optimized prompts without manual prompt engineering.

Diagram confidence: explicit (topology described by the source).

flowchart LR
  mongodb-product-catalog["MongoDB product catalog"] --> scheduled-sorting-job["Scheduled sorting job"]
  scheduled-sorting-job["Scheduled sorting job"] --> mongodb-product-catalog["MongoDB product catalog"]
  scheduled-sorting-job["Scheduled sorting job"] --> nlm-sort-service["NLM Sort Service"]
  nlm-sort-service["NLM Sort Service"] --> scheduled-sorting-job["Scheduled sorting job"]
  nlm-sort-service["NLM Sort Service"] --> mistral-classifier["Mistral classifier"]
  mistral-classifier["Mistral classifier"] --> nlm-sort-service["NLM Sort Service"]
  nlm-sort-service["NLM Sort Service"] --> python-alphanumeric-sorter["Python alphanumeric sorter"]
  python-alphanumeric-sorter["Python alphanumeric sorter"] --> nlm-sort-service["NLM Sort Service"]
  python-alphanumeric-sorter["Python alphanumeric sorter"] --> mongodb-product-catalog["MongoDB product catalog"]
  mongodb-product-catalog["MongoDB product catalog"] --> python-alphanumeric-sorter["Python alphanumeric sorter"]
  nlm-sort-service["NLM Sort Service"] --> gpt-4-semantic-sorter["GPT-4 semantic sorter"]
  gpt-4-semantic-sorter["GPT-4 semantic sorter"] --> nlm-sort-service["NLM Sort Service"]
  gpt-4-semantic-sorter["GPT-4 semantic sorter"] --> mongodb-product-catalog["MongoDB product catalog"]
  mongodb-product-catalog["MongoDB product catalog"] --> gpt-4-semantic-sorter["GPT-4 semantic sorter"]
  dspy-optimizer["DSPy optimizer"] --> mistral-classifier["Mistral classifier"]
  mistral-classifier["Mistral classifier"] --> dspy-optimizer["DSPy optimizer"]
  dspy-optimizer["DSPy optimizer"] --> gpt-4-semantic-sorter["GPT-4 semantic sorter"]
  gpt-4-semantic-sorter["GPT-4 semantic sorter"] --> dspy-optimizer["DSPy optimizer"]
  mongodb-product-catalog["MongoDB product catalog"] --> sorted-search-experience["Sorted search experience"]
  sorted-search-experience["Sorted search experience"] --> mongodb-product-catalog["MongoDB product catalog"]
Drag to pan · scroll or use +/− to zoom · reconstructed from the source's described component topology

Components

Component Role Interacts with
MongoDB product catalog Primary database holding product information and attributes across the catalog. Scheduled sorting job, Sorted search experience
Scheduled sorting job Periodic job that extracts all values for a particular attribute type from the catalog. MongoDB product catalog, NLM Sort Service
NLM Sort Service Application service that decides whether attributes need model-based semantic sorting or ordinary alphanumeric sorting. Scheduled sorting job, Mistral classifier, Python alphanumeric sorter, GPT-4 semantic sorter
Mistral classifier Smaller model used for first-pass classification of whether an attribute can be sorted alphanumerically. NLM Sort Service
Python alphanumeric sorter Deterministic sorter used when the model judges ordinary alphanumeric sorting sufficient. NLM Sort Service, MongoDB product catalog
GPT-4 semantic sorter More capable model used when attribute values require semantic or logical ordering. NLM Sort Service, MongoDB product catalog
DSPy optimizer Framework layer for defining task, examples, metrics, and prompt/model optimization. Mistral classifier, GPT-4 semantic sorter
Sorted search experience Customer-facing product search and filtering interface that consumes normalized, logically sorted attributes. MongoDB product catalog

Data flow

Product and attribute data live in MongoDB. A scheduled sorting job gathers all values for a particular attribute, such as function length, across products. The NLM Sort Service classifies whether the values can be handled by alphanumeric sorting. Straightforward numeric/unit cases go through Python sorting, while semantically ordered values such as clothing sizes are routed to GPT-4. The sorted values are written back to MongoDB and served to the e-commerce search/filter experience.

3 supporting quotes
“our primary DB is uh from there we actually have an uh sorting job uh which takes all the attributes from various different products together”transcript, stated
“we first go to a smaller model uh like mistal and we first try to decide whether there is just an alpha numeric sort or not”transcript, stated
“once we do this uh whole um you know sorting we put it back into mongodb and then serve it from there”transcript, stated

Why this architecture

The architecture separates cheap classification and deterministic sorting from expensive semantic sorting because most catalog values should not require GPT-4. DSPy was used because it lets the team define metrics and examples, compile optimized prompts, and switch models without hand-maintaining long prompts.

3 supporting quotes
“I don't actually have to write long prompts first thing I don't have to do any kind of uh prompt optimization uh um like outside like uh through through hand right”transcript, stated
“also the other biggest Advantage I saw was that it is llm agnostic”transcript, stated
“if it is an alpha numeric sort then we can just do an python based uh you know alpha numeric sort as well”transcript, stated

Models used: Mistral 8B · GPT-4 · DSPy MIPRO optimizer

Scale

Alternatives considered (roads not taken)

Manual product attribute normalization

Rejected because: The supplier and attribute scale made manual normalization humanly impractical.

“there is no way for us to actually make sense of it or like normalize it uh or humanly it is uh very very hard to uh normalize this”transcript, stated

Hand-written long prompt engineering

Rejected because: DSPy automatically optimizes prompts and examples around metrics, removing the need to maintain long prompts manually.

“I don't actually have to write long prompts first thing I don't have to do any kind of uh prompt optimization uh um like outside like uh through through hand right”transcript, stated

Use one model for every case

Rejected because: The team used a smaller model first and routed only semantic sorting cases to GPT-4, keeping simpler cases deterministic or cheaper.

“we first go to a smaller model uh like mistal and we first try to decide whether there is just an alpha numeric sort or not”transcript, stated
“at that time you go back to the Sorting service and then you go uh route it back to uh open AI”transcript, stated

Always semantic-sort with GPT-4

Rejected because: When alphanumeric sorting is enough, Python can sort without a model.

“if it is an alpha numeric sort then we can just do an python based uh you know alpha numeric sort as well”transcript, stated

Patterns

model-routing (architecture)

The sort service routes attributes through a smaller model for classification and only sends semantic cases to GPT-4.

“we first go to a smaller model uh like mistal and we first try to decide whether there is just an alpha numeric sort or not”transcript, stated
“then you go uh route it back to uh open AI open AI is going to now make uh a decision that you know this is how the Sorting needs to be”transcript, stated

cost-aware-routing (operations)

The architecture reserves GPT-4 for cases that need semantic reasoning and uses Python or a smaller model where sufficient.

“Tier 2 - Semantic Sorting with GPT-4: When Mistral 8B determines that alpha-numeric sorting is insufficient, the request is routed to OpenAI's GPT-4”summary, stated
“This two-tier approach optimizes costs by reserving the more expensive and capable GPT-4 for only the cases that require its advanced reasoning capabilities.”summary, stated

eval-driven-development (quality)

DSPy development centers on defining metrics, running baseline evaluation, compiling an optimizer, and iterating with better metrics and examples.

“the most important part is going to be the metric”transcript, stated
“finally you keep iterating on this with better metrics or better examples Etc”transcript, stated

prompt-versioning (prompt-operations)

DSPy compiles and optimizes prompt/example choices instead of relying on manual prompt editing.

“my Pro which is um where DSP actually optimizes both the number of examples that it's going to give to um the llm and also the prompt”transcript, stated
“compiling your Optimizer for uh the task that you're trying to do”transcript, stated

structured-output-contract (data-quality)

The system turns messy supplier attribute strings into a normalized sorted order that can be stored and served consistently.

“some of them may add 25.4 mm all in the same string uh some will have it as 25.4 and then mm in a different you know unit kind of things”transcript, stated
“once we do this uh whole um you know sorting we put it back into mongodb and then serve it from there”transcript, stated

data-quality-loop (catalog-operations)

The pipeline repeatedly pulls product attribute values, normalizes/sorts them, writes them back, and improves the search/filter experience.

“A scheduled sorting job periodically extracts all attribute values for a particular attribute type (like "function length") across the relevant products, aggregating similar attributes together.”summary, stated
“proper attribute normalization and sorting directly affects product discoverability.”summary, stated

Tradeoffs

Use a two-tier model pipeline for sorting.

Chose Small-model classification plus GPT-4 only for semantic sorting over Sending every attribute directly to GPT-4.

Rationale: The smaller model can identify easy alphanumeric cases and reserve expensive semantic reasoning for values that need it.

“we first go to a smaller model uh like mistal and we first try to decide whether there is just an alpha numeric sort or not”transcript, stated
“Tier 2 - Semantic Sorting with GPT-4: When Mistral 8B determines that alpha-numeric sorting is insufficient, the request is routed to OpenAI's GPT-4”summary, stated

Use deterministic Python sorting when alphanumeric order is sufficient.

Chose Python-based alphanumeric sorting over Model-based sorting for every attribute.

Rationale: Ordinary deterministic sorting is enough for simple normalized numeric or string cases.

“if it is an alpha numeric sort then we can just do an python based uh you know alpha numeric sort as well”transcript, stated

Adopt DSPy to optimize prompts programmatically.

Chose Metric-driven DSPy compilation over Manual long-prompt tuning.

Rationale: DSPy let the team optimize prompts/examples, remain LLM-agnostic, and switch from one model to another with recompilation.

“it automatically does the prompt optimization for me”transcript, stated
“we first started with uh Mell 8p uh and then uh for some use cases uh that was not enough so we just switched it over to open uh AI GPD 4”transcript, stated

Limit optimization iterations for the learning example.

Chose A small 10-iteration MIPRO run over A much larger optimization search.

Rationale: The prompt optimization calls the LLM repeatedly, so cost limited experimentation.

“I didn't try this with like a huge number of uh llm uh evaluations because the prompt itself goes to an llm”transcript, stated
“I just gave it like 10 iterations or so”transcript, stated

Gotchas (what bit them)

Supplier data represented the same attribute in incompatible formats.

Root cause: Hundreds of suppliers encoded dimensions and units differently, so identical concepts appeared as mixed strings, split values, or imperial units.

Resolution: The sorting pipeline aggregates each attribute type and normalizes/sorts values before serving them back to search.

“some of them may add 25.4 mm all in the same string uh some will have it as 25.4 and then mm in a different you know unit kind of things”transcript, stated
“there is no way for us to actually make sense of it or like normalize it uh or humanly it is uh very very hard to uh normalize this”transcript, stated

A metric mistake would undermine the whole DSPy optimization.

Root cause: DSPy optimizes toward the metric, so the quality of the metric directly determines the quality of the optimized pipeline.

Resolution: The speaker emphasizes metric definition, baseline evaluation, and iteration before relying on optimized prompts.

“the most important part is going to be the metric”transcript, stated
“the better your metric is going to be the better your outcome or uh the evaluation that is going to be for your particular use case”transcript, stated

Alphanumeric sorting fails for logical categories like clothing sizes.

Root cause: Values such as S, M, L, and XL have a semantic order that ordinary string sorting does not capture.

Resolution: The Mistral classifier sends non-alphanumeric cases to GPT-4 for semantic sorting.

“for something like shirts you have uh size and size is like s m l Etc now you can't do an numeric sort on this because it doesn't make sense”transcript, stated
“for this cases the ml actually is going to say no this is not going to be an alpha numeric sort”transcript, stated

Optimization experiments can become expensive because each iteration calls an LLM.

Root cause: DSPy prompt optimization itself invokes models during compilation/evaluation.

Resolution: The speaker limited MIPRO to about 10 iterations in the Sanskrit experiment because of computational cost.

“I didn't try this with like a huge number of uh llm uh evaluations because the prompt itself goes to an llm”transcript, stated
“I just gave it like 10 iterations or so”transcript, stated

Evaluation

Zoro's DSPy approach uses explicitly defined task metrics, zero-shot baselines, few-shot comparisons, optimizer compilation, and iteration. The production sorter is evaluated by whether it can classify alphanumeric versus semantic sorting and produce usable sorted attributes for search.

Metrics: Zero-shot Sanskrit translation score of 51.7% · Few-shot Sanskrit translation score of 70.0% · MIPRO score of about 71.5% after roughly 10 iterations · Product catalog scale of 3.5 million products and 75,000 attributes · Attribute-specific correctness for alphanumeric versus semantic sorting

3 supporting quotes
“you simply do an uh zero shot evaluation which means you don't do any kind of optimization at all”transcript, stated
“the zero shot uh 51.7% U with few shot itself it actually got a pretty big bump which is 70.0”transcript, stated
“finally you keep iterating on this with better metrics or better examples Etc”transcript, stated

Outcomes

The system supports a 3.5M-product catalog, 300+ suppliers, 75,000 attribute types, about 1M monthly active users, and examples such as 2,500 products sharing one function-length attribute.

3 supporting quotes
“it is in uh production as in like in a company like that”transcript, stated
“we first started with uh Mell 8p uh and then uh for some use cases uh that was not enough so we just switched it over to open uh AI GPD 4”transcript, stated
“proper attribute normalization and sorting directly affects product discoverability.”summary, stated