Real World Example

Product Listing Attribute Extraction

Marketplaces like Flipkart, Meesho and Amazon receive millions of seller listings with messy titles: "Mens Cotton Kurta Blue XL festive wear combo of 2 !!". Structured output turns them into clean attributes for filters and search:

from typing import Literal
from pydantic import BaseModel
import anthropic

class Listing(BaseModel):
    category: Literal["kurta", "shirt", "tshirt", "saree", "jeans", "other"]
    gender: Literal["men", "women", "unisex", "kids"]
    color: str
    sizes: list[str]
    material: str | None
    pack_of: int

client = anthropic.Anthropic()

def extract(title: str) -> Listing | None:
    try:
        r = client.messages.parse(
            model="claude-haiku-5-5", max_tokens=500,
            system="Extract listing attributes. Use null when a value is not stated.",
            messages=[{"role": "user", "content": title}],
            output_format=Listing,
        )
        listing = r.parsed_output
    except anthropic.APIError:
        return None                       # send to the manual review queue
    if listing.pack_of < 1 or listing.pack_of > 20:
        return None                       # business rule check
    return listing

print(extract("Mens Cotton Kurta Blue XL festive wear combo of 2 !!"))

Run it

python listing.py

Example output

category='kurta' gender='men' color='blue' sizes=['XL'] material='cotton' pack_of=2

At Scale

  • Cheap model (Haiku) because the task is narrow and volume is huge.
  • For millions of listings, use the Message Batches API (asynchronous, lower price) instead of one call at a time.
  • Track the share of listings that fail validation; a sudden rise usually means a new kind of seller input.