Files
ai-agent-book/chapter9/self-evolving-tools/test_web_search_num_results.py
T
liqiang b119135836
Build latest book artifacts / build (push) Canceled after 0s
dependency resolution / resolve (3.11) (push) Canceled after 0s
dependency resolution / resolve (3.13) (push) Canceled after 0s
deploy-pages / build (push) Canceled after 0s
deploy-pages / deploy (push) Canceled after 0s
i18n consistency check / check (push) Canceled after 0s
provider adoption tests / test (chapter2/context-compression) (push) Canceled after 0s
provider adoption tests / test (chapter2/prompt-injection) (push) Canceled after 0s
provider adoption tests / test (chapter2/system-hint) (push) Canceled after 0s
provider adoption tests / test (chapter3/log-sanitization) (push) Canceled after 0s
web-search-agent tests / test (push) Canceled after 0s
web-search-agent tests / agentbook (push) Canceled after 0s
ai-agent-book 精选快照(<2MB 代码与文档,来自 github.com/bojieli/ai-agent-book)
2026-08-20 13:12:50 +00:00

37 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""web_search 对模型给出的异常 num_results(null / 非数字字符串)应兜底为默认值,
而不是抛 TypeError/ValueError 中断整个 Agent 循环。"""
import json
import base_tools
def _search_without_network(monkeypatch, num_results):
"""屏蔽真实网络与退避 sleep,只验证参数处理不崩溃。"""
def _fake_post(*a, **k):
raise RuntimeError("network disabled in test")
monkeypatch.setattr(base_tools.requests, "post", _fake_post)
monkeypatch.setattr(base_tools.time, "sleep", lambda *_a, **_k: None)
return base_tools.web_search("python stock library", num_results)
def test_num_results_null_falls_back(monkeypatch):
# 模型显式传 JSON null.get(..., 6) 的默认值挡不住,应兜底为 6
args = json.loads('{"query": "python stock library", "num_results": null}')
result = _search_without_network(monkeypatch, args.get("num_results", 6))
assert result["success"] is False # 网络被禁用 -> 走失败返回,而不是异常
assert "search failed" in result["error"]
def test_num_results_garbage_string_falls_back(monkeypatch):
args = json.loads('{"query": "python stock library", "num_results": "five"}')
result = _search_without_network(monkeypatch, args.get("num_results", 6))
assert result["success"] is False
assert "search failed" in result["error"]
def test_num_results_normal_still_clamped(monkeypatch):
result = _search_without_network(monkeypatch, 3)
assert result["success"] is False
assert "search failed" in result["error"]