fix(utils): do not let a blank or non-numeric LK_* debug flag break package import - #7470
Open
Lesereingrape wants to merge 2 commits into
Open
Lesereingrape wants to merge 2 commits into
Lesereingrape wants to merge 2 commits into
Conversation
… import The debug flags are parsed at module scope, so `int(os.getenv(...))` runs while `import livekit.agents` is still executing. An empty value -- which is how an unset variable reaches a container -- raised ValueError there, and so did `LK_DUMP_TTS=true`. `resolve_env_int` follows what the package already does for the rest of its environment: `resolve_env_var` treats "" as unset, and `_env_seconds` and `_cpu_count_from_env` fall back to a default with a warning instead of raising.
|
|
The openai, google and aws plugins parse their debug flags at import time with a bare int(os.getenv(...)), so a blank value kept the plugin unimportable even with the core fix in place. Route them through the same helper and cover the openai plugin import.
This branch has not been deployed
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.
What
LK_DUMP_TTS,LK_OPENAI_DEBUG,LK_KEYTERMS_DEBUGandLIVEKIT_EVALS_VERBOSEare parsed withint(os.getenv(...))at module scope, so the parse runs whileimport livekit.agentsis still executing. A value that is not an integer raisedValueErrorthere and left the whole package unimportable.utils.env.resolve_env_int()returns the parsed value, and falls back to the default (logging a warning) when the variable is unset, blank, or not an integer. The six module-level flags now go through it — including the two that readLK_OPENAI_DEBUG, which duplicated the same parse ininference/llm.pyandllm/_realtime/openai.py.import oswas dropped from the five files where the flag was its only use.Why
An empty value is exactly how "not configured" arrives in a container:
environment: [LK_DUMP_TTS]in compose, orenv:from an unset CI variable, both hand the process"". Measured on currentmain(98ed3e8):The same trace, one line different, for
LK_OPENAI_DEBUG,LK_KEYTERMS_DEBUGandLIVEKIT_EVALS_VERBOSE.LK_DUMP_TTS=truefails the same way. Every worker, CLI invocation and notebook in the process dies at import — not just the feature the flag belongs to.The rest of the package already treats the environment the way this PR makes these flags behave:
utils/env.py:31—resolve_env_varskips a variable whose value is"";telemetry/loop_monitor.py:125-141—_env_secondsreturns the default for blank input and warns on anything unparsable; its docstring says invalid values "fall back to the defaults with a warning rather than disabling silently";utils/hw/cpu.py:24-30—_cpu_count_from_envwraps itsfloat()parse inexcept ValueErrorand warns.So this is the odd one out, and it is the only one of the four that can take the process down.
Valid values keep their meaning: every one of these flags is only ever read in a truthiness test (
if lk_dump_tts:etc.), and integers parse as before. I deliberately did not add word parsing ("true","on") — those now log a warning and fall back to the default instead of raising.Tests
tests/test_utils_env.pygainsTestResolveEnvInt(unset, blank, whitespace,true/on/1.5, and1/0/" 2 ") plus a regression test that runsimport livekit.agentsin a subprocess with all four flags blank — that subprocess test fails onmainwith the trace above and passes with the fix.pytest tests/test_utils_env.py --unit→17 passed(7 existing + 10 new)ruff check/ruff format --checkon all 9 files → cleanmypy livekit(strict) → 51 errors, all pre-existing platform/stub issues; the only one in a touched file isinference/llm.py:604(RealtimeModel.from_model_stringreturn type), which is the same statement as:605on the base, i.e. untouched by this diffpytest --unit --audio_eotgate could not be run here: this is a Windows host anduv sync --all-extrascannot resolvebithuman(nowin_amd64wheel), so I ran the affected module plustests/test_keyterm_detection_span.py tests/test_evals.py tests/test_agent_tts_node.py(5 passed, 3 deselected) instead