@@ -100,6 +100,7 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
100100
101101 // Process the main content part of the response.
102102 partsResult := gjson .GetBytes (rawJSON , "candidates.0.content.parts" )
103+ hasFunctionCall := false
103104 if partsResult .IsArray () {
104105 partResults := partsResult .Array ()
105106 for i := 0 ; i < len (partResults ); i ++ {
@@ -121,6 +122,7 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
121122 template , _ = sjson .Set (template , "choices.0.delta.role" , "assistant" )
122123 } else if functionCallResult .Exists () {
123124 // Handle function call content.
125+ hasFunctionCall = true
124126 toolCallsResult := gjson .Get (template , "choices.0.delta.tool_calls" )
125127 functionCallIndex := (* param ).(* convertGeminiResponseToOpenAIChatParams ).FunctionIndex
126128 (* param ).(* convertGeminiResponseToOpenAIChatParams ).FunctionIndex ++
@@ -172,6 +174,11 @@ func ConvertGeminiResponseToOpenAI(_ context.Context, _ string, originalRequestR
172174 }
173175 }
174176
177+ if hasFunctionCall {
178+ template , _ = sjson .Set (template , "choices.0.finish_reason" , "tool_calls" )
179+ template , _ = sjson .Set (template , "choices.0.native_finish_reason" , "tool_calls" )
180+ }
181+
175182 return []string {template }
176183}
177184
@@ -231,6 +238,7 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
231238
232239 // Process the main content part of the response.
233240 partsResult := gjson .GetBytes (rawJSON , "candidates.0.content.parts" )
241+ hasFunctionCall := false
234242 if partsResult .IsArray () {
235243 partsResults := partsResult .Array ()
236244 for i := 0 ; i < len (partsResults ); i ++ {
@@ -252,6 +260,7 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
252260 template , _ = sjson .Set (template , "choices.0.message.role" , "assistant" )
253261 } else if functionCallResult .Exists () {
254262 // Append function call content to the tool_calls array.
263+ hasFunctionCall = true
255264 toolCallsResult := gjson .Get (template , "choices.0.message.tool_calls" )
256265 if ! toolCallsResult .Exists () || ! toolCallsResult .IsArray () {
257266 template , _ = sjson .SetRaw (template , "choices.0.message.tool_calls" , `[]` )
@@ -297,5 +306,10 @@ func ConvertGeminiResponseToOpenAINonStream(_ context.Context, _ string, origina
297306 }
298307 }
299308
309+ if hasFunctionCall {
310+ template , _ = sjson .Set (template , "choices.0.finish_reason" , "tool_calls" )
311+ template , _ = sjson .Set (template , "choices.0.native_finish_reason" , "tool_calls" )
312+ }
313+
300314 return template
301315}
0 commit comments