From 6d3a6281c60481caed8a741496e422d577f15352 Mon Sep 17 00:00:00 2001 From: ME-Msc <1543669883@qq.com> Date: Wed, 8 Jan 2025 16:41:31 +0800 Subject: [PATCH] Docs: add `asyncio` to all examples that need it. --- docs/agents.md | 2 +- docs/dependencies.md | 8 ++++---- docs/message-history.md | 2 +- docs/multi-agent-applications.md | 4 ++-- docs/results.md | 8 ++++---- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/agents.md b/docs/agents.md index 56f9dcc2b..0309fb5c5 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -89,7 +89,7 @@ async def main(): print(await response.get_data()) #> London ``` -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ You can also pass messages from previous runs to continue a conversation or provide context, as described in [Messages and Chat History](message-history.md). diff --git a/docs/dependencies.md b/docs/dependencies.md index be533614b..62822017b 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -47,7 +47,7 @@ async def main(): 2. Pass the dataclass type to the `deps_type` argument of the [`Agent` constructor][pydantic_ai.Agent.__init__]. **Note**: we're passing the type here, NOT an instance, this parameter is not actually used at runtime, it's here so we can get full type checking of the agent. 3. When running the agent, pass an instance of the dataclass to the `deps` parameter. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ ## Accessing Dependencies @@ -97,7 +97,7 @@ async def main(): 3. Access dependencies through the [`.deps`][pydantic_ai.tools.RunContext.deps] attribute. 4. Access dependencies through the [`.deps`][pydantic_ai.tools.RunContext.deps] attribute. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ ### Asynchronous vs. Synchronous dependencies @@ -154,7 +154,7 @@ async def main(): 1. Here we use a synchronous `httpx.Client` instead of an asynchronous `httpx.AsyncClient`. 2. To match the synchronous dependency, the system prompt function is now a plain function, not a coroutine. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ ## Full Example @@ -222,7 +222,7 @@ async def main(): 1. To pass `RunContext` to a tool, use the [`tool`][pydantic_ai.Agent.tool] decorator. 2. `RunContext` may optionally be passed to a [`result_validator`][pydantic_ai.Agent.result_validator] function as the first argument. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ ## Overriding Dependencies diff --git a/docs/message-history.md b/docs/message-history.md index e730d2c47..9b7e440f5 100644 --- a/docs/message-history.md +++ b/docs/message-history.md @@ -141,7 +141,7 @@ async def main(): ] """ ``` -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ ### Using Messages as Input for Further Agent Runs diff --git a/docs/multi-agent-applications.md b/docs/multi-agent-applications.md index cda678592..6556fdbf5 100644 --- a/docs/multi-agent-applications.md +++ b/docs/multi-agent-applications.md @@ -163,7 +163,7 @@ async def main(): 5. Define a tool on the delegate agent that uses the dependencies to make an HTTP request. 6. Usage now includes 4 requests — 2 from the calling agent and 2 from the delegate agent. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ This example shows how even a fairly simple agent delegation can lead to a complex control flow: @@ -306,7 +306,7 @@ async def main(): # (7)! 6. Define a function to find the user's seat preference, which asks the user for their seat preference and then calls the agent to extract the seat preference. 7. Now that we've put our logic for running each agent into separate functions, our main app becomes very simple. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ The control flow for this example can be summarised as follows: diff --git a/docs/results.md b/docs/results.md index 292ed11f5..dbd3e321b 100644 --- a/docs/results.md +++ b/docs/results.md @@ -190,7 +190,7 @@ async def main(): 2. The [`Agent.run_stream()`][pydantic_ai.Agent.run_stream] method is used to start a streamed run, this method returns a context manager so the connection can be closed when the stream completes. 3. Each item yield by [`StreamedRunResult.stream()`][pydantic_ai.result.StreamedRunResult.stream] is the complete text response, extended as new data is received. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ We can also stream text as deltas rather than the entire text in each item: @@ -214,7 +214,7 @@ async def main(): 1. [`stream_text`][pydantic_ai.result.StreamedRunResult.stream_text] will error if the response is not text -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ !!! warning "Result message not included in `messages`" The final result message will **NOT** be added to result messages if you use `.stream_text(delta=True)`, @@ -261,7 +261,7 @@ async def main(): #> {'name': 'Ben', 'dob': date(1990, 1, 28), 'bio': 'Likes the chain the dog and the pyramid'} ``` -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ If you want fine-grained control of validation, particularly catching validation errors, you can use the following pattern: @@ -307,7 +307,7 @@ async def main(): 1. [`stream_structured`][pydantic_ai.result.StreamedRunResult.stream_structured] streams the data as [`ModelResponse`][pydantic_ai.messages.ModelResponse] objects, thus iteration can't fail with a `ValidationError`. 2. [`validate_structured_result`][pydantic_ai.result.StreamedRunResult.validate_structured_result] validates the data, `allow_partial=True` enables pydantic's [`experimental_allow_partial` flag on `TypeAdapter`][pydantic.type_adapter.TypeAdapter.validate_json]. -_(This example is complete, it can be run "as is")_ +_(This example is complete, it can be run "as is" — you'll need to add `asyncio.run(main())` to run `main`)_ ## Examples