{"record":{"id":"6851d63f166c04b2","repo":"numpy/numpy","slug":"fromstring-needs-a-dtype-or-formats-argument","errorCode":null,"errorMessage":"fromstring() needs a 'dtype' or 'formats' argument","messagePattern":"fromstring\\(\\) needs a 'dtype' or 'formats' argument","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"numpy/_core/records.py","lineNumber":810,"sourceCode":"            dtype=[('f0', 'u1'), ('f1', 'u1'), ('f2', 'u1'), ('f3', 'S3')])\n\n    >>> grades_dtype = [('Name', (np.str_, 10)), ('Marks', np.float64),\n    ...                 ('GradeLevel', np.int32)]\n    >>> grades_array = np.array([('Sam', 33.3, 3), ('Mike', 44.4, 5),\n    ...                         ('Aadi', 66.6, 6)], dtype=grades_dtype)\n    >>> np.rec.fromstring(grades_array.tobytes(), dtype=grades_dtype)\n    rec.array([('Sam', 33.3, 3), ('Mike', 44.4, 5), ('Aadi', 66.6, 6)],\n            dtype=[('Name', '<U10'), ('Marks', '<f8'), ('GradeLevel', '<i4')])\n\n    >>> s = '\\x01\\x02\\x03abc'\n    >>> np.rec.fromstring(s, dtype='u1,u1,u1,S3')\n    Traceback (most recent call last):\n       ...\n    TypeError: a bytes-like object is required, not 'str'\n    \"\"\"\n\n    if dtype is None and formats is None:\n        raise TypeError(\"fromstring() needs a 'dtype' or 'formats' argument\")\n\n    if dtype is not None:\n        descr = sb.dtype(dtype)\n    else:\n        descr = format_parser(formats, names, titles, aligned, byteorder).dtype\n\n    itemsize = descr.itemsize\n\n    # NumPy 1.19.0, 2020-01-01\n    shape = _deprecate_shape_0_as_None(shape)\n\n    if shape in (None, -1):\n        shape = (len(datastring) - offset) // itemsize\n\n    _array = recarray(shape, descr, buf=datastring, offset=offset)\n    return _array\n\ndef get_remaining_size(fd):","sourceCodeStart":792,"sourceCodeEnd":828,"githubUrl":"https://github.com/numpy/numpy/blob/e117b3ca4edacf581f440dccfd7f3242f0312afa/numpy/_core/records.py#L792-L828","documentation":"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.","triggerScenarios":"Calling np.rec.fromstring(datastring) or np.rec.fromstring(buf, formats=None, dtype=None) with neither a dtype nor a formats argument.","commonSituations":"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.","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."],"exampleFix":"// before\nnp.rec.fromstring(buf)\n// after\nnp.rec.fromstring(buf, dtype='u1,u1,u1,S3')","handlingStrategy":"validation","validationCode":"if dtype is None and formats is None:\n    raise TypeError('provide dtype= or formats= to np.rec.fromstring')","typeGuard":"def has_layout(dtype, formats):\n    return dtype is not None or formats is not None","tryCatchPattern":null,"preventionTips":["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."],"tags":["numpy","records","fromstring","missing-argument"],"analyzedSha":"e117b3ca4edacf581f440dccfd7f3242f0312afa","analyzedAt":"2026-08-07T01:25:31.049Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}