{"record":{"id":"1e97beedd23c739d","repo":"TheAlgorithms/Python","slug":"var-name-nested-text-all-values-must-be-value","errorCode":null,"errorMessage":"{var_name} {nested_text}all values must be {value_type.__name__}","messagePattern":"(.+?) (.+?)all values must be (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"dynamic_programming/viterbi.py","lineNumber":371,"sourceCode":"    >>> _validate_dict({2: 0.5}, \"mock_name\",float, True)\n    Traceback (most recent call last):\n            ...\n    ValueError: mock_name all keys must be strings\n    >>> _validate_dict({\"b\": 4}, \"mock_name\", float,True)\n    Traceback (most recent call last):\n            ...\n    ValueError: mock_name nested dictionary all values must be float\n    \"\"\"\n    if not isinstance(_object, dict):\n        msg = f\"{var_name} must be a dict\"\n        raise ValueError(msg)\n    if not all(isinstance(x, str) for x in _object):\n        msg = f\"{var_name} all keys must be strings\"\n        raise ValueError(msg)\n    if not all(isinstance(x, value_type) for x in _object.values()):\n        nested_text = \"nested dictionary \" if nested else \"\"\n        msg = f\"{var_name} {nested_text}all values must be {value_type.__name__}\"\n        raise ValueError(msg)\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod()\n","sourceCodeStart":353,"sourceCodeEnd":378,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/dynamic_programming/viterbi.py#L353-L378","documentation":"Raised by viterbi's _validate_dict when the dict has string keys but one or more values are not of the expected type (float for probability tables). Initial probabilities map state->float; transition/emission tables map state->dict-of-floats (validated with nested=True, which prefixes 'nested dictionary' to the message). Ints like {'b': 4} fail because Python treats int and float as distinct under isinstance.","triggerScenarios":"Passing emission_probabilities={'rainy': {'walk': 1}} (int 1 instead of 1.0); a probability table where any leaf is an int, string, or None; the doctest _validate_dict({'b': 4}, 'mock_name', float, True) reproduces it exactly.","commonSituations":"Writing probabilities as whole numbers (0 or 1) in hand-authored configs and forgetting the decimal point; JSON that deserializes 1.0 as int when the text is '1'; computed values from integer arithmetic instead of float division.","solutions":["Write all probabilities as floats: use 1.0, 0.0, 0.7 — not 1, 0, or integer results.","Normalize tables once: {k: {s: float(v) for s, v in row.items()} for k, row in table.items()}.","Prefer true division (/) over integer division or int constants when generating tables."],"exampleFix":"# before\nemit_p = {'rainy': {'walk': 1, 'shop': 0}}  # ValueError\n\n# after\nemit_p = {'rainy': {'walk': 1.0, 'shop': 0.0}}","handlingStrategy":"validation","validationCode":"def float_tables(table: dict) -> dict:\n    return {k: {s: float(v) for s, v in row.items()} for k, row in table.items()}","typeGuard":"def float_valued(d: object) -> TypeGuard[dict[str, float]]:\n    return isinstance(d, dict) and all(isinstance(v, float) for v in d.values())","tryCatchPattern":null,"preventionTips":["Write probabilities with explicit decimals: 1.0, 0.0, not 1, 0.","Normalize all tables once with float(...) before calling viterbi.","Use true division (/) when computing probabilities."],"tags":["dynamic-programming","hmm","type-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}