Skip to content

fix(azure-ai-inference): guard against IndexError when tool message items list is empty#13678

Open
frankgoldfish wants to merge 1 commit intomicrosoft:mainfrom
frankgoldfish:fix/azure-ai-inference-empty-tool-items
Open

fix(azure-ai-inference): guard against IndexError when tool message items list is empty#13678
frankgoldfish wants to merge 1 commit intomicrosoft:mainfrom
frankgoldfish:fix/azure-ai-inference-empty-tool-items

Conversation

@frankgoldfish
Copy link

Summary

In _format_tool_message within the Azure AI Inference utilities, when a ChatMessageContent with role TOOL has an empty items list, the existing guard only checks len(message.items) != 1 and logs a warning — but then immediately proceeds to access message.items[0], raising an unhandled IndexError.

Bug

def _format_tool_message(message: ChatMessageContent) -> ToolMessage:
    if len(message.items) != 1:
        # This logs a warning for items==0, but does NOT stop execution
        logger.warning("Unsupported number of items ...")

    # IndexError: list index out of range when message.items is empty
    if not isinstance(message.items[0], FunctionResultContent):
        raise ValueError("No FunctionResultContent found in the message items")

When message.items is an empty list, message.items[0] raises IndexError: 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:

def _format_tool_message(message: ChatMessageContent) -> ToolMessage:
    if len(message.items) == 0:
        raise ValueError("Tool message has no items; expected exactly one FunctionResultContent item.")

    if len(message.items) != 1:
        logger.warning("Unsupported number of items ...")

    if not isinstance(message.items[0], FunctionResultContent):
        raise ValueError("No FunctionResultContent found in the message items")

Files Changed

  • python/semantic_kernel/connectors/ai/azure_ai_inference/services/utils.py

Test plan

  • Pass a ChatMessageContent(role=AuthorRole.TOOL, items=[]) to _format_tool_message and confirm it raises ValueError with a clear message instead of IndexError
  • Run existing Azure AI Inference unit tests to verify no regression

🤖 Generated with Claude Code

…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>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_message before indexing message.items[0].
  • Raise a descriptive ValueError when 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.")
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants