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:
@@ -58,6 +58,13 @@ function resolveThoughtSignature(isSameProviderAndModel: boolean, signature: str
|
|||||||
return isSameProviderAndModel && isValidThoughtSignature(signature) ? signature : undefined;
|
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.
|
* Convert internal messages to Gemini Content[] format.
|
||||||
*/
|
*/
|
||||||
@@ -128,6 +135,7 @@ export function convertMessages<T extends GoogleApiType>(model: Model<T>, contex
|
|||||||
functionCall: {
|
functionCall: {
|
||||||
name: block.name,
|
name: block.name,
|
||||||
args: block.arguments,
|
args: block.arguments,
|
||||||
|
...(requiresToolCallId(model.id) ? { id: block.id } : {}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.thoughtSignature);
|
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 = {
|
const functionResponsePart: Part = {
|
||||||
functionResponse: {
|
functionResponse: {
|
||||||
name: msg.toolName,
|
name: msg.toolName,
|
||||||
response: msg.isError ? { error: responseValue } : { output: responseValue },
|
response: msg.isError ? { error: responseValue } : { output: responseValue },
|
||||||
// Nest images inside functionResponse.parts for Gemini 3
|
// Nest images inside functionResponse.parts for Gemini 3
|
||||||
...(hasImages && supportsMultimodalFunctionResponse && { parts: imageParts }),
|
...(hasImages && supportsMultimodalFunctionResponse && { parts: imageParts }),
|
||||||
|
...(includeId ? { id: msg.toolCallId } : {}),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user