Fix Claude via Google APIs requiring tool call IDs (#653)

Claude models accessed through Google Cloud Code Assist API require
explicit id fields in both functionCall and functionResponse parts.
Without these IDs, the API returns 'tool_use.id: Field required' error.

Add requiresToolCallId() helper to centralize the Claude model detection
and include IDs in both tool call and tool result message conversions.
This commit is contained in:
Danila Poyarkov 2026-01-12 18:40:07 +03:00 committed by GitHub
parent 934e7e470b
commit 7a41975e9e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -58,6 +58,13 @@ function resolveThoughtSignature(isSameProviderAndModel: boolean, signature: str
return isSameProviderAndModel && isValidThoughtSignature(signature) ? signature : undefined;
}
/**
* Claude models via Google APIs require explicit tool call IDs in function calls/responses.
*/
export function requiresToolCallId(modelId: string): boolean {
return modelId.startsWith("claude-");
}
/**
* Convert internal messages to Gemini Content[] format.
*/
@ -128,6 +135,7 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
functionCall: {
name: block.name,
args: block.arguments,
...(requiresToolCallId(model.id) ? { id: block.id } : {}),
},
};
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.thoughtSignature);
@ -169,12 +177,14 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
},
}));
const includeId = requiresToolCallId(model.id);
const functionResponsePart: Part = {
functionResponse: {
name: msg.toolName,
response: msg.isError ? { error: responseValue } : { output: responseValue },
// Nest images inside functionResponse.parts for Gemini 3
...(hasImages && supportsMultimodalFunctionResponse && { parts: imageParts }),
...(includeId ? { id: msg.toolCallId } : {}),
},
};