Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ async def main():
print(nodes)
"""
[
UserPromptNode(
user_prompt='What is the capital of France?',
system_prompts=(),
system_prompt_functions=[],
system_prompt_dynamic_functions={},
),
ModelRequestNode(
request=ModelRequest(
parts=[
Expand Down Expand Up @@ -338,6 +344,7 @@ if __name__ == '__main__':
print(output_messages)
"""
[
'=== UserPromptNode: What will the weather be like in Paris on Tuesday? ===',
'=== ModelRequestNode: streaming partial request tokens ===',
'[Request] Starting part 0: ToolCallPart(tool_name=\'weather_forecast\', args=\'{"location":"Pa\', tool_call_id=\'0001\', part_kind=\'tool-call\')',
'[Request] Part 0 args_delta=ris","forecast_',
Expand Down
1 change: 1 addition & 0 deletions docs/graph.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ async def main():
#> Node: CountDown()
#> Node: CountDown()
#> Node: CountDown()
#> Node: CountDown()
#> Node: End(data=0)
print('Final result:', run.result.output) # (3)!
#> Final result: 0
Expand Down
12 changes: 12 additions & 0 deletions pydantic_ai_slim/pydantic_ai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ async def main():
print(nodes)
'''
[
UserPromptNode(
user_prompt='What is the capital of France?',
system_prompts=(),
system_prompt_functions=[],
system_prompt_dynamic_functions={},
),
ModelRequestNode(
request=ModelRequest(
parts=[
Expand Down Expand Up @@ -1355,6 +1361,12 @@ async def main():
print(nodes)
'''
[
UserPromptNode(
user_prompt='What is the capital of France?',
system_prompts=(),
system_prompt_functions=[],
system_prompt_dynamic_functions={},
),
ModelRequestNode(
request=ModelRequest(
parts=[
Expand Down
8 changes: 8 additions & 0 deletions pydantic_graph/pydantic_graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ async def main():
print(node_states)
'''
[
(Increment(), MyState(number=1)),
(Increment(), MyState(number=1)),
(Check42(), MyState(number=2)),
(End(data=2), MyState(number=2)),
Expand All @@ -621,6 +622,7 @@ async def main():
print(node_states)
'''
[
(Increment(), MyState(number=41)),
(Increment(), MyState(number=41)),
(Check42(), MyState(number=42)),
(Increment(), MyState(number=42)),
Expand Down Expand Up @@ -665,6 +667,7 @@ def __init__(
self.deps = deps

self._next_node: BaseNode[StateT, DepsT, RunEndT] | End[RunEndT] = start_node
self._is_started: bool = False

@property
def next_node(self) -> BaseNode[StateT, DepsT, RunEndT] | End[RunEndT]:
Expand Down Expand Up @@ -777,8 +780,13 @@ def __aiter__(self) -> AsyncIterator[BaseNode[StateT, DepsT, RunEndT] | End[RunE

async def __anext__(self) -> BaseNode[StateT, DepsT, RunEndT] | End[RunEndT]:
"""Use the last returned node as the input to `Graph.next`."""
if not self._is_started:
self._is_started = True
return self._next_node

if isinstance(self._next_node, End):
raise StopAsyncIteration

return await self.next(self._next_node)

def __repr__(self) -> str:
Expand Down
4 changes: 3 additions & 1 deletion tests/graph/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ async def test_iter():
assert graph_iter.result
assert graph_iter.result.output == 8

assert node_reprs == snapshot(["String2Length(input_data='3.14')", 'Double(input_data=4)', 'End(data=8)'])
assert node_reprs == snapshot(
['Float2String(input_data=3.14)', "String2Length(input_data='3.14')", 'Double(input_data=4)', 'End(data=8)']
)


async def test_iter_next(mock_snapshot_id: object):
Expand Down