Skip to content

Commit 0eca36f

Browse files
authored
Fix pydantic-ai docs (CopilotKit#2522)
1 parent 9ee60ce commit 0eca36f

4 files changed

Lines changed: 166 additions & 27 deletions

File tree

docs/content/docs/pydantic-ai/generative-ui/agentic.mdx

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,96 @@ First, you'll need to make sure you have a running Pydantic AI Agent. If you hav
5151
<Tabs groupId="language" items={['Python']} default="Python">
5252
<Tab value="Python">
5353
```python title="agent-py/sample_agent/agent.py"
54+
import asyncio
55+
from textwrap import dedent
5456
from pydantic import BaseModel, Field
55-
from pydantic_ai import Agent
57+
from pydantic_ai import Agent, RunContext
5658
from pydantic_ai.ag_ui import StateDeps
59+
from ag_ui.core import StateSnapshotEvent, EventType
5760

58-
class Step(BaseModel):
59-
"""Represents a step in a plan."""
60-
description: str = Field(description='The description of the step')
61-
status: str = Field(default='pending', description='The status of the step')
6261

63-
class Plan(BaseModel):
64-
"""Represents a plan with multiple steps."""
65-
steps: list[Step] = Field(default_factory=list, description='The steps in the plan')
62+
class Search(BaseModel):
63+
query: str
64+
done: bool
6665

67-
agent = Agent('openai:gpt-4o-mini', deps_type=StateDeps[Plan])
68-
app = agent.to_ag_ui(deps=StateDeps(Plan()))
66+
67+
class AgentState(BaseModel):
68+
searches: list[Search] = Field(default_factory=list)
69+
70+
71+
agent = Agent("openai:gpt-4o-mini", deps_type=StateDeps[AgentState])
72+
73+
74+
@agent.tool
75+
async def add_search(
76+
ctx: RunContext[StateDeps[AgentState]], new_query: str
77+
) -> StateSnapshotEvent:
78+
"""Add a search to the agent's list of searches.
79+
80+
Args:
81+
ctx: The run context containing the agent's state.
82+
new_query: The query to add to the agent's state.
83+
84+
"""
85+
new_search = Search(query=new_query, done=False)
86+
searches = ctx.deps.state.searches
87+
searches.append(new_search)
88+
agent_state = AgentState(searches=searches)
89+
ctx.deps.state = agent_state
90+
91+
return StateSnapshotEvent(type=EventType.STATE_SNAPSHOT, snapshot=agent_state)
92+
93+
94+
@agent.tool
95+
async def run_searches(ctx: RunContext[StateDeps[AgentState]]) -> StateSnapshotEvent:
96+
"""Run the searches in the agent's state.
97+
98+
Args:
99+
ctx: The run context containing the agent's state.
100+
"""
101+
searches = ctx.deps.state.searches
102+
103+
for search in searches:
104+
await asyncio.sleep(1)
105+
search.done = True
106+
107+
agent_state = AgentState(searches=searches)
108+
ctx.deps.state = agent_state
109+
110+
return StateSnapshotEvent(type=EventType.STATE_SNAPSHOT, snapshot=agent_state)
111+
112+
113+
@agent.instructions()
114+
async def search_instructions(ctx: RunContext[StateDeps[AgentState]]) -> str:
115+
"""Instructions for the search agent.
116+
117+
Args:
118+
ctx: The run context containing search state information.
119+
120+
Returns:
121+
Instructions string for the search agent.
122+
"""
123+
return dedent(
124+
f"""
125+
You are a helpful assistant for storing searches.
126+
127+
IMPORTANT:
128+
- Use the `add_search` tool to add a search to the agent's state
129+
- After using the `add_search` tool, YOU MUST ALWAYS use the `run_searches` tool to run the searches in the agent's state.
130+
- ONLY USE THE `add_search` TOOL ONCE FOR A GIVEN QUERY.
131+
132+
The current state of the searches is:
133+
134+
{ctx.deps.state.model_dump_json(indent=2)}
135+
"""
136+
)
137+
138+
139+
app = agent.to_ag_ui(deps=StateDeps(AgentState()))
140+
if __name__ == "__main__":
141+
import uvicorn
142+
143+
uvicorn.run(app, host="0.0.0.0", port=8000)
69144
```
70145
</Tab>
71146
</Tabs>

docs/content/docs/pydantic-ai/generative-ui/tool-based.mdx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,21 @@ as this guide uses it as a starting point.
5353
```python title="agent/sample_agent/agent.py"
5454
from pydantic_ai import Agent
5555

56-
agent = Agent('openai:gpt-4o-mini')
56+
agent = Agent("openai:gpt-4o-mini")
57+
5758

5859
@agent.tool_plain
59-
async def get_current_time(timezone: str = 'UTC') -> str:
60-
"""Get the current time in a specific timezone."""
61-
from datetime import datetime
62-
from zoneinfo import ZoneInfo
60+
async def get_weather(location: str = "Everywhere ever") -> str:
61+
"""Get the weather for a given location. Ensure location is fully spelled out."""
62+
return f"The weather in {location} is sunny."
6363

64-
tz = ZoneInfo(timezone)
65-
return datetime.now(tz=tz).isoformat()
6664

6765
app = agent.to_ag_ui()
66+
67+
if __name__ == "__main__":
68+
import uvicorn
69+
70+
uvicorn.run(app, host="0.0.0.0", port=8000)
6871
```
6972
</Tab>
7073

docs/content/docs/pydantic-ai/shared-state/in-app-agent-read.mdx

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,47 @@ state update you can reflect these updates natively in your application.
4545
<Tab value="Python">
4646
```python title="agent-py/sample_agent/agent.py"
4747
from pydantic import BaseModel
48-
from pydantic_ai import Agent
48+
from pydantic_ai import Agent, RunContext
4949
from pydantic_ai.ag_ui import StateDeps
50+
from textwrap import dedent
51+
5052

5153
class AgentState(BaseModel):
5254
"""State for the agent."""
5355
language: str = "english"
5456

55-
agent = Agent('openai:gpt-4o-mini', deps_type=StateDeps[AgentState])
57+
58+
agent = Agent("openai:gpt-4o-mini", deps_type=StateDeps[AgentState])
59+
60+
61+
@agent.instructions()
62+
async def language_instructions(ctx: RunContext[StateDeps[AgentState]]) -> str:
63+
"""Instructions for the language tracking agent.
64+
65+
Args:
66+
ctx: The run context containing language state information.
67+
68+
Returns:
69+
Instructions string for the language tracking agent.
70+
"""
71+
return dedent(
72+
f"""
73+
You are a helpful assistant for tracking the language.
74+
75+
IMPORTANT:
76+
- ALWAYS use the lower case for the language
77+
- The language is not used for responding, always respond in english. Just track the language.
78+
79+
The current state of the language is: "{ctx.deps.state.language}"
80+
"""
81+
)
82+
5683
app = agent.to_ag_ui(deps=StateDeps(AgentState()))
84+
85+
if __name__ == "__main__":
86+
import uvicorn
87+
88+
uvicorn.run(app, host="0.0.0.0", port=8000)
5789
```
5890
</Tab>
5991
</Tabs>
@@ -137,7 +169,3 @@ function YourMainContent() {
137169

138170
By default, the Pydantic AI Agent state will only update _between_ Pydantic AI Agent node transitions --
139171
which means state updates will be discontinuous and delayed.
140-
141-
{/* You likely want to render the agent state as it updates **continuously.**
142-
143-
See **[emit intermediate state](/pydantic-ai/shared-state/predictive-state-updates).** */}

docs/content/docs/pydantic-ai/shared-state/in-app-agent-write.mdx

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,57 @@ when your agent is calling tools. CopilotKit allows you to fully customize how t
4848
from pydantic_ai import Agent, RunContext
4949
from pydantic_ai.ag_ui import StateDeps
5050
from ag_ui.core import StateSnapshotEvent, EventType
51+
from textwrap import dedent
52+
5153

5254
class AgentState(BaseModel):
5355
"""State for the agent."""
56+
5457
language: str = "english"
5558

56-
agent = Agent('openai:gpt-4o-mini', deps_type=StateDeps[AgentState])
59+
60+
agent = Agent("openai:gpt-4o-mini", deps_type=StateDeps[AgentState])
61+
5762

5863
@agent.tool_plain
5964
def update_language(language: str) -> StateSnapshotEvent:
6065
"""Update the language of the agent."""
6166
updated_state = {"language": language}
6267

63-
return StateSnapshotEvent(
64-
type=EventType.STATE_SNAPSHOT,
65-
snapshot=updated_state
68+
return StateSnapshotEvent(type=EventType.STATE_SNAPSHOT, snapshot=updated_state)
69+
70+
71+
@agent.instructions()
72+
async def language_instructions(ctx: RunContext[StateDeps[AgentState]]) -> str:
73+
"""Instructions for the language tracking agent.
74+
75+
Args:
76+
ctx: The run context containing language state information.
77+
78+
Returns:
79+
Instructions string for the language tracking agent.
80+
"""
81+
return dedent(
82+
f"""
83+
You are a helpful assistant for tracking the language.
84+
85+
IMPORTANT:
86+
- Use the `update_language` tool to update the language
87+
- ALWAYS use the lower case for the language
88+
- The language is not used for responding, always respond in english. Just track the language.
89+
90+
The current state of the language is: "{ctx.deps.state.language}"
91+
"""
6692
)
6793

94+
6895
app = agent.to_ag_ui(deps=StateDeps(AgentState()))
96+
97+
98+
if __name__ == "__main__":
99+
import uvicorn
100+
101+
uvicorn.run(app, host="0.0.0.0", port=8000)
69102
```
70103
</Tab>
71104
</Tabs>

0 commit comments

Comments
 (0)