{"record":{"id":"730374e53408c8ec","repo":"sgl-project/sglang","slug":"name-must-be-a-tensor-list-of-tensors-or-none","errorCode":null,"errorMessage":"{name} must be a tensor, list of tensors, or None.","messagePattern":"(.+?) must be a tensor, list of tensors, or None\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/utils/condition_expansion.py","lineNumber":53,"sourceCode":"        if current_batch_size == self.sample_batch_size:\n            return value\n        if current_batch_size != self.prompt_batch_size:\n            raise ValueError(\n                f\"{name} has batch dim {current_batch_size} (shape \"\n                f\"{tuple(value.shape)}); expected {self.prompt_batch_size} \"\n                f\"(per-prompt) or {self.sample_batch_size} (per-sample).\"\n            )\n        repeats = self.sample_batch_size // self.prompt_batch_size\n        return value.repeat_interleave(repeats, dim=0)\n\n    def _expand_tensors(self, value, name: str):\n        \"\"\"Expand a tensor or each tensor in a list, preserving its container.\"\"\"\n        if value is None:\n            return None\n        if isinstance(value, torch.Tensor):\n            return self._expand_tensor(value, name)\n        if not isinstance(value, list):\n            raise TypeError(f\"{name} must be a tensor, list of tensors, or None.\")\n        if any(\n            item is not None and not isinstance(item, torch.Tensor) for item in value\n        ):\n            raise TypeError(f\"{name} entries must be tensors or None.\")\n        return [\n            self._expand_tensor(item, f\"{name}[{index}]\") if item is not None else None\n            for index, item in enumerate(value)\n        ]\n\n    def _expand_sequence_lengths(\n        self, value: list[list[int] | None] | None, name: str\n    ) -> list[list[int] | None] | None:\n        if value is None:\n            return None\n        repeats = self.sample_batch_size // self.prompt_batch_size\n        expanded = []\n        for index, sequence_lengths in enumerate(value):\n            if (","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/utils/condition_expansion.py#L35-L71","documentation":"Thrown by ConditionExpansion._expand_tensors when the field value is not None, not a torch.Tensor, and not a list — only tensors, lists of tensors (entries may be None), and None are expandable conditioning values.","triggerScenarios":"Passing a float, numpy array, string, or dict to expand_field — e.g. a numpy conditioning array or a scalar guidance scale.","commonSituations":"Numpy-based pipelines feeding np.ndarray conditioning; scalar per-batch hyperparameters mistakenly routed through expand_field.","solutions":["Convert the value to a torch.Tensor first: torch.as_tensor(value)","Keep scalars/non-tensor metadata out of expand_field; handle them separately","If using numpy arrays anywhere in conditioning, add an explicit torch.from_numpy conversion at the boundary"],"exampleFix":"# before\nexpand.expand_field(np_array, \"condition\")\n# after\nexpand.expand_field(torch.from_numpy(np_array), \"condition\")","handlingStrategy":"type-guard","validationCode":"import torch\ndef is_expandable(v) -> bool:\n    return v is None or isinstance(v, torch.Tensor) or (isinstance(v, list) and all(i is None or isinstance(i, torch.Tensor) for i in v))","typeGuard":"def is_expandable(v) -> bool:\n    import torch\n    return v is None or isinstance(v, torch.Tensor) or (isinstance(v, list) and all(i is None or isinstance(i, torch.Tensor) for i in v))","tryCatchPattern":"try:\n    out = exp.expand_field(value, name)\nexcept TypeError:\n    out = exp.expand_field(torch.as_tensor(value), name)","preventionTips":["Convert numpy arrays to torch tensors at the pipeline boundary","Route scalars through a separate path instead of expand_field"],"tags":["batching","type-error","validation"],"backgroundTag":"type-validation-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}