From 0d1003559d85372aed77116a68362e73e93b5b37 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Mon, 30 Mar 2026 13:37:25 -0700 Subject: [PATCH] refactor: simplify web backend priority detection (#4036) * fix(gateway): honor default for invalid bool-like config values * refactor: simplify web backend priority detection Replace cascading boolean conditions with a priority-ordered loop. Same behavior (verified against all 16 env var combinations), half the lines, trivially extensible for new backends. --------- Co-authored-by: aydnOktay --- tools/web_tools.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/tools/web_tools.py b/tools/web_tools.py index c8e7fb0f3..c61bc1eb7 100644 --- a/tools/web_tools.py +++ b/tools/web_tools.py @@ -77,20 +77,18 @@ def _get_backend() -> str: if configured in ("parallel", "firecrawl", "tavily", "exa"): return configured - # Fallback for manual / legacy config — use whichever key is present. - has_firecrawl = _has_env("FIRECRAWL_API_KEY") or _has_env("FIRECRAWL_API_URL") - has_parallel = _has_env("PARALLEL_API_KEY") - has_tavily = _has_env("TAVILY_API_KEY") - has_exa = _has_env("EXA_API_KEY") - if has_exa and not has_firecrawl and not has_parallel and not has_tavily: - return "exa" - if has_tavily and not has_firecrawl and not has_parallel: - return "tavily" - if has_parallel and not has_firecrawl: - return "parallel" + # Fallback for manual / legacy config — pick highest-priority backend + # that has a key configured. Order: firecrawl > parallel > tavily > exa. + for backend, keys in [ + ("firecrawl", ("FIRECRAWL_API_KEY", "FIRECRAWL_API_URL")), + ("parallel", ("PARALLEL_API_KEY",)), + ("tavily", ("TAVILY_API_KEY",)), + ("exa", ("EXA_API_KEY",)), + ]: + if any(_has_env(k) for k in keys): + return backend - # Default to firecrawl (backward compat, or when both are set) - return "firecrawl" + return "firecrawl" # default (backward compat) # ─── Firecrawl Client ────────────────────────────────────────────────────────