> ## Documentation Index
> Fetch the complete documentation index at: https://prompt.university/llms.txt
> Use this file to discover all available pages before exploring further.

# Lesson 2.1: Text Classification Prompts

> Learn how to extract reliable classifications from text-generating LLMs

<Info>
  **Duration:** 60 minutes
</Info>

## Introduction

Text classification is one of the most common NLP tasks—from sentiment analysis to content moderation. While LLMs are primarily designed to generate text, we can leverage them for classification by carefully structuring our prompts. In this lesson, you'll learn proven patterns to get reliable, consistent classifications.

## The Classification Challenge

LLMs generate text probabilistically, which creates unique challenges for classification:

<Warning>
  **Common Issues:**

  * Inconsistent output formats
  * Verbose explanations when you need labels
  * Hallucinated categories
  * Difficulty with edge cases
</Warning>

The solution? **Constrained output patterns** that guide the model toward structured responses.

## Core Classification Patterns

### Pattern 1: Direct Label Classification

The simplest approach—provide examples and ask for a label.

```
Classify the sentiment of the following text as Positive, Negative, or Neutral.

Text: "The product exceeded my expectations!"
Sentiment:
```

**Output:**

```
Positive
```

<Tip>
  **Best for:** Simple binary or multi-class classification with clear boundaries
</Tip>

### Pattern 2: Few-Shot Classification

Provide examples to establish the pattern and decision boundaries.

```
Classify customer feedback into categories.

Example 1: "Fast shipping, great quality" → Category: Product Quality
Example 2: "Customer service was unhelpful" → Category: Customer Service
Example 3: "Website is hard to navigate" → Category: User Experience

Now classify: "The checkout process was confusing"
Category:
```

**Output:**

```
User Experience
```

<Note>
  Few-shot examples help the model understand:

  * The classification criteria
  * Edge case handling
  * Output format expectations
</Note>

### Pattern 3: Constrained Output with Reasoning

Get both the classification and the reasoning behind it.

```
Classify the following email as Spam or Not Spam.
Provide your reasoning, then give the final classification.

Email: "Congratulations! You've won $1,000,000! Click here to claim your prize now!"

Reasoning:
Classification:
```

**Output:**

```
Reasoning: This email contains typical spam indicators: unrealistic prize claim, urgency tactics, and suspicious links without context.
Classification: Spam
```

### Pattern 4: Multi-Label Classification

When items can belong to multiple categories simultaneously.

```
Classify the following article into one or more categories: Technology, Business, Health, Politics, Entertainment.

Article: "Apple announces new health features in latest iPhone, boosting stock prices."

Categories (comma-separated):
```

**Output:**

```
Technology, Business, Health
```

## Advanced Techniques

### Confidence Scoring

Ask the model to provide confidence levels with classifications.

```
Classify the sentiment and provide a confidence score (0-100).

Text: "The product is okay, nothing special."

Sentiment:
Confidence:
```

**Output:**

```
Sentiment: Neutral
Confidence: 85
```

### Hierarchical Classification

For complex taxonomies, use a two-stage approach.

```
First, classify the broad category, then the specific subcategory.

Text: "The laptop's battery life is disappointing."

Step 1 - Broad Category (Product, Service, Shipping, Other):
Step 2 - Specific Issue (if Product: Quality, Performance, Design, Price):
```

**Output:**

```
Step 1 - Broad Category: Product
Step 2 - Specific Issue: Performance
```

### Zero-Shot with Definitions

Provide clear definitions when examples aren't available.

```
Classify the following text using these definitions:

- Informative: Provides factual information or explanations
- Persuasive: Attempts to convince or influence opinion
- Entertaining: Primarily for amusement or enjoyment
- Instructional: Teaches how to do something

Text: "Follow these steps to bake the perfect chocolate cake."

Classification:
```

**Output:**

```
Instructional
```

## Handling Edge Cases

### Ambiguous Cases

```
Classify the sentiment. If ambiguous, explain why and provide the most likely classification.

Text: "The service was fast, but the food was cold."

Analysis:
Classification:
```

**Output:**

```
Analysis: Mixed sentiment - positive about speed, negative about quality. The negative aspect (core product) likely outweighs the positive (service speed).
Classification: Negative
```

### Unknown Categories

```
Classify into: Bug Report, Feature Request, Question, or Other.
If "Other", briefly explain.

Text: "I love the new design!"

Classification:
Explanation (if Other):
```

**Output:**

```
Classification: Other
Explanation: This is positive feedback/praise, not fitting the technical support categories.
```

