fix(evaluation): return last assistant message on max_iterations fallback#173
Open
Ricardo-M-L wants to merge 1 commit into
Open
fix(evaluation): return last assistant message on max_iterations fallback#173Ricardo-M-L wants to merge 1 commit into
Ricardo-M-L wants to merge 1 commit into
Conversation
…back
When the agent loop hits `max_iterations` without producing a final
answer, both `AzureOpenAIAgentLoop` and `OpenAIAgentLoop` returned
`messages[-1].get("content", "")`. But the loop appends a
`{"role": "tool", ...}` message after each tool call, so the last
message is often a tool response — not the assistant's last
reasoning/answer.
Walk the list in reverse to find the last assistant message with
non-empty content instead, falling back to "" only if none exists.
Also add unit tests covering:
- last message is a tool response -> returns earlier assistant content
- only tool responses present -> returns ""
- last message is assistant -> returned directly
Broken out from the closed PR agent-infra#167 per the "one fix per PR" policy.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This was referenced Apr 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #172.
When the evaluation agent loop hits
max_iterations, bothAzureOpenAIAgentLoopandOpenAIAgentLoopreturnedmessages[-1].get(\"content\", \"\"). Since each tool call appends a{\"role\": \"tool\", ...}entry tomessages, the final element is typically a tool response — not the assistant's reasoning — so the evaluation harness treats raw tool output as the agent's answer.This PR walks
messagesin reverse to find the last assistant message with non-empty content, falling back to\"\"only if none exists.Changes
evaluation/agent_loop.py— apply the fallback walk in both agent loops (identical 4-line change in each)evaluation/tests/test_max_iterations_fallback.py— 3 unit tests per loop covering:\"\"Notes
max_iterationsfallback path.🤖 Generated with Claude Code