{"record":{"id":"7c5328a63ad33a66","repo":"TheAlgorithms/Python","slug":"expected-string-as-input-found-type-input-str","errorCode":null,"errorMessage":"Expected string as input, found {type(input_str)}","messagePattern":"Expected string as input, found (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"strings/camel_case_to_snake_case.py","lineNumber":27,"sourceCode":"    'some_random_str_ng'\r\n\r\n    >>> camel_to_snake_case(\"123someRandom123String123\")\r\n    '123_some_random_123_string_123'\r\n\r\n    >>> camel_to_snake_case(\"123SomeRandom123String123\")\r\n    '123_some_random_123_string_123'\r\n\r\n    >>> camel_to_snake_case(123)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Expected string as input, found <class 'int'>\r\n\r\n    \"\"\"\r\n\r\n    # check for invalid input type\r\n    if not isinstance(input_str, str):\r\n        msg = f\"Expected string as input, found {type(input_str)}\"\r\n        raise ValueError(msg)\r\n\r\n    snake_str = \"\"\r\n\r\n    for index, char in enumerate(input_str):\r\n        if char.isupper():\r\n            snake_str += \"_\" + char.lower()\r\n\r\n        # if char is lowercase but proceeded by a digit:\r\n        elif input_str[index - 1].isdigit() and char.islower():\r\n            snake_str += \"_\" + char\r\n\r\n        # if char is a digit proceeded by a letter:\r\n        elif input_str[index - 1].isalpha() and char.isnumeric():\r\n            snake_str += \"_\" + char.lower()\r\n\r\n        # if char is not alphanumeric:\r\n        elif not char.isalnum():\r\n            snake_str += \"_\"\r","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/strings/camel_case_to_snake_case.py#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"See trigger scenarios.","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 '')."],"exampleFix":"# before\ncamel_to_snake_case(profile.get('userName'))  # value may be None/int\n\n# after\nraw = profile.get('userName')\nif not isinstance(raw, str):\n    raise TypeError(f'userName must be str, got {type(raw).__name__}')\ncamel_to_snake_case(raw)","handlingStrategy":"type-guard","validationCode":"if not isinstance(input_str, str):\n    raise TypeError(f'expected str, got {type(input_str).__name__}')\nsnake = camel_to_snake_case(input_str)","typeGuard":"def is_str(value) -> bool:\n    return isinstance(value, str)","tryCatchPattern":"try:\n    snake = camel_to_snake_case(name)\nexcept ValueError:\n    if not isinstance(name, str):\n        snake = camel_to_snake_case(str(name))\n    else:\n        raise","preventionTips":["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."],"tags":["strings","naming-convention","type-error","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}