From 60aad5af8621f8f0433bb115eb4ab55d728a10fe Mon Sep 17 00:00:00 2001 From: Serghei Grajdean Date: Thu, 20 Aug 2026 15:18:30 +0300 Subject: [PATCH] Fix Lab 02 (Python): reset input_list per turn and resolve tool calls in conversation The chat loop created input_list once, before the loop, and never cleared it. After the first turn that called a tool, every later turn re-sent function call outputs that had already been handled, and always fired the follow-up request -- so a turn with no tool call had its answer silently overwritten by a re-summary of the previous turn's tool data. The follow-up request also used previous_response_id without conversation, so the agent's answers after tool calls were never saved to the conversation and the function calls were left unresolved in conversation state. Attaching the outputs to the conversation instead resolves them and stores the answer, and matches the pattern already used in the consolidated A4 lab. - Move the input_list comment inside the chat loop so the list is created per turn - Send function call outputs with conversation= instead of previous_response_id - Drop the duplicate FunctionTool import from the Add references snippet Co-Authored-By: Claude Opus 5 (1M context) --- Instructions/Exercises/02-agent-custom-tools.md | 7 +++++-- Labfiles/02-agent-custom-tools/Python/agent.py | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Instructions/Exercises/02-agent-custom-tools.md b/Instructions/Exercises/02-agent-custom-tools.md index 694d52b70..1855c7de3 100644 --- a/Instructions/Exercises/02-agent-custom-tools.md +++ b/Instructions/Exercises/02-agent-custom-tools.md @@ -153,7 +153,6 @@ Now you're ready to create an AI agent that uses MCP server tools to access exte ```python # Add references from azure.ai.projects import AIProjectClient - from azure.ai.projects.models import FunctionTool from azure.identity import DefaultAzureCredential from azure.ai.projects.models import PromptAgentDefinition, FunctionTool from openai.types.responses.response_input_param import FunctionCallOutput, ResponseInputParam @@ -312,6 +311,8 @@ Now that you've created the agent with the function tools, you can send messages input_list: ResponseInputParam = [] ``` + This list is created inside the chat loop so that each turn starts with a fresh set of function call outputs. + 1. Find the comment **Send a prompt to the agent** and add the following code: ```python @@ -375,8 +376,8 @@ Now that you've created the agent with the function tools, you can send messages # Send function call outputs back to the model and retrieve a response if input_list: response = openai_client.responses.create( + conversation=conversation.id, input=input_list, - previous_response_id=response.id, extra_body={"agent_reference": {"name": agent.name, "type": "agent_reference"}}, ) # Display the agent's response @@ -385,6 +386,8 @@ Now that you've created the agent with the function tools, you can send messages This code checks if there are any function call outputs in the input list, and if so, it sends them back to the agent as input to retrieve an updated response. Finally, it prints the agent's response. + Note that the outputs are attached to the same **conversation**, so the function calls are resolved in conversation state and the agent's answer is saved to the chat history. Sending them back with `previous_response_id` instead would make the *next* message fail with *"No tool output found for function call"*. + 1. Find the comment **Delete the agent when done** and add the following code: ```python diff --git a/Labfiles/02-agent-custom-tools/Python/agent.py b/Labfiles/02-agent-custom-tools/Python/agent.py index aa5ce660e..16921ec2b 100644 --- a/Labfiles/02-agent-custom-tools/Python/agent.py +++ b/Labfiles/02-agent-custom-tools/Python/agent.py @@ -32,15 +32,15 @@ def main(): # Create a thread for the chat session - # Create a list to hold function call outputs that will be sent back as input to the agent - - while True: user_input = input("Enter a prompt for the astronomy agent. Use 'quit' to exit.\nUSER: ").strip() if user_input.lower() == "quit": print("Exiting chat.") break + # Create a list to hold function call outputs that will be sent back as input to the agent + + # Send a prompt to the agent