summarize_text from yesterday constrains the agent to two sentences. Your inbox has forty journal decision emails this month. You need a quick tone scan before triaging them. How do you reduce each email to a single sentiment label?
Tighten the system prompt to return exactly one word. Something like "Return exactly one word: positive, negative, or neutral." Then .strip().lower() to normalise.
That's it. The system prompt is the instruction; .strip().lower() is the safety net for leading spaces or capitalisation the model sometimes adds:
agent = Agent(model, system_prompt="Classify the sentiment. Return exactly one word: positive, negative, or neutral.")
return agent.run_sync(text).output.strip().lower()What if the model returns "Positive." with a period? Does .lower() strip that?
.lower() handles capitalisation but not punctuation. .strip() removes leading/trailing whitespace. For a hard guarantee of one of three strings, Week 2 introduces result_type=Literal["positive","negative","neutral"] — the model must return exactly one of those strings or the call fails. Today's pattern handles the common case; Literal handles the production case.
So right now I might get "positive." with a period. Week 2 will enforce "positive" exactly. I'll live with the period today.
Correct. And for inbox triage — positive, negative, neutral — a period doesn't change the downstream sort. The pattern gets you 95% of the way there now.
Forty decision emails, one word each, sorted into three piles. That's my Sunday evening triage done in three seconds.
A dispatcher loop looks like this:
labels = [classify_sentiment(email) for email in inbox]
negatives = [e for e, l in zip(inbox, labels) if l == "negative"]The model classifies tone, not importance. Sentiment is one signal; your review is the second.
agent = Agent(
model,
system_prompt="Classify the sentiment. Return exactly one word: positive, negative, or neutral."
)
return agent.run_sync(text).output.strip().lower().strip().lower() safety chain| Issue | Fix |
|---|---|
| Leading/trailing whitespace | .strip() |
| Capitalised first letter | .lower() |
| Period at end | Not fixed here — use Literal in Week 2 |
.strip().lower() is enoughFor human-readable triage (sort, group, display), .strip().lower() handles the common cases. For machine-readable pipelines (sort by key, join to a Sheet column), use result_type=Literal from Week 2 — the guaranteed single-word output prevents broken downstream joins.
summarize_text from yesterday constrains the agent to two sentences. Your inbox has forty journal decision emails this month. You need a quick tone scan before triaging them. How do you reduce each email to a single sentiment label?
Tighten the system prompt to return exactly one word. Something like "Return exactly one word: positive, negative, or neutral." Then .strip().lower() to normalise.
That's it. The system prompt is the instruction; .strip().lower() is the safety net for leading spaces or capitalisation the model sometimes adds:
agent = Agent(model, system_prompt="Classify the sentiment. Return exactly one word: positive, negative, or neutral.")
return agent.run_sync(text).output.strip().lower()What if the model returns "Positive." with a period? Does .lower() strip that?
.lower() handles capitalisation but not punctuation. .strip() removes leading/trailing whitespace. For a hard guarantee of one of three strings, Week 2 introduces result_type=Literal["positive","negative","neutral"] — the model must return exactly one of those strings or the call fails. Today's pattern handles the common case; Literal handles the production case.
So right now I might get "positive." with a period. Week 2 will enforce "positive" exactly. I'll live with the period today.
Correct. And for inbox triage — positive, negative, neutral — a period doesn't change the downstream sort. The pattern gets you 95% of the way there now.
Forty decision emails, one word each, sorted into three piles. That's my Sunday evening triage done in three seconds.
A dispatcher loop looks like this:
labels = [classify_sentiment(email) for email in inbox]
negatives = [e for e, l in zip(inbox, labels) if l == "negative"]The model classifies tone, not importance. Sentiment is one signal; your review is the second.
agent = Agent(
model,
system_prompt="Classify the sentiment. Return exactly one word: positive, negative, or neutral."
)
return agent.run_sync(text).output.strip().lower().strip().lower() safety chain| Issue | Fix |
|---|---|
| Leading/trailing whitespace | .strip() |
| Capitalised first letter | .lower() |
| Period at end | Not fixed here — use Literal in Week 2 |
.strip().lower() is enoughFor human-readable triage (sort, group, display), .strip().lower() handles the common cases. For machine-readable pipelines (sort by key, join to a Sheet column), use result_type=Literal from Week 2 — the guaranteed single-word output prevents broken downstream joins.
Create a free account to get started. Paid plans unlock all tracks.