{"record":{"id":"846669722e75f5d5","repo":"hiyouga/LlamaFactory","slug":"no-valid-messages-or-chosen-messages-rejected-mess","errorCode":null,"errorMessage":"No valid messages or chosen_messages/rejected_messages found in sample.","messagePattern":"No valid messages or chosen_messages/rejected_messages found in sample\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/v1/core/rendering/rendering.py","lineNumber":313,"sourceCode":"                # chosen and rejected are independent sequences; position ids must restart at 1 for\n                # each (a single continuous range would offset rejected's positional embeddings).\n                model_input[\"position_ids\"] = list(range(1, len(chosen_input[\"input_ids\"]) + 1)) + list(\n                    range(1, len(rejected_input[\"input_ids\"]) + 1)\n                )\n\n                for key in _MULTIMODAL_PASSTHROUGH_KEYS:\n                    tensors = [inp[key] for inp in (chosen_input, rejected_input) if key in inp]\n                    if tensors:\n                        model_input[key] = torch.cat(tensors, dim=0)\n\n                if \"mm_token_type_ids\" in chosen_input or \"mm_token_type_ids\" in rejected_input:\n                    chosen_mm = chosen_input.get(\"mm_token_type_ids\", [0] * len(chosen_input[\"input_ids\"]))\n                    rejected_mm = rejected_input.get(\"mm_token_type_ids\", [0] * len(rejected_input[\"input_ids\"]))\n                    model_input[\"mm_token_type_ids\"] = chosen_mm + rejected_mm\n\n                rendered.append(model_input)\n            else:\n                raise ValueError(\"No valid messages or chosen_messages/rejected_messages found in sample.\")\n\n            for model_input in rendered:\n                if \"extra_info\" in sample:\n                    model_input[\"extra_info\"] = sample[\"extra_info\"]\n                if \"_dataset_name\" in sample:\n                    model_input[\"_dataset_name\"] = sample[\"_dataset_name\"]\n                model_inputs.append(model_input)\n\n        return model_inputs\n","sourceCodeStart":295,"sourceCodeEnd":323,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/v1/core/rendering/rendering.py#L295-L323","documentation":"The renderer's batch path looks for 'messages' (plain SFT/generation) or 'chosen_messages'/'rejected_messages' (preference pairs like DPO/KTO) in each sample. If none are present the sample has no renderable content and a ValueError is raised instead of silently emitting an empty batch.","triggerScenarios":"Passing a dataset of samples that only contain pre-tokenized fields (input_ids), raw 'conversations'/'messages' under a different key name, or a preference sample with only one of chosen/rejected; an empty dict sample.","commonSituations":"Custom dataset converters that keep the original column name ('conversation', 'chat', 'history') instead of the expected keys; mixed-format datasets where some rows lack the fields.","solutions":["Rename/emit the expected key: 'messages' for standard samples, or both 'chosen_messages' and 'rejected_messages' for preference samples","Filter malformed samples during preprocessing: keep only samples containing at least one of the accepted key sets","If your data is pre-tokenized, bypass this renderer path rather than feeding token ids as messages"],"exampleFix":"# before\nsample = {\"conversation\": [{\"role\": \"user\", \"content\": \"hi\"}, ...]}\n\n# after\nsample = {\"messages\": [{\"role\": \"user\", \"content\": \"hi\"}, ...]}","handlingStrategy":"validation","validationCode":"def sample_is_renderable(sample: dict) -> bool:\n    return \"messages\" in sample or (\"chosen_messages\" in sample and \"rejected_messages\" in sample)","typeGuard":"def is_renderable_sample(sample: dict) -> bool:\n    return (\n        isinstance(sample, dict)\n        and (\"messages\" in sample\n             or (\"chosen_messages\" in sample and \"rejected_messages\" in sample))\n    )","tryCatchPattern":null,"preventionTips":["Emit 'messages' (or both chosen_messages and rejected_messages) from custom converters","Filter datasets with sample_is_renderable before training to fail with row context"],"tags":["data-format","dataset","preference-data","rendering"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}