TheAlgorithms/Python · error · ValueError
Expected string as input, found {type(input_str)}
Error message
Expected string as input, found {type(input_str)} What it means
Raised by camel_to_snake_case in strings/camel_case_to_snake_case.py when input_str is not a str; the f-string message embeds the actual type, e.g. "Expected string as input, found <class 'int'>". The function then iterates characters and calls .isupper()/.isdigit(), which only exist on strings, so non-string input is rejected up front. Note it raises ValueError where TypeError would be the conventional choice for a wrong type.
Source
Thrown at strings/camel_case_to_snake_case.py:27
'some_random_str_ng'
>>> camel_to_snake_case("123someRandom123String123")
'123_some_random_123_string_123'
>>> camel_to_snake_case("123SomeRandom123String123")
'123_some_random_123_string_123'
>>> camel_to_snake_case(123)
Traceback (most recent call last):
...
ValueError: Expected string as input, found <class 'int'>
"""
# check for invalid input type
if not isinstance(input_str, str):
msg = f"Expected string as input, found {type(input_str)}"
raise ValueError(msg)
snake_str = ""
for index, char in enumerate(input_str):
if char.isupper():
snake_str += "_" + char.lower()
# if char is lowercase but proceeded by a digit:
elif input_str[index - 1].isdigit() and char.islower():
snake_str += "_" + char
# if char is a digit proceeded by a letter:
elif input_str[index - 1].isalpha() and char.isnumeric():
snake_str += "_" + char.lower()
# if char is not alphanumeric:
elif not char.isalnum():
snake_str += "_"
View on GitHub (pinned to f5988cc097)
Solutions
- Convert to str first if the value is string-like: camel_to_snake_case(str(value)).
- Guard at the boundary: if not isinstance(name, str): raise TypeError(...).
- For None-able fields, default explicitly: camel_to_snake_case(name or '').
Example fix
# before
camel_to_snake_case(profile.get('userName')) # value may be None/int
# after
raw = profile.get('userName')
if not isinstance(raw, str):
raise TypeError(f'userName must be str, got {type(raw).__name__}')
camel_to_snake_case(raw) Defensive patterns
Strategy: type-guard
Validate before calling
if not isinstance(input_str, str):
raise TypeError(f'expected str, got {type(input_str).__name__}')
snake = camel_to_snake_case(input_str) Type guard
def is_str(value) -> bool:
return isinstance(value, str) Try / catch
try:
snake = camel_to_snake_case(name)
except ValueError:
if not isinstance(name, str):
snake = camel_to_snake_case(str(name))
else:
raise Prevention
- Check isinstance(value, str) at your API boundary before string utilities.
- Default None-able fields: (name or '').
- This raises ValueError for a type problem; match on message content if you must branch in a catch.
When it happens
Trigger: camel_to_snake_case(123); camel_to_snake_case(['Abc']); camel_to_snake_case(None). Values from APIs that should be strings but arrive as numbers or None are the usual source.
Common situations: See trigger scenarios.
Related errors
- Input must be a string
- 'float' object cannot be interpreted as an integer
- 'float' object cannot be interpreted as an integer
- iterations must be defined as integers
- All weights must be integers but got weight of type {type(wt
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/7c5328a63ad33a66.
Report an issue: GitHub.