@@ -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