ValueError
builtins.ValueError
Stack trace
Traceback (most recent call last):
File "main.py", line 25, in <module>
instructor = Instructor(role='invalid_role') # triggers error
File "/usr/lib/python3.9/enum.py", line 310, in __call__
raise ValueError(f"{value!r} is not a valid {cls.__name__}")
ValueError: 'invalid_role' is not a valid InstructorRole Why it happens
When an enum field is defined but the input value does not match any of the enum members, Python raises a ValueError. This often happens if the input is a string that doesn't exactly match the enum names or if the enum is not properly referenced in the instructor schema.
Detection
Validate enum fields explicitly before instantiating the instructor object or catch ValueError exceptions to log invalid enum values and prevent crashes.
Causes & fixes
Input string does not match any enum member name exactly (case-sensitive mismatch)
Ensure input strings match enum member names exactly, including case, or convert input to enum using a mapping or case normalization.
Enum field is declared but the instructor schema uses a plain string type instead of the enum type
Declare the field type explicitly as the enum class in the instructor schema to enforce enum validation.
Passing raw strings instead of enum members when creating instructor instances
Convert input strings to enum members before assignment, e.g., InstructorRole(input_string), to ensure validation.
Code: broken vs fixed
from enum import Enum
class InstructorRole(Enum):
TEACHER = 'teacher'
ASSISTANT = 'assistant'
class Instructor:
def __init__(self, role: str):
self.role = role
instructor = Instructor(role='teacher') # This does not validate enum
print(instructor.role)
instructor_invalid = Instructor(role='invalid_role') # No error here, but invalid
print(instructor_invalid.role) # triggers logical error later import os
from enum import Enum
class InstructorRole(Enum):
TEACHER = 'teacher'
ASSISTANT = 'assistant'
class Instructor:
def __init__(self, role: InstructorRole):
self.role = role
role_input = 'teacher'
try:
role_enum = InstructorRole(role_input) # Convert string to enum, validates
instructor = Instructor(role=role_enum)
print(instructor.role)
except ValueError as e:
print(f'Invalid role: {role_input}') # Handle invalid enum input
# Example with invalid input
invalid_input = 'invalid_role'
try:
role_enum = InstructorRole(invalid_input) # Raises ValueError
instructor_invalid = Instructor(role=role_enum)
except ValueError as e:
print(f'Invalid role: {invalid_input}') # Proper error handling
# Note: API keys not needed here, but environment usage shown
os.environ['API_KEY'] = 'your_api_key_here' # placeholder for environment usage Workaround
Wrap instructor creation in try/except ValueError, catch invalid enum values, and fallback to a default enum member or log the error for manual correction.
Prevention
Always declare enum fields explicitly in schemas and convert or validate input strings against enum members before assignment to prevent invalid values.