Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion astrbot/core/provider/sources/anthropic_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,8 @@ async def _query_stream(
try:
if "input_json" in tool_info:
tool_info["input"] = json.loads(tool_info["input_json"])
else:
tool_info["input"] = tool_info.get("input", {})
Comment on lines +424 to +425
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This else block is redundant. The tool_info dictionary, which comes from tool_use_buffer, is guaranteed to have an 'input' key initialized to {} on line 377. When 'input_json' is not present, tool_info['input'] already contains the correct empty dictionary. This else block is a no-op and can be removed to improve code clarity.


# 添加到最终结果
final_tool_calls.append(
Expand All @@ -442,7 +444,7 @@ async def _query_stream(
id=id,
)
except json.JSONDecodeError:
# JSON 解析失败,跳过这个工具调用
# JSON 解析失败,跳过这个工具调用,不 yield
logger.warning(f"工具调用参数 JSON 解析失败: {tool_info}")
Comment on lines 446 to 448
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider logging the exception details in addition to tool_info when JSON parsing fails.

Currently the log only includes tool_info, which limits debuggability in production. Consider capturing the exception (e.g. except json.JSONDecodeError as exc:) and logging exc or str(exc) as well so you can distinguish encoding issues from schema or partial-data problems.

Suggested change
except json.JSONDecodeError:
# JSON 解析失败,跳过这个工具调用
# JSON 解析失败,跳过这个工具调用,不 yield
logger.warning(f"工具调用参数 JSON 解析失败: {tool_info}")
except json.JSONDecodeError as exc:
# JSON 解析失败,跳过这个工具调用,不 yield,记录异常详情便于排查
logger.warning(
"工具调用参数 JSON 解析失败: %s, error: %s",
tool_info,
exc,
)


# 清理缓冲区
Expand Down
Loading