fix(claude): prevent index out of range panic when message Content is empty#751
Open
shentongmartin wants to merge 1 commit intomainfrom
Open
fix(claude): prevent index out of range panic when message Content is empty#751shentongmartin wants to merge 1 commit intomainfrom
shentongmartin wants to merge 1 commit intomainfrom
Conversation
Need to create a new tagThe following modules have changes and may need version updates:
|
… 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
c6ab4dd to
7a5445d
Compare
Need to create a new tagThe following modules have changes and may need version updates:
|
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.
What type of PR is this?
fix: A bug fix
Check the PR title.
(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:This happens in
populateInputat two locations wheremsgParam.Content[len(msgParam.Content)-1]is accessed without checking ifContentis empty.Root Cause
When
convSchemaMessageprocesses an assistant message that hasToolCallsbut no text content (emptyContentstring), the resultingMessageParam.Contentcan 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) > 0bounds checks at both locations inpopulateInputthat access the last element of theContentslice: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
genMessageNewParamsdoes 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