fix(community): reject bool and fractional web-search max_results like image search does (#5852)

This commit is contained in:
Grapette.L 2026-09-25 21:43:48 +08:00 committed by GitHub
parent 5242fcfb63
commit 63401d79fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 3 deletions

View File

@ -33,10 +33,14 @@ WIKIPEDIA_LANGUAGE_ALIASES = {
def _coerce_max_results(value: object) -> int:
"""Normalize config/parameter values before passing them to DDGS."""
try:
count = int(value) # type: ignore[call-overload]
except (TypeError, ValueError, OverflowError):
if isinstance(value, bool) or (isinstance(value, float) and not value.is_integer()):
# int() accepts booleans and silently truncates a YAML value such as 3.5.
count = 0
else:
try:
count = int(value) # type: ignore[call-overload]
except (TypeError, ValueError, OverflowError):
count = 0
if count <= 0:
logger.warning("Invalid DDG Search max_results=%r; using default %s", value, DEFAULT_MAX_RESULTS)
return DEFAULT_MAX_RESULTS

View File

@ -22,6 +22,8 @@ from deerflow.community.ddg_search import tools
pytest.param("$DDG_TEST_MAX_RESULTS", "abc", {"max_results": 2}, 5, True, id="invalid-env"),
pytest.param("$DDG_TEST_MAX_RESULTS", "", {"max_results": 2}, 5, True, id="empty-env"),
pytest.param("$DDG_TEST_MAX_RESULTS", "3.5", {"max_results": 2}, 5, True, id="fractional-env"),
pytest.param(3.5, "3", {"max_results": 2}, 5, True, id="fractional-config"),
pytest.param(True, "3", {"max_results": 2}, 5, True, id="boolean-config"),
pytest.param("$DDG_TEST_MAX_RESULTS", "0", {"max_results": 2}, 5, True, id="zero-env"),
pytest.param("$DDG_TEST_MAX_RESULTS", "-2", {"max_results": 2}, 5, True, id="negative-env"),
pytest.param(0, "3", {}, 5, True, id="zero-config"),