Skip to content

Commit 238f679

Browse files
liuruibinwangdan-fit2cloud
authored andcommitted
fix: enhance tool result handling for various content types
1 parent c266680 commit 238f679

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

apps/application/flow/tools.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,23 @@ async def _yield_mcp_response(chat_model, message_list, mcp_servers, mcp_output_
400400
if tool_id in tool_calls_info:
401401
tool_info = tool_calls_info[tool_id]
402402
try:
403-
tool_result = json.loads(chunk[0].content)
404-
tool_lib_id = tool_result.pop('tool_id') if 'tool_id' in tool_result else None
403+
if isinstance(chunk[0].content, str):
404+
tool_result = json.loads(chunk[0].content)
405+
elif isinstance(chunk[0].content, dict):
406+
tool_result = chunk[0].content
407+
elif isinstance(chunk[0].content, list):
408+
tool_result = chunk[0].content[0] if len(chunk[0].content) > 0 else {}
409+
else:
410+
tool_result = {}
411+
text = tool_result.pop('text') if 'text' in tool_result else None
412+
text_result = json.loads(text)
413+
if text:
414+
tool_lib_id = text_result.pop('tool_id') if 'tool_id' in text_result else None
415+
else:
416+
tool_lib_id = tool_result.pop('tool_id') if 'tool_id' in tool_result else None
405417
if tool_lib_id:
406418
await save_tool_record(tool_lib_id, tool_info, tool_result, source_id, source_type)
407-
tool_result = json.dumps(tool_result, ensure_ascii=False)
419+
tool_result = json.dumps(text_result, ensure_ascii=False)
408420
except Exception as e:
409421
tool_result = chunk[0].content
410422
content = generate_tool_message_complete(

0 commit comments

Comments
 (0)