Skip to content

Stop to_sharegpt blanking the input column when no merged_prompt is given - #8277

Merged
oobabooga merged 3 commits into
unslothai:mainfrom
vineethsaivs:fix/to-sharegpt-blank-input-column
Aug 25, 2026
Merged

oobabooga merged 3 commits into
unslothai:mainfrom
vineethsaivs:fix/to-sharegpt-blank-input-column

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

Problem

merged_prompt is optional. Its docstring describes it as the prompt used to "merge multiple columns into 1 input", and merged_column_name as the "final column name for the input field", so with no merged_prompt the input column is simply the one already in the dataset.

The merging map runs unconditionally though:

possible_columns, final_optional_prompts = _parse_combined_prompt(merged_prompt, dataset)
formatter = _create_formatter(possible_columns, final_optional_prompts, merged_column_name)
dataset = dataset.map(formatter, batched = True, desc = "Merging columns")

With the signature's own default merged_prompt = "", _parse_combined_prompt returns no columns and a single empty prompt part, so the formatter emits "" for every row and the map overwrites merged_column_name with empty strings. __convert_to_sharegpt__ then reads that column and emits a blank human turn.

Repro

An Alpaca-shaped dataset, with instruction and output being exactly to_sharegpt's two defaults, and every argument left alone:

ds = Dataset.from_dict({"instruction": ["What is 2+2?", "Capital of France?"],
                        "output": ["4", "Paris"]})
to_sharegpt(ds)

# [{'from': 'human', 'value': ''}, {'from': 'gpt', 'value': '4'}]
# [{'from': 'human', 'value': ''}, {'from': 'gpt', 'value': 'Paris'}]

The assistant turns are intact, so the dataset looks structurally fine and only the prompts are gone. Nothing errors and nothing warns, and a model finetuned on it learns to answer without reading its input.

Passing merged_prompt="{instruction}" works correctly, so it is only the documented default path that is affected.

Worth linking: #1769 is a user hitting exactly this shape, merged_prompt commented out with merged_column_name="Query" and output_column_name="Answer". At the time it produced an empty dataset, which at least failed loudly downstream. It was closed as completed. Re-running that same shape today gives blank human turns instead, so the loud failure became a silent one.

Fix

Skip the merge when there is no merged_prompt.

A dataset with neither a merged_prompt nor the named input column previously got a column of empty strings; it now raises naming both the column it wanted and the columns the dataset has, which is the case that used to be silent.

Test

tests/python/test_to_sharegpt_optional_none.py is the mapped test file, from the earlier optional-block fix. It loads the helpers out of the source with ast rather than importing unsloth, since importing the package needs unsloth_zoo, and the new tests use the same trick to pull in to_sharegpt itself.

