The hallucinating classifier pattern
-->
Don't fill this out if you're human:
Doug's search blog and newsletter
Subscribe
Learn more
Check your email to confirm your subscription
back home
The hallucinating classifier pattern
August<br>10th,
2026
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<br>Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables<br>Furniture / Living Room Furniture / Coffee Tables & End Tables / End & Side Tables<br>Décor & Pillows / Decorative Pillows & Blankets / Throw Pillows<br>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<br>from pydantic import BaseModel, Field
FullyQualifiedClassifications = Literal[<br>'Furniture / Bedroom Furniture / Beds & Headboards / Beds',<br>'Furniture / Living Room Furniture / Chairs & Seating / Accent Chairs',<br>'Rugs / Area Rugs',<br>...<br># times 500
class QueryClassification(BaseModel):<br>"""<br>Structured representation of a search query for furniture e-commerce.<br>Inherits keywords from the base Query model and adds category and sub-category.<br>"""<br>classifications: list[FullyQualifiedClassifications] = Field(<br>description="A possible classification for the product."
response = client.responses.parse(<br>model="gpt-5.4-mini",<br>input="Classify the query: brown coffee table",<br>text_format=QueryClassification,
print(response.output_parsed.message)<br># 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"""<br>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<br>Décor & Pillows / Decorative Pillows & Blankets / Throw Pillows<br>Furniture / Bedroom Furniture / Dressers & Chests<br>Kitchen & Tabletop / Kitchen Organization / Food Storage & Canisters<br>School Furniture and Supplies / School Furniture / School Chairs & Seating / Stackable Chairs<br>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:
response = client.responses.parse(<br>model="gpt-5.4-mini",<br>input=hallucination_prompt,<br>text_format=list[str],
It’ll then make up some BS that doesn’t actually exist in your real taxonomy like:
Furniture / Living Room / Tables / Coffee
Well that’s not very helpful.
Actually it’s extremely helpful. You can now resolve that into the real vocabulary.
It’s very cheap to build an in-memory set of embeddings of the REAL classifications. As I’ve done in this notebook and this utility.
In the notebook, I compute a MiniLM embedding of every real Wayfair classification. I compute the embedding of the fake, hypothetical embedding from the LLM. I then dot product the fake embedding into the real ones to find the most similar. Producing:
Furniture / Living Room Furniture / Coffee Tables & End Tables / Coffee Tables
You can give these hallucination tasks to dumb / cheap LLMs. And you don’t need to ship the schema over to the LLM every time.
Upcoming events: Vectors Week
Join me for Vectors Week, a series of events about vector retrieval, hybrid search, and building your own vector database.
Doug Turnbull
More from Doug
Twitter | LinkedIn | Newsletter | Bsky