Yesterday's summarize_text uses a system prompt to constrain length. What if you need the model to return exactly one word — positive, negative, or neutral — for a survey open-response?
A stricter system prompt. Instead of 'summarize in 2 sentences', I'd say 'Return exactly one word: positive, negative, or neutral.' Then strip and lowercase the output.
Precisely. The system prompt acts like a Likert scale — you define the response options, and the model can only pick one. .strip().lower() normalises any extra whitespace or capitalisation:
def classify_sentiment(text: str) -> str:
agent = Agent(model, system_prompt="Return exactly one word: positive, negative, or neutral.")
result = agent.run_sync(text)
return result.output.strip().lower()What if the model ignores the instruction and returns a sentence anyway? I've seen LLMs do that.
It happens. For a one-word instruction, most models comply — but not always. .strip().lower() catches extra whitespace and case. For stricter enforcement, Week 2 introduces result_type=Literal["positive","negative","neutral"] which forces structured output at the API level. Today's approach works for most cases:
def classify_sentiment(text: str) -> str:
agent = Agent(model, system_prompt="Return exactly one word: positive, negative, or neutral.")
result = agent.run_sync(text)
label = result.output.strip().lower()
print(f"Sentiment: {label}")
return labelI can run this on every open-text survey response in my dataset — 200 responses classified in seconds instead of hours of manual coding.
Qualitative coding is one of the most time-consuming parts of survey research. A classifier that runs in 200 agent calls is not a replacement for human judgment on individual responses — but it gives you a first-pass frequency distribution you can verify with a sample.
My methodology chapter just got a section on 'AI-assisted qualitative coding'. That's going to raise some eyebrows at my thesis defence.
Disclose it clearly and validate a sample manually. Reviewers accept AI-assisted coding when the validation method is described. The tool is the easy part — the methodology note is the work.
agent = Agent(model, system_prompt="Return exactly one word: positive, negative, or neutral.")
result = agent.run_sync(text)
return result.output.strip().lower().strip().lower()Without result_type enforcement, the model returns free text. .strip() removes leading and trailing whitespace; .lower() normalises case so "Positive" and "positive" compare equal.
The system prompt is a best-effort instruction. The model usually complies, but it can return "I am unable to classify this" for ambiguous input. Use result_type=Literal[...] when you need API-level enforcement.
Use system prompt constraints for quick prototypes. Use Literal types when classification labels feed downstream logic that requires exact string matching.
Yesterday's summarize_text uses a system prompt to constrain length. What if you need the model to return exactly one word — positive, negative, or neutral — for a survey open-response?
A stricter system prompt. Instead of 'summarize in 2 sentences', I'd say 'Return exactly one word: positive, negative, or neutral.' Then strip and lowercase the output.
Precisely. The system prompt acts like a Likert scale — you define the response options, and the model can only pick one. .strip().lower() normalises any extra whitespace or capitalisation:
def classify_sentiment(text: str) -> str:
agent = Agent(model, system_prompt="Return exactly one word: positive, negative, or neutral.")
result = agent.run_sync(text)
return result.output.strip().lower()What if the model ignores the instruction and returns a sentence anyway? I've seen LLMs do that.
It happens. For a one-word instruction, most models comply — but not always. .strip().lower() catches extra whitespace and case. For stricter enforcement, Week 2 introduces result_type=Literal["positive","negative","neutral"] which forces structured output at the API level. Today's approach works for most cases:
def classify_sentiment(text: str) -> str:
agent = Agent(model, system_prompt="Return exactly one word: positive, negative, or neutral.")
result = agent.run_sync(text)
label = result.output.strip().lower()
print(f"Sentiment: {label}")
return labelI can run this on every open-text survey response in my dataset — 200 responses classified in seconds instead of hours of manual coding.
Qualitative coding is one of the most time-consuming parts of survey research. A classifier that runs in 200 agent calls is not a replacement for human judgment on individual responses — but it gives you a first-pass frequency distribution you can verify with a sample.
My methodology chapter just got a section on 'AI-assisted qualitative coding'. That's going to raise some eyebrows at my thesis defence.
Disclose it clearly and validate a sample manually. Reviewers accept AI-assisted coding when the validation method is described. The tool is the easy part — the methodology note is the work.
agent = Agent(model, system_prompt="Return exactly one word: positive, negative, or neutral.")
result = agent.run_sync(text)
return result.output.strip().lower().strip().lower()Without result_type enforcement, the model returns free text. .strip() removes leading and trailing whitespace; .lower() normalises case so "Positive" and "positive" compare equal.
The system prompt is a best-effort instruction. The model usually complies, but it can return "I am unable to classify this" for ambiguous input. Use result_type=Literal[...] when you need API-level enforcement.
Use system prompt constraints for quick prototypes. Use Literal types when classification labels feed downstream logic that requires exact string matching.
Create a free account to get started. Paid plans unlock all tracks.