Skip to content

fix(claude): prevent index out of range panic when message Content is empty#751

Open
shentongmartin wants to merge 1 commit intomainfrom
fix/claude-empty-content-panic
Open

fix(claude): prevent index out of range panic when message Content is empty#751
shentongmartin wants to merge 1 commit intomainfrom
fix/claude-empty-content-panic

Conversation

@shentongmartin
Copy link
Copy Markdown
Contributor

What type of PR is this?

fix: A bug fix

Check the PR title.

  • This PR title match the format: <type>(optional scope): <description>
  • The description of this PR title is user-oriented and clear enough for others to understand.
  • Attach the PR updating the user documentation if the current PR requires user awareness at the usage level.

(Optional) Translate the PR title into Chinese.

fix(claude): 修复消息 Content 为空时的 index out of range panic

(Optional) More detailed description for this PR(en: English/zh: Chinese).

en:

Problem

When using the Claude model with tool calling (GenerateWithTools), a panic occurs after tool execution completes:

index out of range [-1]

This happens in populateInput at two locations where msgParam.Content[len(msgParam.Content)-1] is accessed without checking if Content is empty.

Root Cause

When convSchemaMessage processes an assistant message that has ToolCalls but no text content (empty Content string), the resulting MessageParam.Content can be empty. The cache control breakpoint detection code then panics trying to access the last element of an empty slice.

Fix

Added len(msgParam.Content) > 0 bounds checks at both locations in populateInput that access the last element of the Content slice:

  1. Line 598: Checking for existing cache control breakpoints during message iteration
  2. Line 607: Setting auto cache control on the last message's last content block

Key Insight

The Anthropic Claude API allows assistant messages that contain only tool use blocks with no text content. This is a normal flow in agent/tool-calling scenarios where the model decides to call a tool without providing any accompanying text. The cache control logic assumed every message would have at least one content block, which is not true in this case.

Test

Added a test case that verifies genMessageNewParams does not panic when processing a conversation with an assistant message containing tool calls but empty text content, followed by a tool result message.

zh:

问题

使用 Claude 模型进行工具调用(GenerateWithTools)时,在工具执行完成后会发生 panic:index out of range [-1]。这发生在 populateInput 中访问 msgParam.Content[len(msgParam.Content)-1] 时未检查 Content 是否为空的两个位置。

根本原因

convSchemaMessage 处理一个有 ToolCalls 但没有文本内容(Content 为空字符串)的 assistant 消息时,生成的 MessageParam.Content 可能为空。缓存控制断点检测代码随后在尝试访问空切片的最后一个元素时发生 panic。

修复

populateInput 中访问 Content 切片最后一个元素的两个位置添加了 len(msgParam.Content) > 0 的边界检查。

测试

添加了一个测试用例,验证处理包含仅有工具调用但无文本内容的 assistant 消息的对话时不会 panic。

(Optional) Which issue(s) this PR fixes:

Fixes #749

@github-actions
Copy link
Copy Markdown

Need to create a new tag

The following modules have changes and may need version updates:

  • components/model/claude (Current: components/model/claude/v0.1.16)

⚠️ Please create and push new version tags for these modules after merging this PR.

… empty

Add length checks before accessing Content slice in populateInput to prevent
panic when assistant messages have tool calls but no text content.

Closes #749

Change-Id: I36fa4764d061e92ed2f9182ff653dbdda5506c3d
@shentongmartin shentongmartin force-pushed the fix/claude-empty-content-panic branch from c6ab4dd to 7a5445d Compare March 31, 2026 02:36
@github-actions
Copy link
Copy Markdown

Need to create a new tag

The following modules have changes and may need version updates:

  • components/model/claude (Current: components/model/claude/v0.1.16)

⚠️ Please create and push new version tags for these modules after merging this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant