fix: sanitize Read tool args to prevent retry loops from non-Anthropic models (#1144)

* fix: sanitize Read tool args to prevent retry loops from non-Anthropic models

* fix: sanitize invalid Read pages from tool args

Non-Anthropic models sometimes emit optional Read args like pages: "" for
non-PDF files, which Claude Code rejects before the tool runs. Drop invalid
pages values, keep valid PDF page ranges, and coerce numeric string bounds
before clamping limit/offset.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
GodrezJr2
2026-05-26 11:33:38 +07:00
committed by GitHub
co-authored by Claude Sonnet 4.6
parent dcc46f2eee
commit 96a9a2b36d
2 changed files with 114 additions and 28 deletions
@@ -4,21 +4,40 @@ import { FORMATS } from "../formats.js";
// Prefix for Claude OAuth tool names (must match request translator) // Prefix for Claude OAuth tool names (must match request translator)
const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_"; const CLAUDE_OAUTH_TOOL_PREFIX = "proxy_";
// Strip optional empty-string tool arguments that some providers emit. // Sanitize tool call arguments to fix bad params from non-Anthropic models
// Claude Code's Read tool rejects pages: "" but accepts pages being absent. function sanitizeToolArgs(toolName, argsJson) {
function sanitizeToolArguments(toolName, argsJson) {
try { try {
const args = JSON.parse(argsJson); const args = JSON.parse(argsJson);
if (typeof args === "object" && args !== null) { const name = toolName.startsWith(CLAUDE_OAUTH_TOOL_PREFIX)
if (toolName === "Read" && args.pages === "") { ? toolName.slice(CLAUDE_OAUTH_TOOL_PREFIX.length)
: toolName;
if (name === "Read") sanitizeReadArgs(args);
return JSON.stringify(args);
} catch {
return argsJson;
}
}
function sanitizeReadArgs(args) {
if (typeof args.limit === "string" && /^\d+$/.test(args.limit)) args.limit = Number(args.limit);
if (typeof args.offset === "string" && /^-?\d+$/.test(args.offset)) args.offset = Number(args.offset);
if (typeof args.limit === "number") {
if (args.limit > 2000) args.limit = 2000;
if (args.limit < 1) delete args.limit;
}
if (typeof args.offset === "number" && args.offset < 0) args.offset = 0;
if ("pages" in args && !isValidPdfPagesArg(args.file_path, args.pages)) {
delete args.pages; delete args.pages;
} }
return JSON.stringify(args); }
}
} catch { function isValidPdfPagesArg(filePath, pages) {
// Not valid JSON yet (streaming chunk) — return as-is return typeof filePath === "string" &&
} filePath.toLowerCase().endsWith(".pdf") &&
return argsJson; typeof pages === "string" &&
/^\d+(?:-\d+)?$/.test(pages);
} }
// Helper: stop thinking block if started // Helper: stop thinking block if started
@@ -187,12 +206,9 @@ export function openaiToClaudeResponse(chunk, state) {
if (tc.function?.arguments) { if (tc.function?.arguments) {
const toolInfo = state.toolCalls.get(idx); const toolInfo = state.toolCalls.get(idx);
if (toolInfo) { if (toolInfo) {
const sanitized = sanitizeToolArguments(toolInfo.name, tc.function.arguments); // Buffer args instead of streaming — sanitize at finish to fix bad params
results.push({ if (!state.toolArgBuffers) state.toolArgBuffers = new Map();
type: "content_block_delta", state.toolArgBuffers.set(idx, (state.toolArgBuffers.get(idx) || "") + tc.function.arguments);
index: toolInfo.blockIndex,
delta: { type: "input_json_delta", partial_json: sanitized }
});
} }
} }
} }
@@ -203,7 +219,17 @@ export function openaiToClaudeResponse(chunk, state) {
stopThinkingBlock(state, results); stopThinkingBlock(state, results);
stopTextBlock(state, results); stopTextBlock(state, results);
for (const [, toolInfo] of state.toolCalls) { for (const [idx, toolInfo] of state.toolCalls) {
// Emit buffered + sanitized args as single delta before stop
const buffered = state.toolArgBuffers?.get(idx);
if (buffered) {
const sanitized = sanitizeToolArgs(toolInfo.name, buffered);
results.push({
type: "content_block_delta",
index: toolInfo.blockIndex,
delta: { type: "input_json_delta", partial_json: sanitized }
});
}
results.push({ results.push({
type: "content_block_stop", type: "content_block_stop",
index: toolInfo.blockIndex index: toolInfo.blockIndex
@@ -238,4 +264,3 @@ function convertFinishReason(reason) {
// Register // Register
register(FORMATS.OPENAI, FORMATS.CLAUDE, null, openaiToClaudeResponse); register(FORMATS.OPENAI, FORMATS.CLAUDE, null, openaiToClaudeResponse);
@@ -0,0 +1,61 @@
import { describe, expect, it } from "vitest";
import { openaiToClaudeResponse } from "../../open-sse/translator/response/openai-to-claude.js";
function createState() {
return { toolCalls: new Map(), nextBlockIndex: 0 };
}
function getInputJsonDelta(events) {
return events.find((event) => event.type === "content_block_delta" && event.delta?.type === "input_json_delta")?.delta.partial_json;
}
describe("openaiToClaudeResponse tool argument sanitization", () => {
it("drops invalid Read pages and clamps numeric bounds", () => {
const state = createState();
openaiToClaudeResponse({
id: "chatcmpl-test-read",
model: "test-model",
choices: [{ delta: { tool_calls: [{ index: 0, id: "toolu_read", function: { name: "Read" } }] } }],
}, state);
const events = openaiToClaudeResponse({
id: "chatcmpl-test-read",
model: "test-model",
choices: [{
delta: { tool_calls: [{ index: 0, function: { arguments: JSON.stringify({ file_path: "F:/repo/file.js", offset: -5, limit: 999999999, pages: "" }) } }] },
finish_reason: "tool_calls",
}],
}, state);
expect(JSON.parse(getInputJsonDelta(events))).toEqual({
file_path: "F:/repo/file.js",
offset: 0,
limit: 2000,
});
});
it("keeps valid PDF pages", () => {
const state = createState();
openaiToClaudeResponse({
id: "chatcmpl-test-pdf",
model: "test-model",
choices: [{ delta: { tool_calls: [{ index: 0, id: "toolu_pdf", function: { name: "proxy_Read" } }] } }],
}, state);
const events = openaiToClaudeResponse({
id: "chatcmpl-test-pdf",
model: "test-model",
choices: [{
delta: { tool_calls: [{ index: 0, function: { arguments: JSON.stringify({ file_path: "F:/repo/doc.pdf", pages: "1-3" }) } }] },
finish_reason: "tool_calls",
}],
}, state);
expect(JSON.parse(getInputJsonDelta(events))).toEqual({
file_path: "F:/repo/doc.pdf",
pages: "1-3",
});
});
});