@@ -104,33 +104,39 @@ print(f"Created message, ID: {message['id']}")
104104## Create a run and check the output
105105``` python
106106# Create and process a run for the agent to handle the message
107- run = project_client.agents.runs.create_and_process (thread_id = thread.id, agent_id = agent.id)
107+ run = project_client.agents.runs.create (thread_id = thread.id, agent_id = agent.id)
108108print (f " Created run, ID: { run.id} " )
109109
110110# Poll the run status until it is completed or requires action
111111while run.status in [" queued" , " in_progress" , " requires_action" ]:
112- time.sleep(1 )
113- run = project_client.agents.runs.get(thread_id = thread.id, run_id = run.id)
114-
115- if run.status == " requires_action" :
116- tool_calls = run.required_action.submit_tool_outputs.tool_calls
117- tool_outputs = []
118- for tool_call in tool_calls:
119- if tool_call.name == " fetch_weather" :
120- output = fetch_weather(" New York" )
121- tool_outputs.append({" tool_call_id" : tool_call.id, " output" : output})
122- project_client.agents.runs.submit_tool_outputs(thread_id = thread.id, run_id = run.id, tool_outputs = tool_outputs)
123-
124- print (f " Run completed with status: { run.status} " )
125-
126- # Fetch and log all messages from the thread
127- messages = project_client.agents.messages.list(thread_id = thread.id)
128- for message in messages:
129- print (f " Role: { message[' role' ]} , Content: { message[' content' ]} " )
130-
131- # Delete the agent after use
132- project_client.agents.delete_agent(agent.id)
133- print (" Deleted agent" )
112+ time.sleep(1 )
113+ run = agents_client.runs.get(thread_id = thread.id, run_id = run.id)
114+
115+ if run.status == " requires_action" and isinstance (run.required_action, SubmitToolOutputsAction):
116+ tool_calls = run.required_action.submit_tool_outputs.tool_calls
117+ if not tool_calls:
118+ print (" No tool calls provided - cancelling run" )
119+ agents_client.runs.cancel(thread_id = thread.id, run_id = run.id)
120+ break
121+
122+ tool_outputs = []
123+ for tool_call in tool_calls:
124+ if isinstance (tool_call, RequiredFunctionToolCall):
125+ try :
126+ print (f " Executing tool call: { tool_call} " )
127+ output = functions.execute(tool_call)
128+ tool_outputs.append(
129+ ToolOutput(
130+ tool_call_id = tool_call.id,
131+ output = output,
132+ )
133+ )
134+ except Exception as e:
135+ print (f " Error executing tool_call { tool_call.id} : { e} " )
136+
137+ print (f " Tool outputs: { tool_outputs} " )
138+ if tool_outputs:
139+ agents_client.runs.submit_tool_outputs(thread_id = thread.id, run_id = run.id, tool_outputs = tool_outputs)
134140```
135141
136142::: zone-end
0 commit comments