TheAlgorithms/Python · error · ValueError
There's an empty parameter
Error message
There's an empty parameter
What it means
Raised by viterbi()'s non-empty validation when any of its five parameters — observations_space, states_space, initial_probabilities, transition_probabilities, emission_probabilities — is empty (falsy). The Viterbi decoder needs at least one state, one observation symbol, and complete probability tables to run, so an empty any of these makes decoding undefined.
Solutions
- Check all five structures are non-empty before calling viterbi and log which one is empty.
- Fix the parameter loading code to fail loudly when a config section (states, transitions, emissions) is absent.
- If the observation sequence is legitimately empty, short-circuit in your caller instead of calling viterbi.
Example fix
# before
viterbi([], states, initial_p, trans_p, emit_p) # ValueError
# after
if not observations:
result = [] # nothing to decode
else:
result = viterbi(observations, states, initial_p, trans_p, emit_p) Defensive patterns
Strategy: validation
Validate before calling
def valid_hmm_args(obs, states, init_p, trans_p, emit_p) -> bool:
return all([obs, states, init_p, trans_p, emit_p]) Try / catch
try:
viterbi(obs, states, init_p, trans_p, emit_p)
except ValueError as e:
if 'empty parameter' in str(e):
raise RuntimeError(f'HMM config incomplete: {e}') from e
raise Prevention
- Fail loudly when loading HMM config: require every section present.
- Short-circuit empty observation sequences before decoding.
- Log which of the five parameters is empty during development.
When it happens
Trigger: Calling viterbi(observation, [], initial_prob, transition_prob, emission_prob) with an empty states list; passing {} for transition_probabilities; passing '' or [] for observations_space. Note: truthiness is used, so an empty dict/list/tuple all trigger it.
Common situations: Loading HMM parameters from a JSON/YAML file where a section is missing and deserializes to {}; empty observation sequence after a filter step removes all events; initializing tables programmatically and forgetting to populate them.
Related errors
- All days elements should be greater than 0
- All days elements should be less than 366
- All weights must be integers but got weight of type
- Both the weights and values vectors must be either lists or…
- Each integral piece of rod must have a corresponding price…
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/0e122c03f981d052.
Report an issue: GitHub.
Appendix: source
Thrown at dynamic_programming/viterbi.py:245
>>> _validate_not_empty(["a"], ["b"], {"c":0.5}, {}, {"f": {"g": 0.7}})
Traceback (most recent call last):
...
ValueError: There's an empty parameter
>>> _validate_not_empty(["a"], ["b"], None, {"d": {"e": 0.6}}, {"f": {"g": 0.7}})
Traceback (most recent call last):
...
ValueError: There's an empty parameter
"""
if not all(
[
observations_space,
states_space,
initial_probabilities,
transition_probabilities,
emission_probabilities,
]
):
raise ValueError("There's an empty parameter")
def _validate_lists(observations_space: Any, states_space: Any) -> None:
"""
>>> _validate_lists(["a"], ["b"])
>>> _validate_lists(1234, ["b"])
Traceback (most recent call last):
...
ValueError: observations_space must be a list
>>> _validate_lists(["a"], [3])
Traceback (most recent call last):
...
ValueError: states_space must be a list of strings
"""
_validate_list(observations_space, "observations_space")
_validate_list(states_space, "states_space")
View on GitHub (pinned to f5988cc097)