MCPcopy Index your code
hub / github.com/google/adk-python / call_agent_async

Function call_agent_async

contributing/samples/models/interactions_api/main.py:57–128  ·  view source on GitHub ↗

Call the agent asynchronously with the user's prompt. Args: runner: The agent runner user_id: The user ID session_id: The session ID prompt: The prompt to send agent_name: The expected agent name for filtering responses show_interaction_id: Whether to show interaction IDs

(
    runner: Runner,
    user_id: str,
    session_id: str,
    prompt: str,
    agent_name: str = "",
    show_interaction_id: bool = True,
    additional_parts: list[types.Part] | None = None,
)

Source from the content-addressed store, hash-verified

55
56
57async def call_agent_async(
58 runner: Runner,
59 user_id: str,
60 session_id: str,
61 prompt: str,
62 agent_name: str = "",
63 show_interaction_id: bool = True,
64 additional_parts: list[types.Part] | None = None,
65) -> tuple[str, str | None]:
66 """Call the agent asynchronously with the user's prompt.
67
68 Args:
69 runner: The agent runner
70 user_id: The user ID
71 session_id: The session ID
72 prompt: The prompt to send
73 agent_name: The expected agent name for filtering responses
74 show_interaction_id: Whether to show interaction IDs in output
75 additional_parts: Optional list of additional content parts (e.g. files)
76
77 Returns:
78 A tuple of (response_text, interaction_id)
79 """
80 parts = [types.Part.from_text(text=prompt)]
81 if additional_parts:
82 parts.extend(additional_parts)
83
84 content = types.Content(role="user", parts=parts)
85
86 final_response_text = ""
87 last_interaction_id = None
88
89 print(f"\n>> User: {prompt}")
90
91 async for event in runner.run_async(
92 user_id=user_id,
93 session_id=session_id,
94 new_message=content,
95 run_config=RunConfig(save_input_blobs_as_artifacts=False),
96 ):
97 # Track interaction ID if available
98 if event.interaction_id:
99 last_interaction_id = event.interaction_id
100
101 # Show function calls
102 if event.get_function_calls():
103 for fc in event.get_function_calls():
104 print(f" [Tool Call] {fc.name}({fc.args})")
105
106 # Show function responses
107 if event.get_function_responses():
108 for fr in event.get_function_responses():
109 print(f" [Tool Result] {fr.name}: {fr.response}")
110
111 # Collect text responses from the agent (not user, not partial)
112 if (
113 event.content
114 and event.content.parts

Callers 7

test_function_callingFunction · 0.70
test_google_search_toolFunction · 0.70
test_pdf_summarizationFunction · 0.70
interactive_modeFunction · 0.70

Calls 5

RunConfigClass · 0.90
get_function_callsMethod · 0.80
extendMethod · 0.45
run_asyncMethod · 0.45

Tested by 6

test_function_callingFunction · 0.56
test_google_search_toolFunction · 0.56
test_pdf_summarizationFunction · 0.56