Skip to content

Commit fbb0457

Browse files
committed
Reduce complexity of _call_tools function
Extract metadata population logic into separate _populate_deferred_calls helper function to reduce cyclomatic complexity from 16 to 15.
1 parent 367d860 commit fbb0457

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pydantic_ai_slim/pydantic_ai/_agent_graph.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,19 @@ async def handle_call_or_result(
10301030
output_parts.extend([tool_parts_by_index[k] for k in sorted(tool_parts_by_index)])
10311031
output_parts.extend([user_parts_by_index[k] for k in sorted(user_parts_by_index)])
10321032

1033+
_populate_deferred_calls(
1034+
tool_calls, deferred_calls_by_index, deferred_metadata_by_index, output_deferred_calls, output_deferred_metadata
1035+
)
1036+
1037+
1038+
def _populate_deferred_calls(
1039+
tool_calls: list[_messages.ToolCallPart],
1040+
deferred_calls_by_index: dict[int, Literal['external', 'unapproved']],
1041+
deferred_metadata_by_index: dict[int, dict[str, Any]],
1042+
output_deferred_calls: dict[Literal['external', 'unapproved'], list[_messages.ToolCallPart]],
1043+
output_deferred_metadata: dict[str, dict[str, Any]],
1044+
) -> None:
1045+
"""Populate deferred calls and metadata from indexed mappings."""
10331046
for k in sorted(deferred_calls_by_index):
10341047
call = tool_calls[k]
10351048
output_deferred_calls[deferred_calls_by_index[k]].append(call)

0 commit comments

Comments
 (0)