{"record":{"id":"c3fea170e94571d1","repo":"jax-ml/jax","slug":"new-enum-value-must-be-an-instance-of-enum-class","errorCode":null,"errorMessage":"new enum value must be an instance of {enum_class}, got {new_val} of type {type(new_val)}.","messagePattern":"new enum value must be an instance of (.+?), got (.+?) of type (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/config.py","lineNumber":649,"sourceCode":"  if not isinstance(default, enum_class):\n    raise TypeError(\n        f'Default value must be of type {enum_class}, got {default} '\n        f\"of type {getattr(type(default), '__name__', type(default))}\"\n    )\n  name = name.lower()\n  default_str = os.getenv(name.upper(), None)\n  if default_str is not None:\n    try:\n      default = enum_class(default_str)\n    except ValueError as e:\n      raise ValueError(f\"Invalid value \\\"{default_str}\\\" for JAX flag {name}\") from e\n  config._contextmanager_flags.add(name)\n\n  def parser(new_val):\n    if isinstance(new_val, str):\n      return enum_class(new_val)\n    if not isinstance(new_val, enum_class):\n      raise TypeError(\n          f'new enum value must be an instance of {enum_class}, got'\n          f' {new_val} of type {type(new_val)}.'\n      )\n    if extra_validator is not None:\n      extra_validator(new_val)\n    return new_val\n\n  s = State[EnumType](\n      name,\n      default,\n      help,\n      update_global_hook=update_global_hook,\n      update_thread_local_hook=update_thread_local_hook,\n      parser=parser,\n      include_in_jit_key=include_in_jit_key,\n      include_in_trace_context=include_in_trace_context,\n  )\n  config.add_option(","sourceCodeStart":631,"sourceCodeEnd":667,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/config.py#L631-L667","documentation":"The parser for enum_class_state accepts either a str (converted via enum_class(new_val), which can itself raise ValueError for unknown strings) or an enum_class instance; any other type raises this TypeError when the flag is set.","triggerScenarios":"with my_flag(1): or jax.config.update('jax_my_flag', SomeObject()) — passing an int, None, or unrelated object.","commonSituations":"Passing a value parsed from JSON/YAML config that lost its Enum type (became a plain dict/str of the wrong shape), or an integer index meant to select a member.","solutions":["Pass MyEnum.FOO or the exact string 'foo'","Convert deserialized data explicitly before setting the flag"],"exampleFix":"// before\nwith jax.my_flag(cfg['mode']):  # cfg loaded from YAML -> int 1\n    ...\n// after\nwith jax.my_flag(MyEnum(cfg['mode'])):\n    ...","handlingStrategy":"validation","validationCode":"if isinstance(new_val, str):\n    new_val = enum_class(new_val)\nelif not isinstance(new_val, enum_class):\n    raise TypeError(f'expected str or {enum_class.__name__}, got {type(new_val)}')","typeGuard":"def coerce_enum(v, cls):\n    return cls(v) if isinstance(v, str) else v if isinstance(v, cls) else None","tryCatchPattern":"try:\n    with my_flag(v):\n        ...\nexcept (TypeError, ValueError):\n    with my_flag(DEFAULT_ENUM):\n        ...","preventionTips":["Normalize deserialized (JSON/YAML) values to Enum members before setting flags","Never pass integer indices to enum flags"],"tags":["jax","config","typeerror","enum"],"backgroundTag":"invalid-config-value","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}