Skip to content

Commit a60f51e

Browse files
committed
fix(continue-watsonx-ai-provider): handle double-encoded JSON in tool call arguments
1 parent 1bff037 commit a60f51e

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

src/api/providers/ibm-watsonx.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,28 @@ export class WatsonxAIHandler extends BaseProvider implements SingleCompletionHa
173173
if (message.tool_calls && message.tool_calls.length > 0) {
174174
for (const toolCall of message.tool_calls) {
175175
if (toolCall.type === "function") {
176+
let args = toolCall.function.arguments
177+
178+
// Fix double-encoded JSON from certain models (e.g., granite-4-h-small)
179+
// They return arguments as: "\"{\\n \\\"path\\\": \\\"value\\\"\\n}\""
180+
// instead of: "{\"path\": \"value\"}"
181+
try {
182+
// Try to parse once - if it's a string, it might be double-encoded
183+
const parsed = JSON.parse(args)
184+
if (typeof parsed === "string") {
185+
// It's double-encoded, use the parsed string (which is the correct JSON string)
186+
args = parsed
187+
console.log("[IBM watsonx] Fixed double-encoded tool arguments")
188+
}
189+
} catch (e) {
190+
// Not valid JSON or already correct format, keep original
191+
}
192+
176193
yield {
177194
type: "tool_call",
178195
id: toolCall.id,
179196
name: toolCall.function.name,
180-
arguments: toolCall.function.arguments,
197+
arguments: args,
181198
}
182199
}
183200
}

0 commit comments

Comments
 (0)