Your batch_classify returns all labels. What if you need the most concise answer from a set of prompts — like testing which phrasing of a question gets the tightest response?
Run all the prompts, get all the responses, then pick the one with the fewest characters. min() with key=len.
Exactly right. One list comprehension to run all the calls, one min() to pick the shortest:
responses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)min(key=len) compares by string length? Not alphabetically?
key=len tells min() to compare elements by their length — shortest string wins. Without key=len, min() compares alphabetically and returns the string that comes first in A-to-Z order, which is not what you want. Here's the full function:
def shortest_response(prompts: list) -> str:
responses = [Agent(model).run_sync(p).output for p in prompts]
result = min(responses, key=len)
print(f"Shortest: {len(result)} chars")
return resultI'm A/B testing my prompts and picking the winner by response length. My LLM is now a conciseness judge.
That's a real use case — if you're generating SMS copy or push notification text, shortest response with acceptable quality wins. min(key=len) is the comparison function.
I can test five phrasings of my onboarding message and pick whichever the AI handles most concisely. Quality filter on top of length filter.
Length is a proxy for conciseness, not quality. The shortest response might also be the least useful. In production you'd add a quality check — run classify_sentiment(response) to filter out negative or unhelpful outputs before applying min(key=len).
min() with key=lenresponses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)key=len tells min() to compare by string length (character count). Without it, min() uses lexicographic ordering — which returns the alphabetically first string, not the shortest.
Shortest is not always best. The shortest response might also be the least complete. Add a quality filter (classify_sentiment) before applying min(key=len) in production.
Your batch_classify returns all labels. What if you need the most concise answer from a set of prompts — like testing which phrasing of a question gets the tightest response?
Run all the prompts, get all the responses, then pick the one with the fewest characters. min() with key=len.
Exactly right. One list comprehension to run all the calls, one min() to pick the shortest:
responses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)min(key=len) compares by string length? Not alphabetically?
key=len tells min() to compare elements by their length — shortest string wins. Without key=len, min() compares alphabetically and returns the string that comes first in A-to-Z order, which is not what you want. Here's the full function:
def shortest_response(prompts: list) -> str:
responses = [Agent(model).run_sync(p).output for p in prompts]
result = min(responses, key=len)
print(f"Shortest: {len(result)} chars")
return resultI'm A/B testing my prompts and picking the winner by response length. My LLM is now a conciseness judge.
That's a real use case — if you're generating SMS copy or push notification text, shortest response with acceptable quality wins. min(key=len) is the comparison function.
I can test five phrasings of my onboarding message and pick whichever the AI handles most concisely. Quality filter on top of length filter.
Length is a proxy for conciseness, not quality. The shortest response might also be the least useful. In production you'd add a quality check — run classify_sentiment(response) to filter out negative or unhelpful outputs before applying min(key=len).
min() with key=lenresponses = [Agent(model).run_sync(p).output for p in prompts]
return min(responses, key=len)key=len tells min() to compare by string length (character count). Without it, min() uses lexicographic ordering — which returns the alphabetically first string, not the shortest.
Shortest is not always best. The shortest response might also be the least complete. Add a quality filter (classify_sentiment) before applying min(key=len) in production.
Create a free account to get started. Paid plans unlock all tracks.