{"record":{"id":"9bcacfff5153f9ce","repo":"unslothai/unsloth","slug":"streaming-chatml-to-alpaca-conversion-failed-on-th","errorCode":null,"errorMessage":"Streaming ChatML-to-Alpaca conversion failed on the first row: {exc}","messagePattern":"Streaming ChatML-to-Alpaca conversion failed on the first row: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"studio/backend/utils/datasets/format_conversion.py","lineNumber":270,"sourceCode":"\n        if num_proc is None or type(num_proc) is not int:\n            num_proc = dataset_map_num_proc()\n        else:\n            num_proc = dataset_map_num_proc(num_proc)\n\n        dataset_map_kwargs[\"num_proc\"] = num_proc\n        dataset_map_kwargs[\"desc\"] = \"Converting ChatML to Alpaca format\"\n\n    result = dataset.map(_convert, **dataset_map_kwargs)\n\n    # For streaming, force the first mapped row through now so any\n    # column/format errors surface before training begins (not mid-iteration).\n    # IterableDataset re-iterates from the generator source, so this is safe.\n    if is_iterable:\n        try:\n            next(iter(result))\n        except Exception as exc:\n            raise ValueError(\n                f\"Streaming ChatML-to-Alpaca conversion failed on the first row: {exc}\"\n            ) from exc\n\n    return result\n\n\ndef convert_alpaca_to_chatml(\n    dataset,\n    batch_size = 1000,\n    num_proc = None,\n):\n    \"\"\"\n    Convert Alpaca format to ChatML format.\n\n    Output: 'conversations' column with standard 'role'/'content' dicts.\n    \"\"\"\n    is_iterable = is_streaming_dataset(dataset)\n","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/utils/datasets/format_conversion.py#L252-L288","documentation":"ValueError raised eagerly when convert_chatml_to_alpaca runs on a streaming dataset: the first mapped row is pulled through (next(iter(result))) so per-row failures surface before training instead of mid-iteration. The underlying cause is chained (from exc) — typically _convert hitting a missing conversation column or malformed turns (non-dict messages, missing role/from keys) in row 0. The probe is safe because IterableDataset re-iterates from its source generator.","triggerScenarios":"Calling convert_chatml_to_alpaca on an IterableDataset whose rows lack 'messages'/'conversations'/'texts' (or the passed chat_column), or whose first conversation contains turns that are strings/None instead of dicts with role/from keys.","commonSituations":"Streaming conversions of hub datasets with non-standard column names or mixed-quality first shards; schema drift after upstream producers renamed fields; a chat_column typo that only fails once rows are actually read.","solutions":["Inspect the chained exception to find the real per-row cause, then fix the data or the column argument","Preview the first row before converting: print(next(iter(dataset))) and confirm the conversation field name/shape","Pass chat_column explicitly rather than relying on the messages/conversations/texts fallback on streaming data"],"exampleFix":"# before\nresult = convert_chatml_to_alpaca(stream_ds)  # raises, chained KeyError('role')\n\n# after\nrow = next(iter(stream_ds))\nprint(row.keys(), row['chat'][0])  # confirm column + turn shape\nresult = convert_chatml_to_alpaca(stream_ds, chat_column='chat')","handlingStrategy":"try-catch","validationCode":"def first_row_convertible(stream_ds, chat_column: str | None = None) -> bool:\n    row = next(iter(stream_ds), None)\n    if row is None:\n        return False\n    col = chat_column or next(\n        (c for c in (\"messages\", \"conversations\", \"texts\") if c in row), None\n    )\n    if col is None:\n        return False\n    turns = row[col] or []\n    return bool(turns) and isinstance(turns[0], dict)","typeGuard":null,"tryCatchPattern":"try:\n    result = convert_chatml_to_alpaca(stream_ds, chat_column=col)\nexcept ValueError as e:\n    if \"failed on the first row\" in str(e):\n        cause = e.__cause__  # real per-row error (KeyError/TypeError...)\n        log_and_surface(cause)\n    raise","preventionTips":["Pull and print the first stream row before converting to verify the column and turn shape","Rely on the chained exception, not the wrapper text, for diagnosis","On streams, pass chat_column explicitly instead of trusting fallback names"],"tags":["datasets","streaming","format-conversion","alpaca","fail-fast"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}