Skip to content

Commit 243bf5c

Browse files
committed
feat: enhance tool call handling in OpenAI response conversion
1 parent 3569e57 commit 243bf5c

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

internal/translator/openai/gemini/openai_gemini_response.go

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,46 @@ func ConvertOpenAIResponseToGemini(_ context.Context, _ string, originalRequestR
141141
toolIndex := int(toolCall.Get("index").Int())
142142
toolID := toolCall.Get("id").String()
143143
toolType := toolCall.Get("type").String()
144+
function := toolCall.Get("function")
144145

145-
if toolType == "function" {
146-
function := toolCall.Get("function")
147-
functionName := function.Get("name").String()
148-
functionArgs := function.Get("arguments").String()
146+
// Skip non-function tool calls explicitly marked as other types.
147+
if toolType != "" && toolType != "function" {
148+
return true
149+
}
149150

150-
// Initialize accumulator if needed
151-
if _, exists := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]; !exists {
152-
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex] = &ToolCallAccumulator{
153-
ID: toolID,
154-
Name: functionName,
155-
}
156-
}
151+
// OpenAI streaming deltas may omit the type field while still carrying function data.
152+
if !function.Exists() {
153+
return true
154+
}
157155

158-
// Update ID if provided
159-
if toolID != "" {
160-
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].ID = toolID
161-
}
156+
functionName := function.Get("name").String()
157+
functionArgs := function.Get("arguments").String()
162158

163-
// Update name if provided
164-
if functionName != "" {
165-
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].Name = functionName
159+
// Initialize accumulator if needed so later deltas without type can append arguments.
160+
if _, exists := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]; !exists {
161+
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex] = &ToolCallAccumulator{
162+
ID: toolID,
163+
Name: functionName,
166164
}
165+
}
167166

168-
// Accumulate arguments
169-
if functionArgs != "" {
170-
(*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex].Arguments.WriteString(functionArgs)
171-
}
167+
acc := (*param).(*ConvertOpenAIResponseToGeminiParams).ToolCallsAccumulator[toolIndex]
168+
169+
// Update ID if provided
170+
if toolID != "" {
171+
acc.ID = toolID
172+
}
173+
174+
// Update name if provided
175+
if functionName != "" {
176+
acc.Name = functionName
172177
}
178+
179+
// Accumulate arguments
180+
if functionArgs != "" {
181+
acc.Arguments.WriteString(functionArgs)
182+
}
183+
173184
return true
174185
})
175186

0 commit comments

Comments
 (0)