builtins.TypeError must be str, not bytes
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
This Python error means a piece of code expected text as str, but you passed binary data as bytes instead. In Python 3, text and bytes are intentionally different types, so the fix is not "force them together" but to decide whether the code should operate on decoded text or raw binary data.
Understand The str Versus bytes Split
In Python 3:
- '
strrepresents text' - '
bytesrepresents raw binary data'
Example:
Even though they look similar, Python does not treat them as interchangeable. That is why an operation designed for text can raise:
A Common Case: Writing Bytes To A Text File
This fails:
The file was opened in text mode, so write() expects str.
Fix it by decoding:
Or keep everything binary and open the file in binary mode:
The right choice depends on whether the content is text or binary.
Another Common Case: Mixing Bytes Into String Operations
This also fails:
You cannot concatenate str and bytes.
Fix it by decoding the bytes:
Or keep the entire operation in bytes if you really want binary output:
Consistency is the real rule.
Network And Subprocess Code Often Produces Bytes
Sockets, subprocess output, and many low-level APIs return bytes by default.
Example:
If you treat result.stdout as ordinary text without decoding it first, string operations may raise the error.
That is why it helps to know where bytes naturally enter the program.
Decode Early Or Stay Binary Intentionally
A good design rule is to pick one of these paths:
- decode bytes into text near the boundary and work with
strafterward - keep the pipeline binary all the way through until text is actually needed
What causes bugs is drifting back and forth without deciding.
For example, HTTP libraries, file uploads, or encrypted payloads often belong in bytes for a while. User-facing display and text parsing belong in strings.
Encoding Must Match Reality
Decoding only works if you choose the correct encoding:
If the bytes are not UTF-8 but you decode them as UTF-8 anyway, you may get a UnicodeDecodeError instead of the original TypeError.
So the deeper fix is not merely "call decode." It is "decode with the encoding the bytes were actually created with."
Common Pitfalls
The biggest mistake is blindly sprinkling .decode() or .encode() around the code without deciding which layer should own the conversion.
Another mistake is opening files in text mode and then writing bytes, or opening them in binary mode and then writing strings.
People also forget that many APIs return bytes by default, especially network and subprocess functions, so later string operations fail unexpectedly.
Finally, decoding with the wrong encoding may replace one bug with another. Always know where the bytes came from and what encoding they use.
Summary
- The error means Python expected
strbut receivedbytes. - '
stris text andbytesis raw binary data in Python 3.' - Decode bytes when the program should operate on text.
- Keep data binary when the content is not yet text and use binary file or stream APIs.
- The cleanest fix is to handle text-versus-binary boundaries deliberately instead of mixing the two types accidentally.
Related reading
- Bulk Generate Pre-Signed URLs boto3
- Bulk insert with SQLAlchemy ORM
- Bulk insert with SQLAlchemy ORM
- Bulk package updates using Conda
- Bulk insert with Spring Boot and Spring Data JPA not working
- Bulk processing data through a join with kafka-streams results in `Skipping record for expired segment`
- C-like structures in Python
- C equivalent of rotating a list using python slice operation
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.