{"record":{"id":"9d6f0c8352c9cfc8","repo":"jax-ml/jax","slug":"lax-platform-dependent-the-default-branch-must","errorCode":null,"errorMessage":"lax.platform_dependent: the 'default' branch must be a callable.","messagePattern":"lax\\.platform_dependent: the 'default' branch must be a callable\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/lax/control_flow/conditionals.py","lineNumber":1248,"sourceCode":"  for pname, pbranch in per_platform.items():\n    if not callable(pbranch):\n      raise TypeError(f\"lax.platform_dependent: the '{pname}' branch must \"\n                      \"be a callable.\")\n    if pname == \"gpu\":\n      raise ValueError(\n          \"Use 'cuda', 'rocm', or 'oneapi' for lax.platform_dependent.\")\n    for ps, b in branches_platforms_list:\n      if b == pbranch:\n        ps.append(pname)\n        break\n    else:\n      branches_platforms_list.append(([pname], pbranch))\n\n  platforms_lists, branches = util.unzip2(branches_platforms_list)\n  branches_platforms: BranchesPlatforms = tuple(tuple(ps) for ps in platforms_lists)\n  if default is not None:\n    if not callable(default):\n      raise TypeError(\"lax.platform_dependent: the 'default' branch must \"\n                      \"be a callable.\")\n    branches = branches + (default,)\n    branches_platforms = branches_platforms + (None,)\n  platform_index = platform_index_p.bind(platforms=branches_platforms)\n\n  if core.is_concrete(platform_index):\n    return branches[int(platform_index)](*args)\n  return _switch_internal(platform_index, branches, args,\n                          branches_platforms=branches_platforms)\n\n\n# A primitive to compute the index of a platform into a list of platforms.\n# Args:\n#   platforms: BranchesPlatforms. If the current lowering\n#     platform is in one of the inner tuples returns the index of that inner\n#     tuple in the outer tuple.\nplatform_index_p = core.Primitive(\"platform_index\")\nplatform_index_p.multiple_results = False","sourceCodeStart":1230,"sourceCodeEnd":1266,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/lax/control_flow/conditionals.py#L1230-L1266","documentation":"The optional 'default' argument of lax.platform_dependent must be a callable just like the named platform branches. Passing a value or None-adjacent sentinel that isn't callable raises this TypeError.","triggerScenarios":"Calling lax.platform_dependent(per_platform, *args, default=some_array) or default=some_object.","commonSituations":"Assuming default is a fallback value rather than a fallback function; reusing a computed result as the default.","solutions":["Pass default as a zero/one-arg function returning the fallback computation","Precompute nothing: let the function receive args and compute lazily","Check callable(default) before the call in generic wrappers"],"exampleFix":"// before\nlax.platform_dependent({'cuda': f}, x, default=x * 2)\n// after\nlax.platform_dependent({'cuda': f}, x, default=lambda a: a * 2)","handlingStrategy":"type-guard","validationCode":"if default is not None and not callable(default):\n    val = default\n    default = (lambda *a: val)  # wrap value in a callable","typeGuard":"def default_is_callable(default) -> bool:\n    return default is None or callable(default)","tryCatchPattern":"try: lax.platform_dependent(p, x, default=d)\nexcept TypeError as e:\n    if 'default' in str(e): d = (lambda *a: d); retry\n    else: raise","preventionTips":["Treat default as a function of the args, not a value","Keep branch constructors uniform: always lambdas"],"tags":["jax","lax","platform-dependent","typeerror","callable"],"backgroundTag":"callback-must-be-callable","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}