File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments