Your search_the_web returns a long prose response. For a sales brief you want one key fact and its source — not a paragraph. How do you get that?
Pass the search result to a structured extractor. Define a Pydantic model with fact: str and source: str, run it on the search output.
Exactly the pattern. Stage 1: search. Stage 2: extract. Two agents, one function:
from pydantic import BaseModel
class Fact(BaseModel):
fact: str
source: str
bg = search_the_web(topic)
result = Agent(model, result_type=Fact).run_sync(bg)
return result.output.model_dump()The extraction agent runs on the search result, not the original query. So Stage 2 is reading the search output, not the web directly?
Correct. The Perplexity search returns grounded text. The extraction agent reads that text and identifies the key fact and source. It's working from a rich, cited source — much better signal than extracting from a raw web page. Here's the full function:
from pydantic import BaseModel
class Fact(BaseModel):
fact: str
source: str
def research_and_extract(topic: str) -> dict:
bg = search_the_web(topic)
result = Agent(model, result_type=Fact).run_sync(bg)
data = result.output.model_dump()
print(f"Fact: {data['fact'][:60]}")
return dataI'm an analyst with a research team. Stage 1 researches, Stage 2 writes the briefing note. The agent is picking the key fact — I just defined what a fact looks like.
The Fact model is your editorial policy in code — you defined that every brief has exactly one key fact and one source. The model fills the form.
Before every investor meeting I can run this on every item in the deck — competitive analysis, market size, customer name. Briefing in seconds.
The Fact model uses ai-basic for the extraction stage — not ai-search. The search already happened in Stage 1. Stage 2 is just extracting from text, which doesn't need web access. Use the right model for each stage.
bg = search_the_web(topic) # Stage 1: search
result = Agent(model, result_type=Fact).run_sync(bg) # Stage 2: extract
return result.output.model_dump()Each stage does one thing and does it well.
from pydantic import BaseModel
class Fact(BaseModel):
fact: str # the key finding
source: str # where it came fromDefine Fact above the entry-point function. The validator checks class definition order.
Your search_the_web returns a long prose response. For a sales brief you want one key fact and its source — not a paragraph. How do you get that?
Pass the search result to a structured extractor. Define a Pydantic model with fact: str and source: str, run it on the search output.
Exactly the pattern. Stage 1: search. Stage 2: extract. Two agents, one function:
from pydantic import BaseModel
class Fact(BaseModel):
fact: str
source: str
bg = search_the_web(topic)
result = Agent(model, result_type=Fact).run_sync(bg)
return result.output.model_dump()The extraction agent runs on the search result, not the original query. So Stage 2 is reading the search output, not the web directly?
Correct. The Perplexity search returns grounded text. The extraction agent reads that text and identifies the key fact and source. It's working from a rich, cited source — much better signal than extracting from a raw web page. Here's the full function:
from pydantic import BaseModel
class Fact(BaseModel):
fact: str
source: str
def research_and_extract(topic: str) -> dict:
bg = search_the_web(topic)
result = Agent(model, result_type=Fact).run_sync(bg)
data = result.output.model_dump()
print(f"Fact: {data['fact'][:60]}")
return dataI'm an analyst with a research team. Stage 1 researches, Stage 2 writes the briefing note. The agent is picking the key fact — I just defined what a fact looks like.
The Fact model is your editorial policy in code — you defined that every brief has exactly one key fact and one source. The model fills the form.
Before every investor meeting I can run this on every item in the deck — competitive analysis, market size, customer name. Briefing in seconds.
The Fact model uses ai-basic for the extraction stage — not ai-search. The search already happened in Stage 1. Stage 2 is just extracting from text, which doesn't need web access. Use the right model for each stage.
bg = search_the_web(topic) # Stage 1: search
result = Agent(model, result_type=Fact).run_sync(bg) # Stage 2: extract
return result.output.model_dump()Each stage does one thing and does it well.
from pydantic import BaseModel
class Fact(BaseModel):
fact: str # the key finding
source: str # where it came fromDefine Fact above the entry-point function. The validator checks class definition order.
Create a free account to get started. Paid plans unlock all tracks.