Improve cross-model thinking handling and add gemini-3-flash fallback

- Add gemini-3-flash to MODEL_FALLBACK_MAP for completeness
- Add hasGeminiHistory() to detect Gemini→Claude cross-model switch
- Trigger recovery for Claude only when Gemini history detected
- Remove unnecessary thinking block filtering for Claude-only conversations
- Add comments explaining '.' placeholder usage
- Remove unused filterUnsignedThinkingFromMessages function

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Badri Narayanan S
2026-01-04 00:11:14 +05:30
parent 53f8d7f6cc
commit 141558dd62
3 changed files with 40 additions and 15 deletions

View File

@@ -15,6 +15,7 @@ import {
removeTrailingThinkingBlocks,
reorderAssistantContent,
filterUnsignedThinkingBlocks,
hasGeminiHistory,
needsThinkingRecovery,
closeToolLoopForThinking
} from './thinking-utils.js';
@@ -77,15 +78,20 @@ export function convertAnthropicToGoogle(anthropicRequest) {
}
}
// Apply thinking recovery for thinking models when needed
// - Gemini: needs recovery for tool loops/interrupted tools (stripped thinking)
// - Claude: needs recovery ONLY when cross-model (incompatible Gemini signatures will be dropped)
// Apply thinking recovery for Gemini thinking models when needed
// Gemini needs recovery for tool loops/interrupted tools (stripped thinking)
let processedMessages = messages;
const targetFamily = isClaudeModel ? 'claude' : isGeminiModel ? 'gemini' : null;
if (isThinking && targetFamily && needsThinkingRecovery(messages)) {
logger.debug(`[RequestConverter] Applying thinking recovery for ${targetFamily}`);
processedMessages = closeToolLoopForThinking(messages, targetFamily);
if (isGeminiModel && isThinking && needsThinkingRecovery(messages)) {
logger.debug('[RequestConverter] Applying thinking recovery for Gemini');
processedMessages = closeToolLoopForThinking(messages, 'gemini');
}
// For Claude: apply recovery only for cross-model (Gemini→Claude) switch
// Detected by checking if history has Gemini-style tool_use with thoughtSignature
if (isClaudeModel && isThinking && hasGeminiHistory(messages) && needsThinkingRecovery(messages)) {
logger.debug('[RequestConverter] Applying thinking recovery for Claude (cross-model from Gemini)');
processedMessages = closeToolLoopForThinking(messages, 'claude');
}
// Convert messages to contents, then filter unsigned thinking blocks
@@ -108,6 +114,8 @@ export function convertAnthropicToGoogle(anthropicRequest) {
// SAFETY: Google API requires at least one part per content message
// This happens when all thinking blocks are filtered out (unsigned)
if (parts.length === 0) {
// Use '.' instead of '' because claude models reject empty text parts.
// A single period is invisible in practice but satisfies the API requirement.
logger.warn('[RequestConverter] WARNING: Empty parts array after filtering, adding placeholder');
parts.push({ text: '.' });
}

View File

@@ -27,6 +27,21 @@ export function hasValidSignature(part) {
return typeof signature === 'string' && signature.length >= MIN_SIGNATURE_LENGTH;
}
/**
* Check if conversation history contains Gemini-style messages.
* Gemini puts thoughtSignature on tool_use blocks, Claude puts signature on thinking blocks.
* @param {Array<Object>} messages - Array of messages
* @returns {boolean} True if any tool_use has thoughtSignature (Gemini pattern)
*/
export function hasGeminiHistory(messages) {
return messages.some(msg =>
Array.isArray(msg.content) &&
msg.content.some(block =>
block.type === 'tool_use' && block.thoughtSignature !== undefined
)
);
}
/**
* Sanitize a thinking part by keeping only allowed fields
*/
@@ -434,14 +449,14 @@ function stripInvalidThinkingBlocks(messages, targetFamily = null) {
return false;
}
// Check family compatibility if targetFamily is provided
if (targetFamily) {
// Check family compatibility only for Gemini targets
// Claude can validate its own signatures, so we don't drop for Claude
if (targetFamily === 'gemini') {
const signature = block.thought === true ? block.thoughtSignature : block.signature;
const signatureFamily = getCachedSignatureFamily(signature);
// Strict validation: If we don't know the family (cache miss) or it doesn't match,
// we drop it. We don't assume validity for unknown signatures.
if (signatureFamily !== targetFamily) {
// For Gemini: drop unknown or mismatched signatures
if (!signatureFamily || signatureFamily !== targetFamily) {
strippedCount++;
return false;
}
@@ -450,6 +465,7 @@ function stripInvalidThinkingBlocks(messages, targetFamily = null) {
return true;
});
// Use '.' instead of '' because claude models reject empty text parts
if (msg.content) {
return { ...msg, content: filtered.length > 0 ? filtered : [{ type: 'text', text: '.' }] };
} else if (msg.parts) {