Using LLMs to classify products, search queries, etc is by now boring. Yet it can still be difficult to constrains the LLM’s output to the legal vocabulary of brands, colors, categories, etc your system allows.
In the Wayfair WANDS e-commerce dataset, for example, you want to classify a query like “wood coffee table” into its most appropriate category. Of which there are hundreds:
Furniture / Office Furniture / Desks Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables Furniture / Living Room Furniture / Coffee Tables & End Tables / End & Side Tables Décor & Pillows / Decorative Pillows & Blankets / Throw Pillows Furniture / Bedroom Furniture / Dressers & Chests
The classic way to implement this would be with structured outputs. You tell your provide it must constrain its outputs to a list of legal values. In Pydantic, you create a giant literal of legal output values:
from typing import Literal from pydantic import BaseModel, Field FullyQualifiedClassifications = Literal[ 'Furniture / Bedroom Furniture / Beds & Headboards / Beds', 'Furniture / Living Room Furniture / Chairs & Seating / Accent Chairs', 'Rugs / Area Rugs', ... # times 500 ] class QueryClassification(BaseModel): """ Structured representation of a search query for furniture e-commerce. Inherits keywords from the base Query model and adds category and sub-category. """ classifications: list[FullyQualifiedClassifications] = Field( description="A possible classification for the product." ) response = client.responses.parse( model="gpt-5.4-mini", input="Classify the query: brown coffee table", text_format=QueryClassification, ) print(response.output_parsed.message) # Outputs: Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
This works. But there’s a way to do this a lot cheaper with small / dumb models at scale. Not to mention, there’s an upper limit you can send
Luckily, there’s an easy pattern that makes LLM classification pretty seamless.
Just ask a dumb LLM to invent plausible, fake classifications for your query:
hallucination_prompt = f""" Your task is to create novel, never seen before, furniture, home goods, or hardware classification that best fit a search query. Product classifications might look like: Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables Décor & Pillows / Decorative Pillows & Blankets / Throw Pillows Furniture / Bedroom Furniture / Dressers & Chests Kitchen & Tabletop / Kitchen Organization / Food Storage & Canisters School Furniture and Supplies / School Furniture / School Chairs & Seating / Stackable Chairs Baby & Kids / Toddler & Kids Bedroom Furniture / Kids Beds Here's the query to generate classifications for: brown coffee table
Now we’re not sending the list of legal classifications. We’re instead, asking the LLM to make stuff up:
... continue reading