Skip to content

Defensive coding for negative indices in tool calls #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions streamaccumulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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
Expand Down Expand Up @@ -169,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
}
Expand Down