Skip to content

panic: index out of range [-1] at claude.go:598 when tool result has empty content #749

@weibaohui

Description

@weibaohui

Bug Description

When using Claude model with tool calling (via GenerateWithTools), a panic occurs after tool execution completes: index out of range [-1] at claude.go:598.

Environment

  • eino-ext/components/model/claude: v0.1.16
  • Go: 1.25+
  • Backend: TaskManager with Agent tool calling

Steps to Reproduce

  1. Configure Claude provider with tool calling enabled
  2. Send a message that triggers tool calling
  3. After tool execution returns (with JSON output), the LLM call crashes

Root Cause Analysis

In claude.go:598, the code accesses msgParam.Content[len(msgParam.Content)-1] without checking if Content is empty:

if ctrl := msgParam.Content[len(msgParam.Content)-1].GetCacheControl(); ctrl != nil && ctrl.Type != "" {
    hasSetMsgBreakPoint = true
}

When convSchemaMessage processes a message that only contains tool calls (no text content), the returned msgParam.Content can be empty. This causes an index out of range panic when the code tries to access the last element.

Suggested Fix

Add a length check before accessing Content:

if len(msgParam.Content) > 0 {
    if ctrl := msgParam.Content[len(msgParam.Content)-1].GetCacheControl(); ctrl != nil && ctrl.Type != "" {
        hasSetMsgBreakPoint = true
    }
}

Additional Context

This affects tool calling scenarios where the Assistant message has ToolCalls but empty Content, and the subsequent Tool message returns a result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions