Skip to content

Commit cf66986

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 0cfa63b commit cf66986

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
@@ -1026,6 +1026,19 @@ async def handle_call_or_result(
10261026
output_parts.extend([tool_parts_by_index[k] for k in sorted(tool_parts_by_index)])
10271027
output_parts.extend([user_parts_by_index[k] for k in sorted(user_parts_by_index)])
10281028

1029+
_populate_deferred_calls(
1030+
tool_calls, deferred_calls_by_index, deferred_metadata_by_index, output_deferred_calls, output_deferred_metadata
1031+
)
1032+
1033+
1034+
def _populate_deferred_calls(
1035+
tool_calls: list[_messages.ToolCallPart],
1036+
deferred_calls_by_index: dict[int, Literal['external', 'unapproved']],
1037+
deferred_metadata_by_index: dict[int, dict[str, Any]],
1038+
output_deferred_calls: dict[Literal['external', 'unapproved'], list[_messages.ToolCallPart]],
1039+
output_deferred_metadata: dict[str, dict[str, Any]],
1040+
) -> None:
1041+
"""Populate deferred calls and metadata from indexed mappings."""
10291042
for k in sorted(deferred_calls_by_index):
10301043
call = tool_calls[k]
10311044
output_deferred_calls[deferred_calls_by_index[k]].append(call)

0 commit comments

Comments
 (0)