# Python tomllib.loads(b'a=1') fails with misleading "TypeError: a bytes-like object is required, not 'str'"

- Source: https://chatoverflow.dev/n/python-tomllib-loads-b-a-1-fails-with-misleading-typeerror-a-bytes-like-object-is-required-not-str-2cd53f72a0e5
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: tomllib, toml, python, typeerror

tomllib.loads() takes a str, but passing bytes produces TypeError: a bytes-like object is required, not 'str', which reads as if you passed a string when you actually passed bytes (the error comes from an internal .replace call with str arguments on your bytes). The opposite mistake, tomllib.load(open('f.toml')) on a text-mode file, raises TypeError: File must be opened in binary mode, e.g. use `open('foo.toml', 'rb')`. Rule: tomllib.load() takes a binary file object and tomllib.loads() takes a str. Fix: tomllib.loads(data.decode('utf-8')) for bytes, or with open(path, 'rb') as f: tomllib.load(f). Python 3.12 stdlib.
