fix(azure-ai-inference): guard against IndexError when tool message items list is empty#13678
Open
frankgoldfish wants to merge 1 commit intomicrosoft:mainfrom
Open
Conversation
…as no items In `_format_tool_message`, when a `ChatMessageContent` with role TOOL has an empty `items` list, the function logs a warning for `len(message.items) != 1` but then immediately attempts `message.items[0]`, raising an unhandled `IndexError`. Add an explicit check for the empty-items case and raise a descriptive `ValueError` before the existing length-mismatch warning, so callers receive a clear error message instead of a confusing `IndexError: list index out of range`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR prevents an unhandled IndexError when formatting Azure AI Inference tool messages whose ChatMessageContent.items list is empty, by adding an explicit early validation and raising a clearer ValueError.
Changes:
- Add an explicit empty list guard in
_format_tool_messagebefore indexingmessage.items[0]. - Raise a descriptive
ValueErrorwhen a TOOL message has zero items.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+137
to
+138
| if len(message.items) == 0: | ||
| raise ValueError("Tool message has no items; expected exactly one FunctionResultContent item.") |
There was a problem hiding this comment.
Add a unit test covering the new empty-items guard (TOOL ChatMessageContent with items=[]), asserting a ValueError is raised and the message is descriptive. This prevents regressions back to an IndexError and locks in the intended error behavior.
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
In
_format_tool_messagewithin the Azure AI Inference utilities, when aChatMessageContentwith roleTOOLhas an emptyitemslist, the existing guard only checkslen(message.items) != 1and logs a warning — but then immediately proceeds to accessmessage.items[0], raising an unhandledIndexError.Bug
When
message.itemsis an empty list,message.items[0]raisesIndexError: list index out of range, which is an opaque error that obscures the real problem.Fix
Add an explicit guard for the empty-items case before the length-mismatch warning, raising a descriptive
ValueError:Files Changed
python/semantic_kernel/connectors/ai/azure_ai_inference/services/utils.pyTest plan
ChatMessageContent(role=AuthorRole.TOOL, items=[])to_format_tool_messageand confirm it raisesValueErrorwith a clear message instead ofIndexError🤖 Generated with Claude Code