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

  1. Convert to str first if the value is string-like: camel_to_snake_case(str(value)).
  2. Guard at the boundary: if not isinstance(name, str): raise TypeError(...).
  3. 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

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


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/7c5328a63ad33a66. Report an issue: GitHub.