Day 7 chained summarize_text into classify_sentiment — free text to free text. Now you have classify_urgency with a Literal type. What changes if you swap the classifier?
The chain is the same — summary goes in, label comes out. But the label is now constrained to three exact values instead of whatever the model decides to write.
Exactly. Same pipeline shape, higher reliability on the output. That's the structured output upgrade:
def summarize_and_classify(text: str) -> str:
summary = summarize_text(text)
return Agent(model, result_type=Literal["urgent","normal","low"]).run_sync(summary).outputWhy inline the second agent instead of calling classify_urgency? Aren't they the same?
You could call classify_urgency directly — and that's clean if you want to reuse it. Inlining shows you how the chain reads when it's all in one function: summarise first, classify second, return the typed result. For a real codebase, compose the named functions. For learning the pattern, seeing it flat is clearer.
from typing import Literal
def summarize_and_classify(text: str) -> str:
summary = summarize_text(text)
result = Agent(model, result_type=Literal["urgent","normal","low"]).run_sync(summary)
return result.outputSo every stage in the chain can have its own result_type. The first agent returns a string, the second returns a Literal — I compose typed functions like typed Python.
That's the Week 2 payoff. Week 1 gave you chains. Week 2 gives you typed chains — where every stage's output is a known shape.
Client email in, urgency label out — two agents, one typed result. I'd use this as a triage step before anything hits my inbox.
What would you do with the urgency label? Route it to a different inbox? Trigger a calendar reminder?
Each agent in a chain can have its own result_type. The first produces a string; the second receives it as input and returns a Literal:
summary = summarize_text(text) # str
result = Agent(model, result_type=Literal["urgent","normal","low"]).run_sync(summary)
result.output # 'urgent', 'normal', or 'low' — guaranteedDay 7 used classify_sentiment — a free-text classifier with .strip().lower(). The Literal constraint here enforces the valid set at the framework level:
.strip().lower())The pipeline shape is identical. The output guarantee is stronger. Use this pattern whenever a downstream step depends on the classification being exactly one of a fixed set.
Day 7 chained summarize_text into classify_sentiment — free text to free text. Now you have classify_urgency with a Literal type. What changes if you swap the classifier?
The chain is the same — summary goes in, label comes out. But the label is now constrained to three exact values instead of whatever the model decides to write.
Exactly. Same pipeline shape, higher reliability on the output. That's the structured output upgrade:
def summarize_and_classify(text: str) -> str:
summary = summarize_text(text)
return Agent(model, result_type=Literal["urgent","normal","low"]).run_sync(summary).outputWhy inline the second agent instead of calling classify_urgency? Aren't they the same?
You could call classify_urgency directly — and that's clean if you want to reuse it. Inlining shows you how the chain reads when it's all in one function: summarise first, classify second, return the typed result. For a real codebase, compose the named functions. For learning the pattern, seeing it flat is clearer.
from typing import Literal
def summarize_and_classify(text: str) -> str:
summary = summarize_text(text)
result = Agent(model, result_type=Literal["urgent","normal","low"]).run_sync(summary)
return result.outputSo every stage in the chain can have its own result_type. The first agent returns a string, the second returns a Literal — I compose typed functions like typed Python.
That's the Week 2 payoff. Week 1 gave you chains. Week 2 gives you typed chains — where every stage's output is a known shape.
Client email in, urgency label out — two agents, one typed result. I'd use this as a triage step before anything hits my inbox.
What would you do with the urgency label? Route it to a different inbox? Trigger a calendar reminder?
Each agent in a chain can have its own result_type. The first produces a string; the second receives it as input and returns a Literal:
summary = summarize_text(text) # str
result = Agent(model, result_type=Literal["urgent","normal","low"]).run_sync(summary)
result.output # 'urgent', 'normal', or 'low' — guaranteedDay 7 used classify_sentiment — a free-text classifier with .strip().lower(). The Literal constraint here enforces the valid set at the framework level:
.strip().lower())The pipeline shape is identical. The output guarantee is stronger. Use this pattern whenever a downstream step depends on the classification being exactly one of a fixed set.
Create a free account to get started. Paid plans unlock all tracks.