From 06ca1e9980fed6009dc442a9468247fac32e5581 Mon Sep 17 00:00:00 2001 From: annguyenNous Date: Sat, 20 Jun 2026 14:00:07 +0530 Subject: [PATCH] fix(utils): add env_float helper for safe float env var parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirrors the existing env_int() helper: returns the default when the variable is unset or non-numeric instead of raising ValueError. Used by the follow-up commit to guard malformed float env vars across the gateway. Salvaged from #48735 (@annguyenNous). The PR's api_server.py change is now redundant — main guards HERMES_MAX_ITERATIONS via _current_max_iterations(). --- utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/utils.py b/utils.py index ad7f28f8dba..5e1b964debc 100644 --- a/utils.py +++ b/utils.py @@ -323,6 +323,17 @@ def env_int(key: str, default: int = 0) -> int: return default +def env_float(key: str, default: float = 0.0) -> float: + """Read an environment variable as a float, with fallback.""" + raw = os.getenv(key, "").strip() + if not raw: + return default + try: + return float(raw) + except (ValueError, TypeError): + return default + + def env_bool(key: str, default: bool = False) -> bool: """Read an environment variable as a boolean.""" return is_truthy_value(os.getenv(key, ""), default=default)