Merge pull request #1420 from xcr1234/dev

fix: 工具调用的时候可能出现的无法解析参数问题
pull/1468/head
SengokuCola 2025-12-29 19:07:32 +08:00 committed by GitHub
commit 72ef7bade2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 13 additions and 1 deletions

View File

@ -447,8 +447,20 @@ def _default_normal_response_parser(
for call in message_part.tool_calls:
try:
arguments = json.loads(repair_json(call.function.arguments))
# 【新增修复逻辑】如果解析出来还是字符串,说明发生了双重编码,尝试二次解析
if isinstance(arguments, str):
try:
# 尝试对字符串内容再次进行修复和解析
arguments = json.loads(repair_json(arguments))
except Exception:
# 如果二次解析失败,保留原值,让下方的 isinstance(dict) 抛出更具体的错误
pass
if not isinstance(arguments, dict):
raise RespParseException(resp, "响应解析失败,工具调用参数无法解析为字典类型")
# 此时为了调试方便,建议打印出 arguments 的类型
raise RespParseException(
resp,
f"响应解析失败,工具调用参数无法解析为字典类型 type={type(arguments)} arguments={arguments}"
)
api_response.tool_calls.append(ToolCall(call.id, call.function.name, arguments))
except json.JSONDecodeError as e:
raise RespParseException(resp, "响应解析失败,无法解析工具调用参数") from e