Self-hosted AI workspace.
Python
fix: skip malformed document tool call items (#3494)
Rohith Matam36b2e7cc5901
diff --git a/src/tool_schemas.py b/src/tool_schemas.pyindex 750b60b50..562b34973 100644--- a/src/tool_schemas.py+++ b/src/tool_schemas.py@@ -1258,14 +1258,24 @@ def function_call_to_tool_block(name: str, arguments: str) -> Optional[ToolBlockcontent = "\n".join(parts)elif tool_type == "edit_document":blocks = []- for edit in args.get("edits", []):+ edits = args.get("edits", [])+ if not isinstance(edits, list):+ edits = []+ for edit in edits:+ if not isinstance(edit, dict):+ continueblocks.append(f'<<<FIND>>>\n{edit.get("find", "")}\n<<<REPLACE>>>\n{edit.get("replace", "")}\n<<<END>>>')content = "\n".join(blocks)elif tool_type == "suggest_document":blocks = []- for s in args.get("suggestions", []):+ suggestions = args.get("suggestions", [])+ if not isinstance(suggestions, list):+ suggestions = []+ for s in suggestions:+ if not isinstance(s, dict):+ continueblocks.append(f'<<<FIND>>>\n{s.get("find", "")}\n<<<SUGGEST>>>\n{s.get("replace", "")}\n<<<REASON>>>\n{s.get("reason", "")}\n<<<END>>>')diff --git a/tests/test_function_call_non_object_args.py b/tests/test_function_call_non_object_args.pyindex a3ea9956d..5e8cf4675 100644--- a/tests/test_function_call_non_object_args.py+++ b/tests/test_function_call_non_object_args.py@@ -35,3 +35,27 @@ def test_non_object_arguments_do_not_crash(arguments):assert block is not Noneassert block.tool_type == "bash"assert block.content == ""+++def test_edit_document_skips_non_object_edit_items():+ block = function_call_to_tool_block(+ "edit_document",+ '{"edits": ["bad", 42, null, {"find": "old", "replace": "new"}]}',+ )++ assert block is not None+ assert block.tool_type == "edit_document"+ assert block.content == "<<<FIND>>>\nold\n<<<REPLACE>>>\nnew\n<<<END>>>"+++def test_suggest_document_skips_non_object_suggestion_items():+ block = function_call_to_tool_block(+ "suggest_document",+ '{"suggestions": ["bad", 42, null, {"find": "old", "replace": "new", "reason": "clearer"}]}',+ )++ assert block is not None+ assert block.tool_type == "suggest_document"+ assert block.content == (+ "<<<FIND>>>\nold\n<<<SUGGEST>>>\nnew\n<<<REASON>>>\nclearer\n<<<END>>>"+ )