AddThoughtSignaturesToLatestTurn patches only the current turn because completed tool-call/result pairs from earlier turns are not validated by Google as active function calls.
(payload map[string]any)
| 93 | // completed tool-call/result pairs from earlier turns are not validated by |
| 94 | // Google as active function calls. |
| 95 | func AddThoughtSignaturesToLatestTurn(payload map[string]any) bool { |
| 96 | messages, ok := payload["messages"].([]any) |
| 97 | if !ok { |
| 98 | return false |
| 99 | } |
| 100 | |
| 101 | currentTurnStart := -1 |
| 102 | for i, raw := range messages { |
| 103 | message, ok := raw.(map[string]any) |
| 104 | if !ok { |
| 105 | continue |
| 106 | } |
| 107 | if role, _ := message["role"].(string); role == "user" { |
| 108 | currentTurnStart = i |
| 109 | } |
| 110 | } |
| 111 | if currentTurnStart == -1 { |
| 112 | return false |
| 113 | } |
| 114 | |
| 115 | changed := false |
| 116 | for _, raw := range messages[currentTurnStart+1:] { |
| 117 | message, ok := raw.(map[string]any) |
| 118 | if !ok || !isAssistantRole(message["role"]) { |
| 119 | continue |
| 120 | } |
| 121 | toolCalls, ok := message["tool_calls"].([]any) |
| 122 | if !ok || len(toolCalls) == 0 { |
| 123 | continue |
| 124 | } |
| 125 | // Every tool call in parallel batches needs a signature, |
| 126 | // not just the first one. |
| 127 | for _, rawToolCall := range toolCalls { |
| 128 | toolCall, ok := rawToolCall.(map[string]any) |
| 129 | if !ok { |
| 130 | continue |
| 131 | } |
| 132 | if ensureThoughtSignature(toolCall) { |
| 133 | changed = true |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | return changed |
| 138 | } |
| 139 | |
| 140 | // Gemini can serialize assistant messages with its native "model" role. |
| 141 | func isAssistantRole(role any) bool { |