fix(gemini): preserve thought_signature in tool calls to prevent 400 errors

This commit is contained in:
mrbeandev
2026-02-16 17:40:23 +05:30
parent 920e30a241
commit 33915fb712
3 changed files with 24 additions and 17 deletions
+8 -2
View File
@@ -624,12 +624,18 @@ func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.M
}
for _, tc := range response.ToolCalls {
argumentsJSON, _ := json.Marshal(tc.Arguments)
thoughtSignature := ""
if tc.Function != nil {
thoughtSignature = tc.Function.ThoughtSignature
}
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
ID: tc.ID,
Type: "function",
Function: &providers.FunctionCall{
Name: tc.Name,
Arguments: string(argumentsJSON),
Name: tc.Name,
Arguments: string(argumentsJSON),
ThoughtSignature: thoughtSignature,
},
})
}
+13 -13
View File
@@ -132,8 +132,9 @@ func (p *HTTPProvider) parseResponse(body []byte) (*LLMResponse, error) {
ID string `json:"id"`
Type string `json:"type"`
Function *struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
Name string `json:"name"`
Arguments string `json:"arguments"`
ThoughtSignature string `json:"thought_signature"`
} `json:"function"`
} `json:"tool_calls"`
} `json:"message"`
@@ -159,18 +160,11 @@ func (p *HTTPProvider) parseResponse(body []byte) (*LLMResponse, error) {
for _, tc := range choice.Message.ToolCalls {
arguments := make(map[string]interface{})
name := ""
thoughtSignature := ""
// Handle OpenAI format with nested function object
if tc.Type == "function" && tc.Function != nil {
name = tc.Function.Name
if tc.Function.Arguments != "" {
if err := json.Unmarshal([]byte(tc.Function.Arguments), &arguments); err != nil {
arguments["raw"] = tc.Function.Arguments
}
}
} else if tc.Function != nil {
// Legacy format without type field
if tc.Function != nil {
name = tc.Function.Name
thoughtSignature = tc.Function.ThoughtSignature
if tc.Function.Arguments != "" {
if err := json.Unmarshal([]byte(tc.Function.Arguments), &arguments); err != nil {
arguments["raw"] = tc.Function.Arguments
@@ -179,7 +173,13 @@ func (p *HTTPProvider) parseResponse(body []byte) (*LLMResponse, error) {
}
toolCalls = append(toolCalls, ToolCall{
ID: tc.ID,
ID: tc.ID,
Type: tc.Type,
Function: &FunctionCall{
Name: name,
Arguments: tc.Function.Arguments,
ThoughtSignature: thoughtSignature,
},
Name: name,
Arguments: arguments,
})
+3 -2
View File
@@ -11,8 +11,9 @@ type ToolCall struct {
}
type FunctionCall struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
Name string `json:"name"`
Arguments string `json:"arguments"`
ThoughtSignature string `json:"thought_signature,omitempty"`
}
type LLMResponse struct {