Skip to content

fix(ch4): parse answer_box_list objects - #858

Draft
linhongyu510 wants to merge 2 commits into
datawhalechina:mainfrom
linhongyu510:fix/ch4-answer-box-list
Draft

fix(ch4): parse answer_box_list objects#858
linhongyu510 wants to merge 2 commits into
datawhalechina:mainfrom
linhongyu510:fix/ch4-answer-box-list

Conversation

@linhongyu510

Copy link
Copy Markdown

问题

answer_box_list 是答案框对象列表。当前代码直接执行
"\n".join(results["answer_box_list"]),当列表元素为字典时会触发:

sequence item 0: expected str instance, dict found

这会让 search() 捕获异常并把错误文本返回给 Agent,而不是返回搜索答案。

Refs #849

改动

  • 从每个答案框对象提取非空字符串 answer 后再拼接。
  • 当列表中没有可用 answer 时继续尝试单个 answer_box、知识图谱和自然结果,避免提前返回空字符串。
  • 同步中文、英文教程中的代码与行为说明。
  • 新增无网络定向测试,覆盖多个答案框对象和回退路径。

SerpApi 官方资料说明多答案通过 answer_box_list 数组返回:
https://serpapi.com/direct-answer-box-api

验证

python3 -m unittest discover -s code/chapter4 -p 'test_*.py' -v
# Ran 2 tests ... OK

python3 -m py_compile code/chapter4/tools.py code/chapter4/test_tools.py
ruff check code/chapter4/test_tools.py
git diff --check

另外按教程依赖安装 google-search-results==2.4.2,使用真实
SerpApiClient 类并仅 mock get_dict() 的网络结果完成 smoke test,
确认两个答案输出为 first\nsecond。未使用真实 API key,也未发起外部
SerpApi 请求。

竞争与范围

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant