typeerror decoding str is not supported

In this article, we will discuss on how to solve the error typeerror: decoding str is not supported.

Also, we’ll discuss to you what are the causes and why this error occur?

Why the decoding str is not supported occur?

The decoding str is not supported typically occurs because when you try to decode a string that is already in the decoded state.

In other words, the string has already been converted from a byte sequence to a Unicode string, and you are trying to convert it again.

This is an example of how the error appears:

str('Welcome to the tutorial typeerror: decoding str is not supported ', str(158))

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 1, in
str(‘Welcome to the tutorial typeerror decoding str is not supported ‘, str(158))
TypeError: decoding str is not supported

In this example, the code attempts to concatenate two strings, yet mistakenly passes str(158) as an argument to the str() function.

This causes a typeerror decoding str is not supported because you can’t convert a string to a string.

Let’s take a look another example:

str('typeerror', encoding='utf-8')

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Traceback (most recent call last):
File “C:\Users\Dell\PycharmProjects\pythonProject\main.py”, line 1, in
str(‘typeerror’, encoding=’utf-8′)
TypeError: decoding str is not supported

In this example, The code str(‘typeerror’, encoding=’utf-8′) tries to convert the string ‘typeerror’ to bytes using the UTF-8 encoding.

However, the str() function does not support the encoding parameter.

Therefore, it results in a TypeError that says decoding str is not supported.

What are the causes of typeerror: decoding str is not supported?

The causes of typeerror: decoding str is not supported error usually occurs if you’re trying to decode a string object that cannot be decoded.

This error commonly occurs if you are trying to use the decode method on a string object that has already been decoded, or when you try to decode a string object that is not in a valid format.

How to solve this error?

There are two solutions to solve this error and the following are:

  • Make sure to supply a valid bytes object
  • You don’t have to decode strings

First solution: Supply a valid bytes object

We are only able to set the encoding when a valid bytes object is supplied.

For example:

print(str(b'Welcome to the tutorial decoding str is not supported ', encoding='utf-8'))

The output being displayed is:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
Welcome to the tutorial decoding str is not supported

Code explanation:

The string that is printed is created using the bytes literal notation b'...' which creates a sequence of bytes.

This sequence of bytes is decoded into a string using the str() function.

It defined the encoding used to decode the bytes with the encoding parameter set to ‘utf-8’.

The resulting decoded string is printed to the console using the print() function.

In addition, you can use a formatted string literal.

Let’s see the other example:

# Assign a new string value to str_value
str_value = 'xyz'

number = 123

result = f'{str_value} {number}'

print(result)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
xyz 123

This code assigns a new string value ‘xyz’ to the variable str_value.

It will assign an integer value of 123 to the variable number.

The code then uses an f-string (formatted string literal) to concatenate the string value and the integer value with a space in between them.

The result of this concatenation is stored in the variable result.

Finally, the code prints the value of the result variable, which is the concatenated string ‘xyz 123’.

Second Solution: Don’t decode the strings

The “TypeError decoding str is not supported” error which is usually means that we are trying to decode a string.

Note: When the string has already been decoded from a bytes object, you must remove any code that will tries to decode it.

This is an example of encoding a string to bytes and for decoding the bytes object back to a string.


my_string = 'hello world'
my_bytes = my_string.encode('utf-8')


print(my_bytes)


my_decoded_string = my_bytes.decode('utf-8')


print(my_decoded_string)

Output:

C:\Users\Dell\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\Dell\PycharmProjects\pythonProject\main.py
b’hello world’
hello world

Code explanation:

This code is to encode a string to bytes using UTF-8 encoding:

my_string = 'hello world'
my_bytes = my_string.encode('utf-8')

Next, this code is to print the encoded bytes:

print(my_bytes)

Then, this code below is to decode the bytes to a string using UTF-8 encoding:

my_decoded_string = my_bytes.decode('utf-8')

Finally, this code is to print the decoded string:

print(my_decoded_string)

Additional Resources

By reading the following tutorials you will be able to learn more about the Python error topics:

Frequently Asked Questions

What is Python TypeError and what causes it?

TypeError is raised when an operation is applied to an object of the wrong type. Common patterns: calling a non-callable object, adding incompatible types (str + int), passing the wrong number of arguments, or accessing attributes on a NoneType. Each TypeError message names the operation and expected vs actual types, the fix is almost always to convert types explicitly (int(), str()) or fix the wrong variable assignment.

How do I quickly debug a Python TypeError?

Three steps: (1) Read the full error message, it names the exact operation and types involved. (2) Print the type of every variable in that line: print(type(var1), type(var2)). (3) Check what the function expected vs what you passed. Most TypeError fixes are 1-line type casts or fixing a variable that became None unexpectedly.

Should I catch TypeError or let it propagate?

For internal code, let TypeError propagate, it’s almost always a real bug (wrong type passed). For boundary code (parsing user input, third-party API responses), catch TypeError + ValueError together: try: parsed = int(value) except (TypeError, ValueError): parsed = 0. Catching internal TypeErrors hides bugs.

How do I prevent TypeError in production?

Three patterns: (1) Use type hints (def add(a: int, b: int) -> int) and check with mypy / pyright in CI. (2) Validate inputs at boundaries (Pydantic for FastAPI, DRF serializers for Django). (3) Default values that match expected types (return 0 not None for numeric functions). Static typing catches 80% of TypeErrors before runtime.

Where can I find more TypeError fixes?

Browse the TypeError reference hub for 220+ specific TypeError fixes. For broader Python debugging, see the Python Tutorial hub. For related error types, see ValueError and AttributeError guides.

Conclusion

In conclusion, by following the solutions in this article you will be able to avoid to encounter the typeerror decoding str is not supported.

FAQs

What is the difference between encoding and decoding in Python?

Encoding is the process of converting a string to a bytes object while the Decoding is the process of converting a bytes object to a string.

Adones Evangelista

Programmer & Technical Writer at PIES IT Solution

Adones Evangelista is a programmer and writer at PIES IT Solution, author of over 900 tutorials and error-fix guides at itsourcecode.com. Specializes in JavaScript, Django, Laravel, and Python error debugging covering ValueError, TypeError, AttributeError, ModuleNotFoundError, and RuntimeError, plus C/C++ and PHP capstone projects for BSIT students.

Expertise: JavaScript · Python · Django · Laravel · Error Debugging · C/C++  · View all posts by Adones Evangelista →