ValueError: Invalid Enum Value

ValueError: 'invalid' is not a valid Color

Quick Answer

You are creating an enum member with a value that does not exist. Use try/except or check membership before creating enum instances.

Why This Happens

Enums have a fixed set of valid values. Calling Color('invalid') raises ValueError. This often happens when deserializing data from external sources.

The Problem

from enum import Enum

class Color(Enum):
    RED = 'red'
    GREEN = 'green'

color = Color('yellow')

The Fix

from enum import Enum

class Color(Enum):
    RED = 'red'
    GREEN = 'green'

try:
    color = Color(user_input)
except ValueError:
    color = Color.RED

Step-by-Step Fix

  1. 1

    Handle invalid values

    Wrap in try/except ValueError.

  2. 2

    List valid values

    Use [e.value for e in Enum] in error messages.

  3. 3

    Use a default

    Provide a sensible default for invalid input.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free