fix(utils): add env_float helper for safe float env var parsing

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().
This commit is contained in:
annguyenNous 2026-06-20 14:00:07 +05:30 committed by kshitijk4poor
parent 15852722d4
commit 06ca1e9980

View file

@ -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)