{"record":{"id":"374cef7c2a868db8","repo":"sgl-project/sglang","slug":"must-provide-either-named-tensors-or-both-flattene","errorCode":null,"errorMessage":"Must provide either named_tensors or both flattened_tensor and metadata","messagePattern":"Must provide either named_tensors or both flattened_tensor and metadata","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/srt/weight_sync/tensor_bucket.py","lineNumber":76,"sourceCode":"\n                numel = flattened.numel()\n                metadata_obj = FlattenedTensorMetadata(\n                    name=name,\n                    shape=tensor.shape,\n                    dtype=tensor.dtype,\n                    start_idx=current_idx,\n                    end_idx=current_idx + numel,\n                    numel=numel,\n                )\n                self.metadata[i] = metadata_obj\n                current_idx += numel\n\n            # Concatenate all flattened tensors\n            self.flattened_tensor = torch.cat(flattened_tensors, dim=0)\n        else:\n            # Initialize from pre-flattened data\n            if flattened_tensor is None or metadata is None:\n                raise ValueError(\n                    \"Must provide either named_tensors or both flattened_tensor and metadata\"\n                )\n            self.flattened_tensor = flattened_tensor\n            self.metadata = metadata\n\n    def get_flattened_tensor(self) -> torch.Tensor:\n        \"\"\"Get the flattened tensor containing all bucket tensors\"\"\"\n        return self.flattened_tensor\n\n    def get_metadata(self) -> List[FlattenedTensorMetadata]:\n        \"\"\"Get metadata for all tensors in the bucket\"\"\"\n        return self.metadata\n\n    def reconstruct_tensors(self) -> List[Tuple[str, torch.Tensor]]:\n        \"\"\"\n        Reconstruct original tensors from flattened tensor with optimized performance.\n        Uses memory-efficient operations to minimize allocations and copies.\n        \"\"\"","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/weight_sync/tensor_bucket.py#L58-L94","documentation":"TensorBucket.__init__ requires either named_tensors or BOTH flattened_tensor and metadata. Passing only one of the pre-flattened pair (or none) is rejected because the bucket cannot reconstruct offsets.","triggerScenarios":"Constructing TensorBucket(flattened_tensor=t) without metadata, or vice versa; deserializing from a partial payload.","commonSituations":"Custom deserialization code that drops the metadata list; IPC/serialization round-trip losing one field.","solutions":["Pass both flattened_tensor and metadata together","Or rebuild from named_tensors instead"],"exampleFix":"# before\nTensorBucket(flattened_tensor=buf)\n# after\nTensorBucket(flattened_tensor=buf, metadata=meta)","handlingStrategy":"validation","validationCode":"assert named_tensors is not None or (flattened_tensor is not None and metadata is not None)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always serialize flattened_tensor and metadata as a pair","Prefer named_tensors for reconstruction"],"tags":["validation","weight-sync","constructor"],"backgroundTag":"missing-required-argument","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}