unslothai/unsloth · error · ValueError

Streaming ChatML-to-Alpaca conversion failed on the first ro

Error message

Streaming ChatML-to-Alpaca conversion failed on the first row: {exc}

What it means

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.

Source

Thrown at studio/backend/utils/datasets/format_conversion.py:270

        if num_proc is None or type(num_proc) is not int:
            num_proc = dataset_map_num_proc()
        else:
            num_proc = dataset_map_num_proc(num_proc)

        dataset_map_kwargs["num_proc"] = num_proc
        dataset_map_kwargs["desc"] = "Converting ChatML to Alpaca format"

    result = dataset.map(_convert, **dataset_map_kwargs)

    # For streaming, force the first mapped row through now so any
    # column/format errors surface before training begins (not mid-iteration).
    # IterableDataset re-iterates from the generator source, so this is safe.
    if is_iterable:
        try:
            next(iter(result))
        except Exception as exc:
            raise ValueError(
                f"Streaming ChatML-to-Alpaca conversion failed on the first row: {exc}"
            ) from exc

    return result


def convert_alpaca_to_chatml(
    dataset,
    batch_size = 1000,
    num_proc = None,
):
    """
    Convert Alpaca format to ChatML format.

    Output: 'conversations' column with standard 'role'/'content' dicts.
    """
    is_iterable = is_streaming_dataset(dataset)

View on GitHub (pinned to 203007d190)

Solutions

  1. Inspect the chained exception to find the real per-row cause, then fix the data or the column argument
  2. Preview the first row before converting: print(next(iter(dataset))) and confirm the conversation field name/shape
  3. Pass chat_column explicitly rather than relying on the messages/conversations/texts fallback on streaming data

Example fix

# before
result = convert_chatml_to_alpaca(stream_ds)  # raises, chained KeyError('role')

# after
row = next(iter(stream_ds))
print(row.keys(), row['chat'][0])  # confirm column + turn shape
result = convert_chatml_to_alpaca(stream_ds, chat_column='chat')
Defensive patterns

Strategy: try-catch

Validate before calling

def first_row_convertible(stream_ds, chat_column: str | None = None) -> bool:
    row = next(iter(stream_ds), None)
    if row is None:
        return False
    col = chat_column or next(
        (c for c in ("messages", "conversations", "texts") if c in row), None
    )
    if col is None:
        return False
    turns = row[col] or []
    return bool(turns) and isinstance(turns[0], dict)

Try / catch

try:
    result = convert_chatml_to_alpaca(stream_ds, chat_column=col)
except ValueError as e:
    if "failed on the first row" in str(e):
        cause = e.__cause__  # real per-row error (KeyError/TypeError...)
        log_and_surface(cause)
    raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of unslothai/unsloth@203007d190 (2026-08-15). Data as JSON: /api/errors/9bcacfff5153f9ce. Report an issue: GitHub.