{"record":{"id":"7a748f8ace325190","repo":"pola-rs/polars","slug":"empty-data-from-context-hint","errorCode":null,"errorMessage":"empty data from {context}{hint}","messagePattern":"empty data from (.+?)(.+?)","errorType":"exception","errorClass":"NoDataError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/_utils.py","lineNumber":310,"sourceCode":"                    BytesIO(f.read().encode(\"utf8\")),\n                    context=f\"{file!r}\",\n                    raise_if_empty=raise_if_empty,\n                )\n\n    return managed_file(file)\n\n\ndef _check_empty(\n    b: BytesIO, *, context: str, raise_if_empty: bool, read_position: int | None = None\n) -> BytesIO:\n    if raise_if_empty and b.getbuffer().nbytes == 0:\n        hint = (\n            f\" (buffer position = {read_position}; try seek(0) before reading?)\"\n            if context in (\"StringIO\", \"BytesIO\") and read_position\n            else \"\"\n        )\n        msg = f\"empty data from {context}{hint}\"\n        raise NoDataError(msg)\n    return b\n\n\ndef looks_like_url(path: str) -> bool:\n    return re.match(r\"^(ht|f)tps?://\", path, re.IGNORECASE) is not None\n\n\ndef process_file_url(path: str, encoding: str | None = None) -> BytesIO:\n    from urllib.request import urlopen\n\n    with urlopen(path) as f:\n        if not encoding or encoding in {\"utf8\", \"utf8-lossy\"}:\n            return BytesIO(f.read())\n        else:\n            return BytesIO(f.read().decode(encoding).encode(\"utf8\"))\n\n\ndef is_glob_pattern(file: str) -> bool:","sourceCodeStart":292,"sourceCodeEnd":328,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/io/_utils.py#L292-L328","documentation":"Polars' _check_empty raises NoDataError when the bytes read from the source are empty. For StringIO/BytesIO inputs whose read position is not 0, it appends a hint suggesting seek(0), because the buffer looks empty only because it was already consumed.","triggerScenarios":"Passing an in-memory buffer to pl.read_csv/read_json etc. after it was already read (position > 0), or genuinely passing an empty file/buffer/empty downloaded response.","commonSituations":"Reusing a BytesIO across two read calls without rewinding; reading a 0-byte file from disk; an HTTP request returning an empty body; a writer that never flushed to the buffer.","solutions":["Call buffer.seek(0) before passing it to polars.","Verify the producer actually wrote data (check buffer.getbuffer().nbytes or file size).","Create a fresh buffer from the raw bytes for each read call.","If the empty source is expected, guard upstream and skip the read."],"exampleFix":"// before\n buf = io.BytesIO(data)\n other = pl.read_csv(buf)  # position already at end\n// after\n buf = io.BytesIO(data)\n buf.seek(0)\n other = pl.read_csv(buf)","handlingStrategy":"validation","validationCode":"if isinstance(src, (io.BytesIO, io.StringIO)):\n    pos = src.tell()\n    src.seek(0, io.SEEK_END)\n    if src.tell() == 0:\n        raise RuntimeError('source buffer is empty')\n    src.seek(pos)","typeGuard":"def has_data(buf: io.BytesIO) -> bool:\n    pos = buf.tell()\n    buf.seek(0, io.SEEK_END)\n    size = buf.tell()\n    buf.seek(pos)\n    return size > 0","tryCatchPattern":"try:\n    df = pl.read_csv(buf)\nexcept pl.exceptions.NoDataError as e:\n    buf.seek(0)\n    df = pl.read_csv(buf)  # rewind once and retry","preventionTips":["Always seek(0) on in-memory buffers right before handing them to polars.","Never reuse a consumed buffer; construct a fresh one from the raw bytes.","Assert file size / buffer length > 0 in tests that build inputs programmatically."],"tags":["python","io","empty-data","buffer"],"backgroundTag":"empty-required-field","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}