{"record":{"id":"3fb7e1acda975992","repo":"xai-org/x-algorithm","slug":"literal-ty-does-not-allow-for-val-r","errorCode":null,"errorMessage":"Literal {ty} does not allow for {val!r}","messagePattern":"Literal (.+?) does not allow for (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"phoenix/python/common/xai-configlib/src/xai_configlib/__init__.py","lineNumber":456,"sourceCode":"            result[cast_type(kty, k.strip())] = cast_type(vty, v.strip())\n        return result\n    elif is_path(ty):\n        return Path(val)\n    elif is_bytes(ty):\n        return base64.b64decode(val.encode(\"utf-8\"))\n    elif is_datetime(ty):\n        return datetime.datetime.fromisoformat(val)\n    elif get_origin(ty) is Literal:\n        options = get_args(ty)\n        subtys = tuple([type(o) for o in options])\n        for subty in subtys:\n            try:\n                cast = cast_type(subty, val)\n            except ValueError:\n                continue\n            if cast in options:\n                return cast\n        raise TypeError(f\"Literal {ty} does not allow for {val!r}\")\n    elif isinstance(ty, enum.EnumMeta):\n        try:\n            return ty(val)\n        except ValueError:\n            return ty[val]\n    elif isinstance(ty, TypeAliasType):\n        return cast_type(ty.__value__, val)\n    elif ty is re.Pattern:\n        return re.compile(val)\n    else:\n        return ty(val)\n\n\ndef is_union(ty) -> bool:\n    return get_origin(ty) in (Union, UnionType)\n\n\ndef is_optional(ty) -> bool:","sourceCodeStart":438,"sourceCodeEnd":474,"githubUrl":"https://github.com/xai-org/x-algorithm/blob/24c60942c5c5fdad3a6addffb4c6e6d2f228f04f/phoenix/python/common/xai-configlib/src/xai_configlib/__init__.py#L438-L474","documentation":"For Literal-typed fields, cast_type tries to cast the string to each allowed literal type and checks membership in the Literal's options. If the resulting value is not one of the allowed literals, this TypeError is raised.","triggerScenarios":"Overriding a field annotated Literal[\"a\",\"b\"] with 'c', or a Literal[1,2] with '3'; the value casts fine but is not in the allowed set.","commonSituations":"Passing an enum-like string that was renamed in a newer config version, or a casing mismatch ('AdamW' vs 'adamw') since membership is exact.","solutions":["Use one of the exact values listed in the Literal annotation","Check casing and quoting; shell may strip or alter quotes","If the new value is legitimate, extend the Literal in the config dataclass"],"exampleFix":"# before\nreplace_cli_subs(cfg, [\"optimizer=adafactor\"])  # Literal[\"adam\", \"adamw\"]\n# after\nreplace_cli_subs(cfg, [\"optimizer=adamw\"])","handlingStrategy":"validation","validationCode":"import typing\n\ndef literal_allows(ty, val: str):\n    if typing.get_origin(ty) is not typing.Literal:\n        return True\n    opts = typing.get_args(ty)\n    try:\n        return any(t(val) in opts for t in (str, int, float) if t is not str or isinstance(opts[0], str))\n    except ValueError:\n        return False","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Expose valid literal values in CLI help","Use exact casing from the annotation"],"tags":["config","literal","validation","type-coercion"],"backgroundTag":"enum-value-invalid","analyzedSha":"24c60942c5c5fdad3a6addffb4c6e6d2f228f04f","analyzedAt":"2026-08-28T11:40:14.686Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}