Skip to navigation

Pydantic AI

View as MarkdownOpen in Claude

Pydantic AI is a Python agent framework from the Pydantic team. Its capability library, Pydantic AI Harness, includes two capabilities backed by You.com APIs:

  • YouSearch adds web_search (Web Search API) and get_page (Contents API).
  • YouResearch adds answer (Answer API), research (Research API), and finance_research (Finance Research API).

Each tool returns a Sources: block the model can cite, plus the same sources as structured metadata for your code.


Getting Started

1

Install the Package

pip install "pydantic-ai-harness[youdotcom,anthropic]"

The youdotcom extra installs the You.com Python SDK, and anthropic installs the model provider used in the examples below. For another provider, add its Pydantic AI extra, such as "pydantic-ai-slim[openai]". Requires Python 3.11+.

Pydantic AI Harness is on 0.x releases, so its API can change between minor versions.

2

Set Your API Keys

export YDC_API_KEY="<YDC_API_KEY>"
export ANTHROPIC_API_KEY="<ANTHROPIC_API_KEY>"

Get your You.com API key at you.com/platform. Creating a capability without YDC_API_KEY set raises an error.


Usage

from pydantic_ai import Agent
from pydantic_ai_harness import YouResearch, YouSearch
agent = Agent('anthropic:claude-sonnet-4-6', capabilities=[YouSearch(), YouResearch()])
result = agent.run_sync('What changed in the latest stable Python release?')
print(result.output)

Use YouSearch alone if the agent only needs search and page reads.

The Pydantic AI Harness reference lists every field and covers custom clients, multiple instances, native search fallback, and YAML agent specs.


Tools

ToolCapabilityAPIUse when
web_searchYouSearchWeb Search APISurvey the web. Returns up to num_results pages with excerpts
get_pageYouSearchContents APIRead one URL in full as markdown
answerYouResearchAnswer APIOne question, one cited answer
researchYouResearchResearch APIQuestions that need many searches and a cited write-up
finance_researchYouResearchFinance Research APICompanies, markets, and financial instruments

Each tool call is billed at the rate of its underlying API.

Error Handling

FailureBehavior
web_search finds nothingReturns No results found for '<query>'. and the run continues
Empty result, 422, 429, 5xx, or network errorSent to the model as ModelRetry, so it can fix the input or try again
401, 402, or 403Raised, and the run stops until you fix the key, credits, or permissions

The error code reference lists what each status means.


Durable Execution

Under Temporal, Prefect, or DBOS, each You.com request is recorded under the capability id (you_search or you_research), so a recovered run reuses the result instead of calling the API again. Keep the id stable once deployed—renaming it breaks in-flight runs.

A Temporal activity times out after 60 seconds by default, which research can exceed. Give the you_research toolset more time:

from datetime import timedelta
from pydantic_ai.durable_exec.temporal import TemporalDurability
from temporalio.workflow import ActivityConfig
TemporalDurability(
toolset_activity_config={
'you_research': ActivityConfig(start_to_close_timeout=timedelta(minutes=10)),
},
)

Cookbook

The pydantic-cookbook repo has four runnable examples:

ExampleMain APITopic
starters/education_web_search.pyWeb Search APIK-12 teacher burnout and retention
starters/healthcare_research.pyResearch APICandida auris outbreak and antimicrobial resistance
starters/finance_research.pyFinance Research APIPrivate credit market stress
deal_desk.pyAll fiveDue diligence with a lead agent and three specialist subagents
git clone https://github.com/youdotcom-oss/pydantic-cookbook
cd pydantic-cookbook
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env # add YDC_API_KEY and OPENROUTER_API_KEY
make smoke # builds every agent without network calls
make run-edu # or run-health, run-finance, run-deal

Resources