{"record":{"id":"45c44acdb012561d","repo":"unslothai/unsloth","slug":"streaming-alpaca-to-chatml-conversion-failed-on-th","errorCode":null,"errorMessage":"Streaming Alpaca-to-ChatML conversion failed on the first row: {exc}","messagePattern":"Streaming Alpaca-to-ChatML conversion failed on the first row: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"studio/backend/utils/datasets/format_conversion.py","lineNumber":336,"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 Alpaca to ChatML 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 Alpaca-to-ChatML conversion failed on the first row: {exc}\"\n            ) from exc\n\n    return result\n\n\ndef _format_eta(seconds):\n    \"\"\"Format seconds into a human-readable ETA string.\"\"\"\n    if seconds < 60:\n        return f\"{seconds:.0f}s\"\n    elif seconds < 3600:\n        m, s = divmod(int(seconds), 60)\n        return f\"{m}m {s}s\"\n    else:\n        h, remainder = divmod(int(seconds), 3600)\n        m, _ = divmod(remainder, 60)\n        return f\"{h}h {m}m\"\n","sourceCodeStart":318,"sourceCodeEnd":354,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/utils/datasets/format_conversion.py#L318-L354","documentation":"ValueError raised eagerly when convert_alpaca_to_chatml runs on a streaming dataset: the first mapped row is forced through so errors surface at setup time, not mid-training. The chained exception (from exc) holds the real cause — typically the Alpaca source rows missing the instruction/output keys that _convert reads when building conversations. Safe on streams because IterableDataset re-iterates from its generator.","triggerScenarios":"Calling convert_alpaca_to_chatml on an IterableDataset whose rows lack Alpaca fields ('instruction'/'output' or equivalents), or whose first row has them as None/incorrect types so the per-row conversion code throws.","commonSituations":"Streaming a chat-format dataset through the Alpaca->ChatML converter by mistake; Alpaca variants using 'prompt'/'response' key names instead of instruction/output; upstream schema changes after the stream was opened.","solutions":["Read the chained exc to identify the missing field or type error, then fix the source data or key mapping","Confirm the dataset really is Alpaca: print(next(iter(dataset)).keys()) should show instruction/output-style fields","Rename variant keys first (e.g. rename_column('prompt','instruction')) or pre-map rows to the expected Alpaca schema before converting"],"exampleFix":"# before\nresult = convert_alpaca_to_chatml(stream_ds)  # rows use prompt/response -> chained error\n\n# after\nstream_ds = stream_ds.rename_column('prompt', 'instruction').rename_column('response', 'output')\nresult = convert_alpaca_to_chatml(stream_ds)","handlingStrategy":"try-catch","validationCode":"def first_row_is_alpaca(stream_ds) -> bool:\n    row = next(iter(stream_ds), None)\n    if row is None:\n        return False\n    return \"instruction\" in row and \"output\" in row","typeGuard":"def is_alpaca_row(row: dict) -> bool:\n    return isinstance(row, dict) and \"instruction\" in row and \"output\" in row","tryCatchPattern":"try:\n    result = convert_alpaca_to_chatml(stream_ds)\nexcept ValueError as e:\n    if \"failed on the first row\" in str(e):\n        cause = e.__cause__  # e.g. KeyError('instruction') on prompt/response schemas\n        log_and_surface(cause)\n    raise","preventionTips":["Verify the stream really is Alpaca (instruction/output fields) before converting","Rename variant keys (prompt/response -> instruction/output) up front","Diagnose via the chained __cause__; the wrapper only tells you it failed on row 0"],"tags":["datasets","streaming","format-conversion","chatml","fail-fast"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}