fix: guard float(os.getenv()) against non-numeric env var values - #30274
Open
annguyenNous wants to merge 1 commit into
Open
fix: guard float(os.getenv()) against non-numeric env var values#30274annguyenNous wants to merge 1 commit into
annguyenNous wants to merge 1 commit into
Conversation
float(os.getenv('VAR', '1800')) raises ValueError when the env var
is set to a non-numeric string like 'abc'. The 'or default' pattern
doesn't help because non-empty strings are truthy.
Add _safe_float_env() helper to run_agent.py and
agent/chat_completion_helpers.py that wraps float() in try/except
(ValueError, TypeError) with a warning log.
Fixed sites (core request path — hit on every API call):
- run_agent.py: HERMES_API_TIMEOUT, HERMES_API_CALL_STALE_TIMEOUT
- chat_completion_helpers.py: HERMES_API_TIMEOUT, HERMES_STREAM_READ_TIMEOUT,
HERMES_STREAM_STALE_TIMEOUT
This was referenced Jun 13, 2026
teknium1
reviewed
Jul 13, 2026
teknium1
left a comment
Contributor
There was a problem hiding this comment.
Thanks for identifying the malformed environment-value failure mode. The remaining non-stream stale-timeout path is still live on current main, but most of this PR has since been covered by the shared helper.
Problems
run_agent.py:1257-1259still directly callsfloat(env_timeout), but the other four proposed sites already use canonicalenv_floataftera7dd98c8609c0d944e3c5dd0c5b9ee31dd99eb29(run_agent.py:1233;agent/chat_completion_helpers.py:2171,2178,2998).- The PR's stale fallback is
300.0, whereas current main's documented and tested default is90.0(run_agent.py:1242-1246,1274;tests/hermes_cli/test_timeouts.py:267-268). tests/hermes_cli/test_timeouts.py:228-268lacks a malformed-value case.
Suggested changes
- Salvage the one live branch with
env_float("HERMES_API_CALL_STALE_TIMEOUT", 90.0)and retain its explicit-value flag. - Add a malformed-env regression case asserting
(90.0, False).
Automated hermes-sweeper review.
| @@ -897,7 +909,7 @@ def _resolved_api_call_stale_timeout_base(self) -> tuple[float, bool]: | |||
|
|
|||
Contributor
There was a problem hiding this comment.
When salvaging this branch onto current main, use the canonical env_float helper and preserve the current 90-second default. Current main documents and tests 90.0 for the implicit stale timeout (run_agent.py:1242-1246, tests/hermes_cli/test_timeouts.py:267-268), so this PR's 300.0 fallback would regress that contract.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
float(os.getenv('VAR', '1800'))raisesValueErrorwhen the env var is set to a non-numeric string like"abc". Theor defaultpattern doesn't help because non-empty strings are truthy —float("abc" or 1800)still raises.This affects 5 call sites in the core request path (hit on every API call):
run_agent.pyHERMES_API_TIMEOUTrun_agent.pyHERMES_API_CALL_STALE_TIMEOUTchat_completion_helpers.pyHERMES_API_TIMEOUTchat_completion_helpers.pyHERMES_STREAM_READ_TIMEOUTchat_completion_helpers.pyHERMES_STREAM_STALE_TIMEOUTFix
Add
_safe_float_env(var_name, default)helper that wrapsfloat()intry/except (ValueError, TypeError)with a warning log. Applied to all 5 unguarded call sites in the core request path.Additional unguarded sites exist in gateway platforms (discord, wecom, telegram) and CLI auth — those only affect startup and can be addressed in a follow-up.
Tests
py_compilepasses on both files