Your classify_sentiment uses a system prompt that says 'respond with exactly one word.' What if the model adds punctuation or uses a synonym? You're back to parsing.
I'd want the model constrained to an exact set of values — like a dropdown in a form. No free text allowed.
result_type=Literal['high','medium','low'] is exactly that. The agent must return one of those three strings — not 'HIGH', not 'urgent', not a sentence. It's a type-safe dropdown:
from typing import Literal
result = Agent(model, result_type=Literal["high", "medium", "low"]).run_sync(text)
return result.outputSo result_type=Literal[...] replaces the system prompt? I don't need both?
For classification tasks, result_type=Literal[...] is cleaner — the constraint is enforced by the type system, not by hoping the model follows a text instruction. No .strip().lower() needed. Here's the full function:
from typing import Literal
def classify_urgency(text: str) -> str:
result = Agent(model, result_type=Literal["high", "medium", "low"]).run_sync(text)
urgency = result.output
print(f"Urgency: {urgency}")
return urgencySo Week 1 was a free-form system prompt, Week 2 is a Literal type. I'm climbing the reliability ladder.
The ladder is: system prompt → Literal → BaseModel. Each step is more constrained and more reliable. Use the simplest constraint that gives you what you need.
I can use classify_urgency on support tickets and route high-urgency ones to Slack immediately. No manual triage.
This function is also a building block for Day 13 — you'll chain it with summarize_text to classify the summary of long tickets, not the full text. Keep that composability pattern in mind.
result_type=Literal[...]Forces the model to return exactly one of the specified string values:
from typing import Literal
result = Agent(model, result_type=Literal["high", "medium", "low"]).run_sync(text)
urgency = result.output # guaranteed to be 'high', 'medium', or 'low'Literal is enforced by the type system — no normalisation needed. System prompt classifiers rely on instruction-following, which can fail. Use Literal whenever you need one of a fixed set of values.
from typing import Literal is required — it's not in the sandbox preamble by default.
Your classify_sentiment uses a system prompt that says 'respond with exactly one word.' What if the model adds punctuation or uses a synonym? You're back to parsing.
I'd want the model constrained to an exact set of values — like a dropdown in a form. No free text allowed.
result_type=Literal['high','medium','low'] is exactly that. The agent must return one of those three strings — not 'HIGH', not 'urgent', not a sentence. It's a type-safe dropdown:
from typing import Literal
result = Agent(model, result_type=Literal["high", "medium", "low"]).run_sync(text)
return result.outputSo result_type=Literal[...] replaces the system prompt? I don't need both?
For classification tasks, result_type=Literal[...] is cleaner — the constraint is enforced by the type system, not by hoping the model follows a text instruction. No .strip().lower() needed. Here's the full function:
from typing import Literal
def classify_urgency(text: str) -> str:
result = Agent(model, result_type=Literal["high", "medium", "low"]).run_sync(text)
urgency = result.output
print(f"Urgency: {urgency}")
return urgencySo Week 1 was a free-form system prompt, Week 2 is a Literal type. I'm climbing the reliability ladder.
The ladder is: system prompt → Literal → BaseModel. Each step is more constrained and more reliable. Use the simplest constraint that gives you what you need.
I can use classify_urgency on support tickets and route high-urgency ones to Slack immediately. No manual triage.
This function is also a building block for Day 13 — you'll chain it with summarize_text to classify the summary of long tickets, not the full text. Keep that composability pattern in mind.
result_type=Literal[...]Forces the model to return exactly one of the specified string values:
from typing import Literal
result = Agent(model, result_type=Literal["high", "medium", "low"]).run_sync(text)
urgency = result.output # guaranteed to be 'high', 'medium', or 'low'Literal is enforced by the type system — no normalisation needed. System prompt classifiers rely on instruction-following, which can fail. Use Literal whenever you need one of a fixed set of values.
from typing import Literal is required — it's not in the sandbox preamble by default.
Create a free account to get started. Paid plans unlock all tracks.