Yesterday the agent produced a list of scores. Today you use those scores to reorder the results — deepest sources first, shallow ones last. What is the Python primitive for ordering a list by a computed value?
sorted() with a key argument? The key function returns the value to sort by, and reverse=True puts high scores first.
Exactly. Step one: build a list of (score, result) tuples using the agent. Step two: sort by the score. Step three: return the results in the new order. The minimal shape:
agent = Agent(model, result_type=Literal["deep", "medium", "shallow"])
scored = [(agent.run_sync(r["snippet"]).output, r) for r in results]
rank_value = {"deep": 2, "medium": 1, "shallow": 0}
scored.sort(key=lambda pair: rank_value[pair[0]], reverse=True)
print([r for _, r in scored])The dict rank_value turns the Literal label into a number so sort has something ordered to work with.
So the agent gives me three labels, but I need numbers to actually sort — the dict is the translator?
Exactly. Python sorts on anything comparable; strings alphabetize, so "deep" < "medium" < "shallow" alphabetically — which is the wrong order for us. A small dict maps each label to an integer and key=lambda... does the lookup per pair. Full function:
def rank_results_by_agent_depth(query: str, count: int) -> list:
results = search(query, count=count)
agent = Agent(model, result_type=Literal["deep", "medium", "shallow"])
scored = [(agent.run_sync(r["snippet"]).output, r) for r in results]
rank_value = {"deep": 2, "medium": 1, "shallow": 0}
scored.sort(key=lambda pair: rank_value[pair[0]], reverse=True)
return [r for _, r in scored]Why a tuple (score, result) instead of attaching the score to the dict itself?
The tuple is throwaway — it only lives during sorting. Mutating results to add a score key would change the shape of every result permanently, which is wasteful when you only need ordering. A tuple is a light scaffold that disappears when you return the final list of original dicts, untouched.
And the final list contains exactly the same dicts as before, just in a new order?
Exactly. Same data, new order. Rankings are an opinion layered onto the engine's raw output — and now you own the opinion.
TL;DR: agent-scored tuples + sorted(key=...) + a label-to-int map = semantic ranking.
Literal — agent's rating shape(score, result) pairs discarded after sorting| Sort key | Result |
|---|---|
pair[0] directly | alphabetical — wrong order |
rank_value[pair[0]] | by mapped integer — correct |
Strings sort alphabetically; integers sort numerically. The dict bridges the two.
Yesterday the agent produced a list of scores. Today you use those scores to reorder the results — deepest sources first, shallow ones last. What is the Python primitive for ordering a list by a computed value?
sorted() with a key argument? The key function returns the value to sort by, and reverse=True puts high scores first.
Exactly. Step one: build a list of (score, result) tuples using the agent. Step two: sort by the score. Step three: return the results in the new order. The minimal shape:
agent = Agent(model, result_type=Literal["deep", "medium", "shallow"])
scored = [(agent.run_sync(r["snippet"]).output, r) for r in results]
rank_value = {"deep": 2, "medium": 1, "shallow": 0}
scored.sort(key=lambda pair: rank_value[pair[0]], reverse=True)
print([r for _, r in scored])The dict rank_value turns the Literal label into a number so sort has something ordered to work with.
So the agent gives me three labels, but I need numbers to actually sort — the dict is the translator?
Exactly. Python sorts on anything comparable; strings alphabetize, so "deep" < "medium" < "shallow" alphabetically — which is the wrong order for us. A small dict maps each label to an integer and key=lambda... does the lookup per pair. Full function:
def rank_results_by_agent_depth(query: str, count: int) -> list:
results = search(query, count=count)
agent = Agent(model, result_type=Literal["deep", "medium", "shallow"])
scored = [(agent.run_sync(r["snippet"]).output, r) for r in results]
rank_value = {"deep": 2, "medium": 1, "shallow": 0}
scored.sort(key=lambda pair: rank_value[pair[0]], reverse=True)
return [r for _, r in scored]Why a tuple (score, result) instead of attaching the score to the dict itself?
The tuple is throwaway — it only lives during sorting. Mutating results to add a score key would change the shape of every result permanently, which is wasteful when you only need ordering. A tuple is a light scaffold that disappears when you return the final list of original dicts, untouched.
And the final list contains exactly the same dicts as before, just in a new order?
Exactly. Same data, new order. Rankings are an opinion layered onto the engine's raw output — and now you own the opinion.
TL;DR: agent-scored tuples + sorted(key=...) + a label-to-int map = semantic ranking.
Literal — agent's rating shape(score, result) pairs discarded after sorting| Sort key | Result |
|---|---|
pair[0] directly | alphabetical — wrong order |
rank_value[pair[0]] | by mapped integer — correct |
Strings sort alphabetically; integers sort numerically. The dict bridges the two.
Create a free account to get started. Paid plans unlock all tracks.