Five tests added: the all-defaults case, renamed columns (#1769's shape), that an explicit merged_prompt still merges across columns, that a missing input column names itself, and that conversation_extension still carries real prompts.

                                                       before   after
test_default_merged_prompt_keeps_the_input_column       FAIL     pass
test_default_merged_prompt_with_renamed_columns         FAIL     pass
test_missing_input_column_says_which_column_is_missing  FAIL     pass
test_conversation_extension_keeps_the_real_prompts      FAIL     pass
test_explicit_merged_prompt_still_merges                pass     pass
the 6 pre-existing tests in the file                    pass     pass

python -m pytest tests/python/test_to_sharegpt_optional_none.py gives 4 failed / 7 passed against main and 11 passed with this change. Both runs were done against files fetched fresh from main at b063387, so the before column is a real upstream control rather than a local tree.

One related thing found while testing and deliberately left out of this PR: to_sharegpt(remove_unused_columns=False, conversation_extension=3) dies with a raw datasets error about duplicated columns, because the extension path renames conversations and then concatenates while the original columns are still present. Different mechanism and a different fix; happy to open it separately or fold it in here, whichever you prefer.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7f97b3ad48

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/chat_templates.py
possible_columns, final_optional_prompts = _parse_combined_prompt(merged_prompt, dataset)
formatter = _create_formatter(possible_columns, final_optional_prompts, merged_column_name)
dataset = dataset.map(formatter, batched = True, desc = "Merging columns")
if merged_prompt:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve None normalization when skipping the merge

When no merged_prompt is supplied and the existing input column contains nulls, this new branch bypasses _create_formatter, which is currently the only path that coalesces None to ""; __convert_to_sharegpt__ then emits str(user), so the human turn becomes the literal string "None". The same row still renders as empty with merged_prompt = "{instruction}", so the default no-merge path can now leak None tokens into training data unless the source column is normalized before conversion or None is handled in __convert_to_sharegpt__.

Useful? React with 👍 / 👎.

@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Good catch, and it is worse than reported, so thanks. Fixed in c956dc3.

You are right that skipping the merge leaves the null in place and __convert_to_sharegpt__ calls str() on it, so the human turn became the literal "None". Confirmed on this branch:

input : [{'instruction': 'What is 2+2?', 'output': '4'}, {'instruction': None, 'output': 'Paris'}]

no merged_prompt        -> {'from': 'human', 'value': 'What is 2+2?'} / {'from': 'human', 'value': 'None'}
merged_prompt='{instruction}' -> {'from': 'human', 'value': 'What is 2+2?'} / {'from': 'human', 'value': ''}

The part you did not mention is that the output column has always leaked it, because it never went through _create_formatter on either path:

to_sharegpt({'instruction': ['hi'], 'output': [None]}, merged_prompt='{instruction}')
-> [{'from': 'human', 'value': 'hi'}, {'from': 'gpt', 'value': 'None'}]

That one predates this PR. So rather than normalize the input column before conversion, I handled null in the one place that builds the turns, which fixes what I introduced and the pre-existing sibling together. It is also the same defect class as #6827, which fixed the "None" leak in the optional-block path, so this keeps all three consistent.

Two tests added: that no turn is ever the string "None" for a mixed null input and null output, and that the no-merge path and the merged_prompt path produce identical conversations for the same rows. Both fail on the previous head of this branch and pass now; the file goes 2 failed / 11 passed to 13 passed.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed this on main, where the merge map at chat_templates.py:2315 runs even with the default empty merged_prompt and silently blanks every human turn, and the null-cell handling looks right too. Backend CI on Python 3.11 and Core (HF=latest + TRL=latest) are green on main but red here, so could you take a look at those before I review.

…iven

merged_prompt is optional: without one, merged_column_name names a column that
is already in the dataset. The column-merging map ran anyway, and with an empty
merged_prompt _create_formatter produces a processor that returns "" for every
row, so the map overwrote merged_column_name with empty strings.

to_sharegpt(dataset) on an Alpaca-shaped dataset, with every argument left at
its documented default, therefore produced conversations whose every human turn
was "". The assistant turns survive, so the result looks structurally valid and
only the prompts are gone; nothing errors and nothing warns, and a model
finetuned on it learns to ignore its input.

Skip the merge when there is no merged_prompt. A dataset that has neither a
merged_prompt nor the named input column now raises naming both, rather than
silently producing blank turns.
@vineethsaivs
vineethsaivs force-pushed the fix/to-sharegpt-blank-input-column branch from 1b456a8 to 22a26ff Compare August 19, 2026 17:34
Skipping the merge left the input column untouched, and __convert_to_sharegpt__
calls str() on it, so a null cell became the literal string "None" in the human
turn. _create_formatter coalesces null to empty on the merged path, so the two
paths disagreed on the same rows.

The output column never went through the formatter at all, so a null there
rendered as "None" with or without a merged_prompt. Handle both in the one place
that builds the turns.
@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Thanks. Those runs are from 2026-08-09, when this branch was cut at b063387, and GitHub has already expired their logs (HTTP 410 on the run archive), so there is nothing left to read on them directly.

What I can say from the tree: unsloth/chat_templates.py and tests/python/test_to_sharegpt_optional_none.py are byte-identical between b063387 and current main, so nothing this PR touches has moved. The base underneath it has, by about ten days.

So I have rebased onto current main (e99dfe5). Still the same two commits, still the same two files, +126/-5, and the pre-commit autofix commits are folded in rather than carried. CI is re-running against a base that is not ten days old, which should separate "this branch is broken" from "that base was".

Re-measured on the rebased tree, against chat_templates.py exactly as it is on main:

# main's chat_templates.py + this branch's test file
$ python -m pytest tests/python/test_to_sharegpt_optional_none.py -q
6 failed, 7 passed
FAILED ...::test_default_merged_prompt_does_not_blank_the_input_column
FAILED ...::test_default_merged_prompt_with_renamed_columns
FAILED ...::test_missing_input_column_says_which_column_is_missing
FAILED ...::test_conversation_extension_keeps_the_real_prompts
FAILED ...::test_null_cells_do_not_render_as_the_word_none
FAILED ...::test_null_cells_match_the_merged_prompt_path

# this branch
$ python -m pytest tests/python/test_to_sharegpt_optional_none.py -q
13 passed

The seven that pass either way are the controls for the behaviour that must not move.

If the new run is still red on those jobs I will dig into the fresh logs, but I did not want to guess at expired ones.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for the rebase, and I confirmed the underlying problem is still on main at chat_templates.py:2315 where the merge map runs with the default empty merged_prompt and blanks the input column, plus the null cells rendering as "None" at 2329.

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 03c0f8dcc3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@oobabooga

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: ca0c946028

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@vineethsaivs

Copy link
Copy Markdown
Contributor Author

Addressed already, flagging it so the thread is not left looking open.

This was raised against 7f97b3ad, the first commit, and it was right about that commit: skipping _create_formatter did bypass the only place that coalesced None to "", so a null input cell would have rendered as the literal "None".

The current head fixes it at the point where the turn is built, which also covers a case the merged path never did:

{"from" : "human", "value" : "" if user      is None else str(user)     },
{"from" : "gpt",   "value" : "" if assistant is None else str(assistant)},

The gpt half matters too: the output column never went through _create_formatter on either path, so a null there leaked "None" into training data before this PR as well.

Codex's own re-review of the current head, ca0c9460, reports no major issues.

@oobabooga

Copy link
Copy Markdown
Member

Thanks @vineethsaivs. Confirmed the default to_sharegpt(dataset) path was silently changing a valid user turn from:

{"from": "human", "value": "What is 2+2?"}

to:

{"from": "human", "value": ""}

The fix preserves the existing input when no merge template is supplied, keeps explicit column merging intact, and converts null turns to empty text instead of "None". The focused tests pass 13/13, the adjacent chat-template tests pass 216/216, and the PR checks are green.

@oobabooga
oobabooga merged commit 74371a3 into unslothai:main Aug 25, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants