fix(agents): guard on chat.messages not chat.last to avoid IndexError in _run_tools#2609
Conversation
`_run_tools`'s no-op guard checked `not chat.last`, but `Chat.last` indexes `messages[-1]`, which raises IndexError on an empty list instead of behaving falsy. A ChatWorkflow with no messages added (e.g. `ChatWorkflow(...).run()`) hit this IndexError inside the guard's own condition instead of the intended clean no-op. Check `not chat.messages` directly instead. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Code Review
This pull request fixes a potential IndexError in the _run_tools method of ChatWorkflow by checking if chat.messages is empty instead of checking chat.last (which accesses messages[-1]). It also adds a corresponding unit test to verify this behavior and prevent regressions. There are no review comments to address, and I have no additional feedback to provide.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
kevinmessiaen
left a comment
There was a problem hiding this comment.
LGTM, thanks for the fix!
What
ChatWorkflow(...).run()with no messages added crashes withIndexErrorinstead of the intended no-op.The "no tool calls" guard in
_run_tools(libs/giskard-agents/src/giskard/agents/workflow.py) starts with:But
Chat.lastisself.messages[-1], which raisesIndexError: list index out of rangeon an emptymessageslist rather than returning something falsy. Sincemessagesdefaults to[], a workflow that runs before any.chat()call hits this — theIndexErrorpropagates andrun()(default RAISE error policy) wraps it inWorkflowError("Step processing failed")instead of the intended early return.Fix
Guard on
chat.messages(the list) instead ofchat.last(which indexes into it):One-line change in
workflow.py.Testing
Added
test_run_with_no_messages_does_not_raise_indexerrortolibs/giskard-agents/tests/test_workflow_steps.py(mockedBaseGenerator, no real API calls). TDD red confirmed against the unfixed guard (raisesWorkflowErrorwrapping theIndexError), green after the fix.libs/giskard-agents/tests/test_workflow_steps.py(non-functional): 4 passed.ruff check+ruff format --checkclean.xref: no open PR or issue touches
workflow.py,_run_tools,Chat.last, orChatWorkflow.Disclosure: this PR was drafted with AI assistance (Claude); I reviewed, tested, and take responsibility for the change.