## Best Practices

<CardGroup cols={2}>
  <Card title="Be Explicit" icon="bullseye">
    Clearly define all possible categories and their boundaries
  </Card>

  <Card title="Use Examples" icon="list">
    Provide 2-5 examples per category when possible
  </Card>

  <Card title="Constrain Output" icon="lock">
    Specify exact format: "Respond with only: Category A, Category B, or Category C"
  </Card>

  <Card title="Handle Edge Cases" icon="triangle-exclamation">
    Include instructions for ambiguous or out-of-scope inputs
  </Card>
</CardGroup>

## Common Pitfalls

<Accordion title="Pitfall 1: Vague Categories">
  **Problem:** "Classify as good or bad"

  **Solution:** Define what makes something "good" or "bad" in your context

  ```
  Classify product reviews as Positive (recommends product, satisfied) or Negative (dissatisfied, would not recommend).
  ```
</Accordion>

<Accordion title="Pitfall 2: Too Many Categories">
  **Problem:** Asking the model to choose from 20+ categories

  **Solution:** Use hierarchical classification or group related categories

  ```
  First classify as: Product, Service, or Shipping
  Then provide specific subcategory based on the first classification
  ```
</Accordion>

<Accordion title="Pitfall 3: Inconsistent Formatting">
  **Problem:** Model returns "positive", "Positive", "POSITIVE", or "The sentiment is positive"

  **Solution:** Explicitly constrain the output format

  ```
  Respond with exactly one word: Positive, Negative, or Neutral
  ```
</Accordion>

## Practice Exercises

### Exercise 1: Sentiment Analysis

Create a prompt to classify movie reviews as Positive, Negative, or Mixed. Test with:

* "Best film I've seen this year!"
* "Terrible acting, waste of time"
* "Great visuals but weak plot"

<Accordion title="Sample Solution">
  ```
  Classify the movie review sentiment as exactly one of: Positive, Negative, or Mixed.

  Positive: Overall recommendation, more positive than negative aspects
  Negative: Overall not recommended, more negative than positive aspects  
  Mixed: Balanced positive and negative aspects

  Review: [INSERT REVIEW]
  Sentiment:
  ```
</Accordion>

### Exercise 2: Content Moderation

Design a prompt to classify social media posts as Safe, Review, or Unsafe based on content policy.

<Accordion title="Sample Solution">
  ```
  Classify social media content according to these categories:

  Safe: Appropriate for all audiences, no policy violations
  Review: Potentially problematic, needs human review (borderline cases)
  Unsafe: Clear policy violation (hate speech, violence, explicit content)

  Post: [INSERT POST]
  Classification:
  Reason:
  ```
</Accordion>

### Exercise 3: Multi-Label Classification

Create a prompt to tag news articles with relevant topics (can have multiple tags).

<Accordion title="Sample Solution">
  ```
  Tag the following news article with all relevant categories from this list:
  Politics, Technology, Business, Health, Science, Sports, Entertainment, Environment

  Provide tags as a comma-separated list.

  Article: [INSERT ARTICLE]
  Tags:
  ```
</Accordion>

## Real-World Application: Email Triage System

Let's build a complete email classification system:

```
You are an email triage assistant. Classify emails into categories and assign priority.

Categories:
- Sales: Customer inquiries about products/services
- Support: Technical issues or help requests  
- Billing: Payment, invoices, refunds
- Feedback: Suggestions, complaints, reviews
- Spam: Unsolicited or irrelevant messages

Priority Levels:
- High: Urgent issues, angry customers, time-sensitive
- Medium: Standard requests, general inquiries
- Low: Feedback, non-urgent questions

Email: "I was charged twice for my subscription this month. Please refund the duplicate charge immediately."

Category:
Priority:
Reasoning:
```

**Output:**

```
Category: Billing
Priority: High
Reasoning: Billing error requiring immediate attention. Customer expects prompt resolution of duplicate charge.
```

## Key Takeaways

<Check>Constrain outputs by explicitly listing valid categories</Check>
<Check>Use few-shot examples to establish classification boundaries</Check>
<Check>Handle edge cases and ambiguous inputs explicitly</Check>
<Check>Consider hierarchical classification for complex taxonomies</Check>
<Check>Request reasoning when you need explainable classifications</Check>

## Next Steps

Now that you can classify text reliably, you're ready to extract structured information from unstructured content.

<Card title="Next: Lesson 2.2 - Information Extraction Prompts" icon="arrow-right" href="/module-2/lesson-2">
  Learn to pull structured data from unstructured text
</Card>
