Skip to content

Commit 46c25c7

Browse files
authored
refactor(openai): simplify tool call handling in streaming response (#415)
Signed-off-by: Tommy Nguyen <[email protected]>
1 parent de4e021 commit 46c25c7

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

gollm/openai.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,23 +342,26 @@ func (cs *openAIChatSession) SendStreaming(ctx context.Context, contents ...any)
342342
}
343343

344344
// Handle tool call completion
345+
var toolCallsForThisChunk []openai.ChatCompletionMessageToolCall
345346
if tool, ok := acc.JustFinishedToolCall(); ok {
346347
klog.V(2).Infof("Tool call finished: %s %s", tool.Name, tool.Arguments)
347-
currentToolCalls = append(currentToolCalls, openai.ChatCompletionMessageToolCall{
348+
newToolCall := openai.ChatCompletionMessageToolCall{
348349
ID: tool.ID,
349350
Function: openai.ChatCompletionMessageToolCallFunction{
350351
Name: tool.Name,
351352
Arguments: tool.Arguments,
352353
},
353-
})
354+
}
355+
currentToolCalls = append(currentToolCalls, newToolCall)
356+
// Only include the newly finished tool call in this chunk
357+
toolCallsForThisChunk = []openai.ChatCompletionMessageToolCall{newToolCall}
354358
}
355359

356-
// Create a streaming response with proper nil checks
357360
streamResponse := &openAIChatStreamResponse{
358361
streamChunk: chunk,
359362
accumulator: acc,
360363
content: "", // Default to empty content
361-
toolCalls: currentToolCalls,
364+
toolCalls: toolCallsForThisChunk,
362365
}
363366

364367
// Only process content if there are choices and a delta

0 commit comments

Comments
 (0)