Skip to content

Fix Lab 02 (Python): reset input_list per turn and resolve tool calls in conversation - #247

Open
grajdeanserghei wants to merge 1 commit into
MicrosoftLearning:mainfrom
grajdeanserghei:fix-lab02-input-list-reset
Open

Fix Lab 02 (Python): reset input_list per turn and resolve tool calls in conversation#247
grajdeanserghei wants to merge 1 commit into
MicrosoftLearning:mainfrom
grajdeanserghei:fix-lab02-input-list-reset

Conversation

@grajdeanserghei

@grajdeanserghei grajdeanserghei commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Module: 02

Lab/Demo: 02 - Use a custom function in an AI agent (Python)

Fixes #245.

Changes proposed in this pull request:

  • Create input_list inside the chat loop instead of once before it. The list was never cleared, so 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. On a turn with no tool call, if input_list: was still truthy and the extra response overwrote the real answer — asking the agent something that needs no tool returned a re-summary of the previous turn's tool data instead. The stale outputs were also stored again in the conversation on each turn, duplicating items and growing the request payload every turn. The comment in agent.py moved inside the loop so the existing "find the comment, add this code" step now lands in the right place.
  • Send function call outputs with conversation=conversation.id instead of previous_response_id=response.id. With previous_response_id, the agent's answer after a tool call was never written to the conversation, and the turn's function_call items were left with no matching output in conversation state — a later request then fails with 400: No tool output found for function call <call_id>. The lab only avoided that error because the unreset input_list accidentally back-filled the missing outputs on the next turn, so the two bugs masked each other and both had to be fixed together. This also matches the pattern already used (and explained) in Instructions/Consolidated/A4-add-custom-function-tools.md. Note the two parameters are mutually exclusive — passing both returns 400 invalid_payload: "Cannot provide both 'previous_response_id' and 'conversation' in the same request".
  • Remove the duplicate FunctionTool import from the Add references snippet — it was imported on its own line and again as part of the PromptAgentDefinition, FunctionTool line.
  • Added two short explanatory notes to the instructions covering why the list is per-turn and why the outputs attach to the conversation.

Verification

Ran the completed lab app end-to-end against a live Foundry project (azure-ai-projects==2.0.0b4, gpt-4.1-mini), using the two prompts from the exercise walkthrough plus a third no-tool prompt.

Before the fix, turn 3 ("Just say hello, do not use any tools") returned a re-summary of the turn 1 telescope data instead of a greeting, and the conversation held each turn-1 function_call_output twice with only a single assistant message in the whole history.

After the fix, all three turns answer correctly, the no-tool turn skips the follow-up request, and the conversation state is a clean per-turn trace with no duplicates:

- message               role=assistant
- message               role=user
- message               role=assistant
- function_call_output  call_id=call_cPz5DreutVbRII0eR4tM1VcW
- function_call         name=generate_observation_report  call_id=call_cPz5DreutVbRII0eR4tM1VcW
- message               role=user
- message               role=assistant
- function_call_output  call_id=call_Gw4Z3R6Sdw1Y1h1KmkNm1Oj7
- function_call_output  call_id=call_pLhda4aNSxuCHoFutqH4STEJ
- function_call         name=calculate_observation_cost  call_id=call_Gw4Z3R6Sdw1Y1h1KmkNm1Oj7
- function_call         name=next_visible_event  call_id=call_pLhda4aNSxuCHoFutqH4STEJ
- message               role=user

Note on other labs

Instructions/Exercises/03-mcp-integration.md and Labfiles/03-mcp-integration/Python/client.py have the same two patterns (input_list declared before the while loop, follow-up sent with previous_response_id). I've left Lab 03 out of this PR to keep it scoped to the linked issue and to what I verified end-to-end — happy to extend this PR or open a separate one if you'd like that fixed too.

@grajdeanserghei
grajdeanserghei force-pushed the fix-lab02-input-list-reset branch from 39f50cc to 131532b Compare August 20, 2026 12:20
… 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) <noreply@anthropic.com>
@grajdeanserghei
grajdeanserghei force-pushed the fix-lab02-input-list-reset branch from 131532b to 60aad5a Compare August 20, 2026 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant