numpy/numpy · error · TypeError
fromstring() needs a 'dtype' or 'formats' argument
Error message
fromstring() needs a 'dtype' or 'formats' argument
What it means
Raised by numpy.rec.fromstring because decoding a binary buffer into a record array requires a structured layout. You must supply either a dtype or the formats/names/titles set so NumPy knows how to interpret the bytes.
Source
Thrown at numpy/_core/records.py:810
dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')])
>>> grades_dtype = [('Name', (np.str_, 10)), ('Marks', np.float64),
... ('GradeLevel', np.int32)]
>>> grades_array = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5),
... ('Aadi', 66.6, 6)], dtype=grades_dtype)
>>> np.rec.fromstring(grades_array.tobytes(), dtype=grades_dtype)
rec.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6)],
dtype=[('Name', '<U10'), ('Marks', '<f8'), ('GradeLevel', '<i4')])
>>> s = '\x01\x02\x03abc'
>>> np.rec.fromstring(s, dtype='u1,u1,u1,S3')
Traceback (most recent call last):
...
TypeError: a bytes-like object is required, not 'str'
"""
if dtype is None and formats is None:
raise TypeError("fromstring() needs a 'dtype' or 'formats' argument")
if dtype is not None:
descr = sb.dtype(dtype)
else:
descr = format_parser(formats, names, titles, aligned, byteorder).dtype
itemsize = descr.itemsize
# NumPy 1.19.0, 2020-01-01
shape = _deprecate_shape_0_as_None(shape)
if shape in (None, -1):
shape = (len(datastring) - offset) // itemsize
_array = recarray(shape, descr, buf=datastring, offset=offset)
return _array
def get_remaining_size(fd):View on GitHub (pinned to e117b3ca4e)
Solutions
- Pass dtype= a valid structured dtype, e.g. np.rec.fromstring(buf, dtype='u1,u1,u1,S3').
- Or pass formats= (plus names=) so NumPy builds the dtype via format_parser.
- Remember the buffer must be bytes-like; if you have a str, encode it first.
Example fix
// before np.rec.fromstring(buf) // after np.rec.fromstring(buf, dtype='u1,u1,u1,S3')
Defensive patterns
Strategy: validation
Validate before calling
if dtype is None and formats is None:
raise TypeError('provide dtype= or formats= to np.rec.fromstring') Type guard
def has_layout(dtype, formats):
return dtype is not None or formats is not None Prevention
- Always pair a binary buffer with an explicit structured dtype.
- Centralize fromstring calls behind a helper that requires a dtype argument.
- Ensure the buffer is bytes-like, not str.
When it happens
Trigger: Calling np.rec.fromstring(datastring) or np.rec.fromstring(buf, formats=None, dtype=None) with neither a dtype nor a formats argument.
Common situations: Omitting the dtype after refactoring a call that previously inferred it; copy-pasting a frombuffer call and forgetting to carry over the dtype; assuming fromstring can auto-detect the layout from the bytes.
Related errors
- fromfile() needs a 'dtype' or 'formats' argument
- Must define formats (or dtype) if object is None, string, or
- Must define a shape if obj is None
- array-shape mismatch in array {k} ("{name}")
- Can only deal with 1-d array.
AI-assisted analysis of numpy/numpy@e117b3ca4e (2026-08-07).
Data as JSON: /api/errors/6851d63f166c04b2.
Report an issue: GitHub.