From 1246d672e95fbca7c0d0fb70391e621352a6de98 Mon Sep 17 00:00:00 2001 From: Stefan Date: Thu, 7 Aug 2025 17:37:58 +0100 Subject: [PATCH 1/2] Defensive coding for negative indices in tool calls --- streamaccumulator.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/streamaccumulator.go b/streamaccumulator.go index 53b7069f..f1b2523e 100644 --- a/streamaccumulator.go +++ b/streamaccumulator.go @@ -125,8 +125,14 @@ func (cc *ChatCompletion) accumulateDelta(chunk ChatCompletionChunk) bool { for j := range delta.Delta.ToolCalls { deltaTool := &delta.Delta.ToolCalls[j] - choice.Message.ToolCalls = expandToFit(choice.Message.ToolCalls, int(deltaTool.Index)) - tool := &choice.Message.ToolCalls[deltaTool.Index] + // Handle negative tool call indices (sometimes -1 from Bedrock) + toolIndex := int(deltaTool.Index) + if toolIndex < 0 { + toolIndex = 0 // Default to 0 for negative indices + } + + choice.Message.ToolCalls = expandToFit(choice.Message.ToolCalls, toolIndex) + tool := &choice.Message.ToolCalls[toolIndex] if deltaTool.ID != "" { tool.ID = deltaTool.ID From 8b0402ff668f2d460f8bafc636d0426fa0f48a3a Mon Sep 17 00:00:00 2001 From: Stefan Date: Fri, 8 Aug 2025 13:51:51 +0100 Subject: [PATCH 2/2] More --- streamaccumulator.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/streamaccumulator.go b/streamaccumulator.go index f1b2523e..95e6e91d 100644 --- a/streamaccumulator.go +++ b/streamaccumulator.go @@ -125,7 +125,7 @@ func (cc *ChatCompletion) accumulateDelta(chunk ChatCompletionChunk) bool { for j := range delta.Delta.ToolCalls { deltaTool := &delta.Delta.ToolCalls[j] - // Handle negative tool call indices (sometimes -1 from Bedrock) + // Handle negative tool call indices (sometimes -1) toolIndex := int(deltaTool.Index) if toolIndex < 0 { toolIndex = 0 // Default to 0 for negative indices @@ -175,7 +175,14 @@ func (prev *chatCompletionResponseState) update(chunk ChatCompletionChunk) (just new.state = refusalResponseState case delta.JSON.ToolCalls.Valid(): new.state = toolResponseState - new.index = int(delta.ToolCalls[0].Index) + // Handle negative tool call indices (sometimes -1) + if len(delta.ToolCalls) > 0 { + index := int(delta.ToolCalls[0].Index) + if index < 0 { + index = 0 // Default to 0 for negative indices + } + new.index = index + } default: new.state = finishedResponseState }