{"record":{"id":"8acf9eafd29c11d0","repo":"jax-ml/jax","slug":"cannot-select-from-refs-of-different-types-types","errorCode":null,"errorMessage":"Cannot select from Refs of different types: {types}","messagePattern":"Cannot select from Refs of different types: (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/state/types.py","lineNumber":253,"sourceCode":"@tree_util.register_dataclass\n@dataclasses.dataclass(frozen=True, slots=True)\nclass SelectTransform(MultiRefTransform):\n  idx: Array | int\n\n  def transform_types(self, xs):\n    def _type(ref):\n      match ref:\n        case AbstractRef():\n          return ref\n        case core.ShapedArray():\n          raise NotImplementedError\n        case _:\n          raise TypeError(f\"Cannot select {ref}\")\n\n    assert isinstance(xs, Sequence), f\"Select expected sequence, got {xs}\"\n    types = tuple(_type(ref) for ref in xs)\n    if any(types[0] != t for t in types[1:]):\n      raise TypeError(f\"Cannot select from Refs of different types: {types}\")\n    return types[0]\n\n  def undo(self, x: core.AbstractValue) -> Transform:\n    raise NotImplementedError(type(self))\n\n  def pretty_print(self, context: core.JaxprPpContext) -> pp.Doc:\n    del context  # Unused.\n    return pp.text(f\"{{select({self.idx=})}}\")\n\n  def getattr(self, name: str, xs: Sequence[core.AbstractValue]) -> Any:\n    attrs = [getattr(x, name) for x in xs]\n    if any(attrs[0] != attr for attr in attrs[1:]):\n      raise TypeError(f\"Cannot resolve attribute {name} from: {attrs}\")\n    return attrs[0]\n\n\n@dataclasses.dataclass(slots=True)\nclass RefIndexer:","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/state/types.py#L235-L271","documentation":"SelectTransform.transform_types raises TypeError when the refs being selected from have differing abstract types, since a single selection result type can't be chosen. All refs in a multiref select must share the same aval.","triggerScenarios":"Calling get/put on a multiref whose constituent refs have different dtypes or shapes, e.g. mixing a float32 ref and an int32 ref in one TransformedRef.","commonSituations":"Grouping heterogeneous buffers into one select for convenience; dtype changes in one buffer but not others after a refactor.","solutions":["Give all refs in the group the same dtype and shape","Split the select into per-type groups","Assert type homogeneity before building the multiref"],"exampleFix":"// before\nrefs = (f32_ref, i32_ref)\nrefs.get()  # TypeError\n// after\nrefs = (f32_ref, f32_ref)\nrefs.get()","handlingStrategy":"validation","validationCode":"types = {core.typeof(r) for r in refs}\nassert len(types) == 1, f\"heterogeneous refs: {types}\"","typeGuard":null,"tryCatchPattern":"try:\n    refs.get()\nexcept TypeError as e:\n    if \"different types\" in str(e):\n        # split group by type\n        ...","preventionTips":["Construct multirefs only from same-dtype/shape refs","Add a helper that asserts homogeneity at construction"],"tags":["jax","multiref","type-mismatch"],"backgroundTag":"type-mismatch-in-collection","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}