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
2 changes: 0 additions & 2 deletions docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down Expand Up @@ -343,7 +342,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down
16 changes: 10 additions & 6 deletions docs/deferred-tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,21 @@ print(result.all_messages())
),
ModelRequest(
parts=[
ToolReturnPart(
tool_name='delete_file',
content='Deleting files is not allowed',
tool_call_id='delete_file',
timestamp=datetime.datetime(...),
),
ToolReturnPart(
tool_name='update_file',
content="File 'README.md' updated: 'Hello, world!'",
tool_call_id='update_file_readme',
timestamp=datetime.datetime(...),
)
]
),
ModelRequest(
parts=[
ToolReturnPart(
tool_name='delete_file',
content='Deleting files is not allowed',
tool_call_id='delete_file',
timestamp=datetime.datetime(...),
),
ToolReturnPart(
tool_name='update_file',
Expand Down
287 changes: 173 additions & 114 deletions pydantic_ai_slim/pydantic_ai/_agent_graph.py

Large diffs are not rendered by default.

33 changes: 6 additions & 27 deletions pydantic_ai_slim/pydantic_ai/agent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@
from ..settings import ModelSettings, merge_model_settings
from ..tools import (
AgentDepsT,
DeferredToolCallResult,
DeferredToolResult,
DeferredToolResults,
DocstringFormat,
GenerateToolJsonSchema,
RunContext,
Tool,
ToolApproved,
ToolDenied,
ToolFuncContext,
ToolFuncEither,
ToolFuncPlain,
Expand Down Expand Up @@ -462,7 +458,7 @@ def iter(
) -> AbstractAsyncContextManager[AgentRun[AgentDepsT, RunOutputDataT]]: ...

@asynccontextmanager
async def iter( # noqa: C901
async def iter(
self,
user_prompt: str | Sequence[_messages.UserContent] | None = None,
*,
Expand Down Expand Up @@ -505,7 +501,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down Expand Up @@ -559,7 +554,6 @@ async def main():
del model

deps = self._get_deps(deps)
new_message_index = len(message_history) if message_history else 0
output_schema = self._prepare_output_schema(output_type, model_used.profile)

output_type_ = output_type or self.output_type
Expand Down Expand Up @@ -620,27 +614,12 @@ async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
instrumentation_settings = None
tracer = NoOpTracer()

tool_call_results: dict[str, DeferredToolResult] | None = None
if deferred_tool_results is not None:
tool_call_results = {}
for tool_call_id, approval in deferred_tool_results.approvals.items():
if approval is True:
approval = ToolApproved()
elif approval is False:
approval = ToolDenied()
tool_call_results[tool_call_id] = approval

if calls := deferred_tool_results.calls:
call_result_types = _utils.get_union_args(DeferredToolCallResult)
for tool_call_id, result in calls.items():
if not isinstance(result, call_result_types):
result = _messages.ToolReturn(result)
tool_call_results[tool_call_id] = result

graph_deps = _agent_graph.GraphAgentDeps[AgentDepsT, RunOutputDataT](
graph_deps = _agent_graph.GraphAgentDeps[
AgentDepsT, RunOutputDataT
](
user_deps=deps,
prompt=user_prompt,
new_message_index=new_message_index,
new_message_index=0, # This will be set in `UserPromptNode` based on the length of the cleaned message history
model=model_used,
model_settings=model_settings,
usage_limits=usage_limits,
Expand All @@ -651,13 +630,13 @@ async def get_instructions(run_context: RunContext[AgentDepsT]) -> str | None:
history_processors=self.history_processors,
builtin_tools=list(self._builtin_tools),
tool_manager=tool_manager,
tool_call_results=tool_call_results,
tracer=tracer,
get_instructions=get_instructions,
instrumentation_settings=instrumentation_settings,
)
start_node = _agent_graph.UserPromptNode[AgentDepsT](
user_prompt=user_prompt,
deferred_tool_results=deferred_tool_results,
instructions=self._instructions,
instructions_functions=self._instructions_functions,
system_prompts=self._system_prompts,
Expand Down
14 changes: 7 additions & 7 deletions pydantic_ai_slim/pydantic_ai/agent/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,12 +499,13 @@ async def on_complete() -> None:
]

parts: list[_messages.ModelRequestPart] = []
async for _event in _agent_graph.process_function_tools(
graph_ctx.deps.tool_manager,
tool_calls,
final_result,
graph_ctx,
parts,
async for _event in _agent_graph.process_tool_calls(
tool_manager=graph_ctx.deps.tool_manager,
tool_calls=tool_calls,
tool_call_results=None,
final_result=final_result,
ctx=graph_ctx,
output_parts=parts,
):
pass
if parts:
Expand Down Expand Up @@ -621,7 +622,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down
1 change: 0 additions & 1 deletion pydantic_ai_slim/pydantic_ai/agent/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down
1 change: 0 additions & 1 deletion pydantic_ai_slim/pydantic_ai/durable_exec/dbos/_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down
2 changes: 0 additions & 2 deletions pydantic_ai_slim/pydantic_ai/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down Expand Up @@ -183,7 +182,6 @@ async def main():
[
UserPromptNode(
user_prompt='What is the capital of France?',
instructions=None,
instructions_functions=[],
system_prompts=(),
system_prompt_functions=[],
Expand Down
6 changes: 3 additions & 3 deletions tests/test_a2a.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,10 +623,10 @@ def track_messages(messages: list[ModelMessage], info: AgentInfo) -> ModelRespon
content='Final result processed.',
tool_call_id=IsStr(),
timestamp=IsDatetime(),
)
]
),
UserPromptPart(content='Second message', timestamp=IsDatetime()),
],
),
ModelRequest(parts=[UserPromptPart(content='Second message', timestamp=IsDatetime())]),
]
)

Expand Down
Loading