Skip to content

Commit 344c510

Browse files
committed
update docstrings
1 parent 1d0b46f commit 344c510

File tree

4 files changed

+13
-6
lines changed

4 files changed

+13
-6
lines changed

docs/agents.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ There are five ways to run an agent:
6565

6666
1. [`agent.run()`][pydantic_ai.agent.AbstractAgent.run] — an async function which returns a [`RunResult`][pydantic_ai.agent.AgentRunResult] containing a completed response.
6767
2. [`agent.run_sync()`][pydantic_ai.agent.AbstractAgent.run_sync] — a plain, synchronous function which returns a [`RunResult`][pydantic_ai.agent.AgentRunResult] containing a completed response (internally, this just calls `loop.run_until_complete(self.run())`).
68-
3. [`agent.run_stream()`][pydantic_ai.agent.AbstractAgent.run_stream] — an async context manager which returns a [`StreamedRunResult`][pydantic_ai.result.StreamedRunResult], which contains methods to stream text and structured output as an async iterable. [`agent.run_stream_sync()`][pydantic_ai.agent.AbstractAgent.run_stream_sync] is a synchronous context manager variation with the same return type.
68+
3. [`agent.run_stream()`][pydantic_ai.agent.AbstractAgent.run_stream] — an async context manager which returns a [`StreamedRunResult`][pydantic_ai.result.StreamedRunResult], which contains methods to stream text and structured output as an async iterable. [`agent.run_stream_sync()`][pydantic_ai.agent.AbstractAgent.run_stream_sync] is a synchronous variation that returns the result directly.
6969
4. [`agent.run_stream_events()`][pydantic_ai.agent.AbstractAgent.run_stream_events] — a function which returns an async iterable of [`AgentStreamEvent`s][pydantic_ai.messages.AgentStreamEvent] and a [`AgentRunResultEvent`][pydantic_ai.run.AgentRunResultEvent] containing the final run result.
7070
5. [`agent.iter()`][pydantic_ai.Agent.iter] — a context manager which returns an [`AgentRun`][pydantic_ai.agent.AgentRun], an async iterable over the nodes of the agent's underlying [`Graph`][pydantic_graph.graph.Graph].
7171

pydantic_ai_slim/pydantic_ai/_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,12 @@ def get_union_args(tp: Any) -> tuple[Any, ...]:
489489
return tuple(_unwrap_annotated(arg) for arg in get_args(tp))
490490
else:
491491
return ()
492+
493+
494+
def get_event_loop():
495+
try:
496+
event_loop = asyncio.get_event_loop()
497+
except RuntimeError:
498+
event_loop = asyncio.new_event_loop()
499+
asyncio.set_event_loop(event_loop)
500+
return event_loop

pydantic_ai_slim/pydantic_ai/agent/abstract.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ def run_stream_sync(
638638
) -> result.StreamedRunResult[AgentDepsT, Any]:
639639
"""Run the agent with a user prompt in sync streaming mode.
640640
641-
This is a convenience method that wraps [`self.run_stream`][pydantic_ai.agent.AbstractAgent.run_stream] with `loop.run_until_complete(...)`.
641+
This is a convenience method that wraps [`run_stream()`][pydantic_ai.agent.AbstractAgent.run_stream] with `loop.run_until_complete(...)`.
642642
You therefore can't use this method inside async code or if there's an active event loop.
643643
644644
This method builds an internal agent graph (using system prompts, tools and output schemas) and then

pydantic_ai_slim/pydantic_ai/result.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from pydantic import ValidationError
1010
from typing_extensions import TypeVar, deprecated
1111

12-
from pydantic_graph._utils import get_event_loop
13-
1412
from . import _utils, exceptions, messages as _messages, models
1513
from ._output import (
1614
OutputDataT_inv,
@@ -563,7 +561,7 @@ def get_output_sync(self) -> OutputDataT:
563561
This is a convenience method that wraps [`get_output()`][pydantic_ai.result.StreamedRunResult.get_output] with `loop.run_until_complete(...)`.
564562
You therefore can't use this method inside async code or if there's an active event loop.
565563
"""
566-
return get_event_loop().run_until_complete(self.get_output())
564+
return _utils.get_event_loop().run_until_complete(self.get_output())
567565

568566
@property
569567
def response(self) -> _messages.ModelResponse:
@@ -641,7 +639,7 @@ class FinalResult(Generic[OutputDataT]):
641639

642640

643641
def _blocking_async_iterator(async_iter: AsyncIterator[T]) -> Iterator[T]:
644-
loop = get_event_loop()
642+
loop = _utils.get_event_loop()
645643
while True:
646644
try:
647645
yield loop.run_until_complete(async_iter.__anext__())

0 commit comments

Comments
 (0)