Repository navigation
fix(calibration): keep messages conversations from numpy/pandas slices (#4936) - #4994
Merged
Merged
Conversation
A `messages` column sliced from a numpy- or pandas-formatted HF dataset
(`with_format("numpy")[:]`, `with_format("pandas")[:]`) was read as one
row: `_is_columnar_mapping` only accepted a list/tuple entry, while numpy
gives a 1-D object array of conversations (or a 2-D object array of turns
when all conversations are equal length) and pandas a Series of arrays.
The whole column then went to `apply_chat_template` as a single
"conversation" and rendered only the template's default system turn, so
calibration silently collapsed to one prompt (#4936).
- classify an ndarray/Series `messages` entry as a conversation container;
- recursively copy rows into plain list/dict/str/number containers before
templating (ndarray, Series, DataFrame rows, record arrays, pyarrow
scalars); torch tensors stay untouched and the caller's slice is not
mutated;
- carry `tools`/`documents` through columnar mappings so a sliced row
passes the same chat-template inputs a plain row does (#4858).
Tests cover `[:]`, numpy/pandas/torch/arrow slices, 2-D object arrays,
plain iteration, tools/documents columns, multi-part content, and a real
Qwen2.5 tokenizer.
This was referenced Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4936
Follow-up to #4902 (fix of #4882).
Root cause
_is_columnar_mappingclassified amessagesmapping as a single row unlessentries[0]was alist/tuple.with_format("numpy")[:]yields a 1-Dndarray(dtype=object)of conversations (or a 2-D object array of turns when all conversations have equal length), and pandas yields aSeriesof arrays — bothndarray, so the whole column went toapply_chat_templateas one "conversation". The template rendered only its default system turn and the data was silently dropped.Repro before the fix: reference
6 rows / 312 tokens; numpy1 / 21; pandas1 / 21; the decoded prompt was the system turn only.A second gap surfaced while testing:
_columnar_mapping_examplescopied only_CALIBRATION_COLUMNS, sotools/documentswere dropped from columnar rows — the #4858 forwarding only worked for plain dict rows. Fixed too.Changes
_is_conversation_entry()acceptsndarray/Series/sequence entries as conversations, while a single-row mapping (entries starting with a role mapping) still classifies as one row._to_python_containers()recursively copies rows into plainlist/dict/str/number containers: mappings, numpy arrays (including 2-D object arrays), pandasSeries, pandasDataFramerows (to_dict("list")), structured/record arrays (dtype.names,np.void), numpy scalars, and pyarrow scalars (as_py).torch.Tensoris returned untouched and caller slices are not mutated.prepare_calibration_datasetbefore templating, so all input shapes share one path._columnar_mapping_examplesnow also carries_TEMPLATE_INPUT_COLUMNS = ("tools", "documents").Verification
tests/test_calibration_messages_formats_4936.py: 12 passed —[:]plus numpy/pandas/torch/arrow, 2-D object array, iteration, tools, documents, multi-partcontent, no-mutation, single-row mapping, and a real Qwen2.5 tokenizer.origin/main): 6 failed, 5 passed (numpy/pandas/torch/arrow slices, 2-D numpy, real Qwen).tests/test_calibration_*.py tests/test_prepare_dataset.py: 262 passed, 5 skipped.main:tests/test_calibration_messages_formats_4936.py tests/test_prepare_dataset.py→ 33 passed.ruff check gptqmodel tests: clean.Residual risk
as_py) is implemented but not directly unit-tested.conversationspipeline (batched_conversations/normalize_chat_calibration_sample) is untouched.tools/documentsare still dropped from columnar mappings (pre-existing).