Skip to content

Commit a5b3fdc

Browse files
authored
Merge pull request #193 from diegosouzapw/fix/issue-191-empty-tool-use-name
fix: validate empty tool_use.name to prevent Claude 400 errors (#191)
2 parents dbffb18 + 47ddb28 commit a5b3fdc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

open-sse/translator/helpers/claudeHelper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,15 @@ export function prepareClaudeRequest(body, provider = null) {
126126
}
127127
}
128128

129+
// Pass 1.4: Filter out tool_use blocks with empty names (causes Claude 400 error)
130+
for (const msg of filtered) {
131+
if (msg.role === "assistant" && Array.isArray(msg.content)) {
132+
msg.content = msg.content.filter(
133+
(block) => block.type !== "tool_use" || (block.name && block.name.trim())
134+
);
135+
}
136+
}
137+
129138
// Pass 1.5: Fix tool_use/tool_result ordering
130139
// Each tool_use must have tool_result in the NEXT message (not same message with other content)
131140
filtered = fixToolUseOrdering(filtered);

open-sse/translator/request/openai-to-claude.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ function getContentBlocksFromMessage(msg, toolNameMap = new Map()) {
228228
});
229229
} else if (part.type === "tool_use") {
230230
// Tool name already has prefix from tool declarations, keep as-is
231-
blocks.push({ type: "tool_use", id: part.id, name: part.name, input: part.input });
231+
// CRITICAL: Skip tool_use blocks with empty name (causes Claude 400 error)
232+
if (part.name && part.name.trim()) {
233+
blocks.push({ type: "tool_use", id: part.id, name: part.name, input: part.input });
234+
}
232235
}
233236
}
234237
} else if (msg.content) {
@@ -241,8 +244,12 @@ function getContentBlocksFromMessage(msg, toolNameMap = new Map()) {
241244
if (msg.tool_calls && Array.isArray(msg.tool_calls)) {
242245
for (const tc of msg.tool_calls) {
243246
if (tc.type === "function") {
247+
// CRITICAL: Skip tool_calls with empty function name (causes Claude 400 error)
248+
const fnName = tc.function?.name;
249+
if (!fnName || !fnName.trim()) continue;
250+
244251
// Apply prefix to tool name
245-
const toolName = CLAUDE_OAUTH_TOOL_PREFIX + tc.function.name;
252+
const toolName = CLAUDE_OAUTH_TOOL_PREFIX + fnName;
246253
blocks.push({
247254
type: "tool_use",
248255
id: tc.id,

0 commit comments

Comments
 (0)