Skip to content

Commit 99bf3c8

Browse files
authored
Result docs (#72)
1 parent 6063844 commit 99bf3c8

File tree

15 files changed

+449
-80
lines changed

15 files changed

+449
-80
lines changed

docs/agents.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ There are three ways to run an agent:
6666

6767
Here's a simple example demonstrating all three:
6868

69-
```python title="run_agent.py"
69+
```py title="run_agent.py"
7070
from pydantic_ai import Agent
7171

7272
agent = Agent('openai:gpt-4o')
@@ -95,7 +95,7 @@ An agent **run** might represent an entire conversation — there's no limit to
9595

9696
Here's an example of a conversation comprised of multiple runs:
9797

98-
```python title="conversation_example.py" hl_lines="13"
98+
```py title="conversation_example.py" hl_lines="13"
9999
from pydantic_ai import Agent
100100

101101
agent = Agent('openai:gpt-4o')
@@ -131,7 +131,7 @@ You can add both to a single agent; they're concatenated in the order they're de
131131

132132
Here's an example using both types of system prompts:
133133

134-
```python title="system_prompts.py"
134+
```py title="system_prompts.py"
135135
from datetime import date
136136

137137
from pydantic_ai import Agent, CallContext
@@ -228,7 +228,7 @@ _(This example is complete, it can be run "as is")_
228228

229229
Let's print the messages from that game to see what happened:
230230

231-
```python title="dice_game_messages.py"
231+
```py title="dice_game_messages.py"
232232
from dice_game import dice_result
233233

234234
print(dice_result.all_messages())
@@ -448,7 +448,7 @@ If models behave unexpectedly (e.g., the retry limit is exceeded, or their API r
448448

449449
In these cases, [`agent.last_run_messages`][pydantic_ai.Agent.last_run_messages] can be used to access the messages exchanged during the run to help diagnose the issue.
450450

451-
```python
451+
```py
452452
from pydantic_ai import Agent, ModelRetry, UnexpectedModelBehaviour
453453

454454
agent = Agent('openai:gpt-4o')

docs/dependencies.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ _(This example is complete, it can be run "as is")_
5555
Dependencies are accessed through the [`CallContext`][pydantic_ai.dependencies.CallContext] type, this should be the first parameter of system prompt functions etc.
5656

5757

58-
```python title="system_prompt_dependencies.py" hl_lines="20-27"
58+
```py title="system_prompt_dependencies.py" hl_lines="20-27"
5959
from dataclasses import dataclass
6060

6161
import httpx
@@ -113,7 +113,7 @@ to use `async` methods where dependencies perform IO, although synchronous depen
113113

114114
Here's the same example as above, but with a synchronous dependency:
115115

116-
```python title="sync_dependencies.py"
116+
```py title="sync_dependencies.py"
117117
from dataclasses import dataclass
118118

119119
import httpx
@@ -161,7 +161,7 @@ _(This example is complete, it can be run "as is")_
161161

162162
As well as system prompts, dependencies can be used in [retrievers](agents.md#retrievers) and [result validators](results.md#result-validators-functions).
163163

164-
```python title="full_example.py" hl_lines="27-35 38-48"
164+
```py title="full_example.py" hl_lines="27-35 38-48"
165165
from dataclasses import dataclass
166166

167167
import httpx

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async def main():
135135
10. The result will be validated with Pydantic to guarantee it is a `SupportResult`, since the agent is generic, it'll also be typed as a `SupportResult` to aid with static type checking.
136136
11. In a real use case, you'd add many more retrievers and a longer system prompt to the agent to extend the context it's equipped with and support it can provide.
137137
12. This is a simple sketch of a database connection, used to keep the example short and readable. In reality, you'd be connecting to an external database (e.g. PostgreSQL) to get information about customers.
138-
13. This [Pydantic](https://docs.pydantic.dev) model is used to constrain the structured data returned by the agent. From this simple definition, Pydantic builds teh JSON Schema that tells the LLM how to return the data, and performs validation to guarantee the data is correct at the end of the conversation.
138+
13. This [Pydantic](https://docs.pydantic.dev) model is used to constrain the structured data returned by the agent. From this simple definition, Pydantic builds the JSON Schema that tells the LLM how to return the data, and performs validation to guarantee the data is correct at the end of the conversation.
139139

140140
To help make things more clear, here is a diagram of what is happening in the `#!python await support_agent.run('What is my balance?', deps=deps)` call within `main`:
141141
```mermaid

docs/message-history.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ and [`StreamedRunResult`][pydantic_ai.result.StreamedRunResult] (returned by [`A
2525

2626
Example of accessing methods on a [`RunResult`][pydantic_ai.result.RunResult] :
2727

28-
```python title="run_result_messages.py" hl_lines="10 28"
28+
```py title="run_result_messages.py" hl_lines="10 28"
2929
from pydantic_ai import Agent
3030

3131
agent = Agent('openai:gpt-4o', system_prompt='Be a helpful assistant.')
@@ -73,7 +73,7 @@ _(This example is complete, it can be run "as is")_
7373

7474
Example of accessing methods on a [`StreamedRunResult`][pydantic_ai.result.StreamedRunResult] :
7575

76-
```python title="streamed_run_result_messages.py" hl_lines="9 31"
76+
```py title="streamed_run_result_messages.py" hl_lines="9 31"
7777
from pydantic_ai import Agent
7878

7979
agent = Agent('openai:gpt-4o', system_prompt='Be a helpful assistant.')
@@ -96,11 +96,9 @@ async def main():
9696

9797
async for text in result.stream():
9898
print(text)
99-
#> Did you
100-
#> Did you hear about
99+
#> Did you hear
101100
#> Did you hear about the toothpaste
102-
#> Did you hear about the toothpaste scandal? They
103-
#> Did you hear about the toothpaste scandal? They called it
101+
#> Did you hear about the toothpaste scandal? They called
104102
#> Did you hear about the toothpaste scandal? They called it Colgate.
105103

106104
# complete messages once the stream finishes
@@ -190,7 +188,7 @@ Since messages are defined by simple dataclasses, you can manually create and ma
190188

191189
The message format is independent of the model used, so you can use messages in different agents, or the same agent with different models.
192190

193-
```python
191+
```py
194192
from pydantic_ai import Agent
195193

196194
agent = Agent('openai:gpt-4o', system_prompt='Be a helpful assistant.')

0 commit comments

Comments
 (0)