@@ -299,31 +299,44 @@ def _convert_call_tool_result(call_result: dict[str, Any]) -> ToolResult:
299299 for block in call_result ["content" ]:
300300 block_type = block .get ("type" )
301301 if block_type == "text" :
302- text_parts .append (block .get ("text" , "" ))
302+ text = block .get ("text" , "" )
303+ if isinstance (text , str ):
304+ text_parts .append (text )
303305 elif block_type == "image" :
304- binary_results .append (
305- ToolBinaryResult (
306- data = block .get ("data" , "" ),
307- mime_type = block .get ("mimeType" , "" ),
308- type = "image" ,
306+ data = block .get ("data" , "" )
307+ mime_type = block .get ("mimeType" , "" )
308+ if isinstance (data , str ) and isinstance (mime_type , str ):
309+ binary_results .append (
310+ ToolBinaryResult (
311+ data = data ,
312+ mime_type = mime_type ,
313+ type = "image" ,
314+ )
309315 )
310- )
311316 elif block_type == "resource" :
312317 resource = block .get ("resource" , {})
313- if resource .get ("text" ):
314- text_parts .append (resource ["text" ])
315- if resource .get ("blob" ):
318+ if not isinstance (resource , dict ):
319+ continue
320+ text = resource .get ("text" )
321+ if isinstance (text , str ) and text :
322+ text_parts .append (text )
323+ blob = resource .get ("blob" )
324+ if isinstance (blob , str ) and blob :
325+ mime_type = resource .get ("mimeType" , "application/octet-stream" )
326+ uri = resource .get ("uri" , "" )
316327 binary_results .append (
317328 ToolBinaryResult (
318- data = resource ["blob" ],
319- mime_type = resource .get ("mimeType" , "application/octet-stream" ),
329+ data = blob ,
330+ mime_type = mime_type
331+ if isinstance (mime_type , str )
332+ else "application/octet-stream" ,
320333 type = "resource" ,
321- description = resource . get ( " uri" , "" ) ,
334+ description = uri if isinstance ( uri , str ) else "" ,
322335 )
323336 )
324337
325338 return ToolResult (
326339 text_result_for_llm = "\n " .join (text_parts ),
327- result_type = "failure" if call_result .get ("isError" ) else "success" ,
340+ result_type = "failure" if call_result .get ("isError" ) is True else "success" ,
328341 binary_results_for_llm = binary_results if binary_results else None ,
329342 )
0 commit comments