Best practices & review
Let's talk production patterns. Always validate. Always verify calculations. Test with edge cases: what happens with empty fields, unusual formats, or multilingual text?
Structured output retry loop
How to handle malformed LLM output with retries and fallbacks
class Review(BaseModel):
product_name: str
rating: int # 1-5 stars
sentiment: str # "positive", "neutral", "negative"
pros: list[str]
cons: list[str]
summary: str
review_text = """
Just got the new AirPods Pro 2. Sound quality is amazing
and the noise cancellation is top-notch. Battery life is
decent at 6 hours. Only downside is the price at $249
and the case is a bit slippery. Overall 4/5 stars.
"""
prompt = f"Analyze this product review as JSON: {review_text}"
data = extract_json(prompt)
review = Review(**data)
print(f"Rating: {'⭐' * review.rating}")
print(f"Sentiment: {review.sentiment}")
print(f"Pros: {', '.join(review.pros)}")
print(f"Cons: {', '.join(review.cons)}")Sentiment analysis with structured output: extract rating, pros, cons, and a summary from any product review.
Matching exercise: Match extraction task to best practice
Loading practice…
Timed quiz: Structured outputs speed round
Loading practice…
Validation checklist: Structured outputs checklist
Loading practice…