{"record":{"id":"5b36306151f358b7","repo":"jax-ml/jax","slug":"the-sharding-attribute-is-not-available-on-self-5b3630","errorCode":null,"errorMessage":"The 'sharding' attribute is not available on {self._error_repr()}. To query sharding information on tracers, use `jax.typeof(x)`.","messagePattern":"The 'sharding' attribute is not available on (.+?)\\. To query sharding information on tracers, use `jax\\.typeof\\(x\\)`\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"jax/_src/core.py","lineNumber":1183,"sourceCode":"  # NumPy also only looks up special methods on classes.\n  def __array_module__(self, types):\n    if not hasattr(self.aval, \"_array_module\"):\n      raise TypeError(f\"Value of type {type(self)} is not compatible with the Array API.\")\n    return self.aval._array_module(self, types)\n\n  def __getattr__(self, name):\n    # if the aval property raises an AttributeError, gets caught here\n    assert not config.enable_checks.value or name != \"aval\"\n\n    # These must raise AttributeError in the base class for backward compatibility.\n    # TODO(jakevdp): can we change this and make them raise NotImplementedError instead?\n    if name in [\"block_until_ready\", \"copy_to_host_async\"]:\n      raise AttributeError(\n        f\"The '{name}' method is not available on {self._error_repr()}.\"\n        f\"{self._origin_msg()}\")\n\n    if name == 'sharding':\n      raise AttributeError(\n        f\"The 'sharding' attribute is not available on {self._error_repr()}. \"\n        \"To query sharding information on tracers, use `jax.typeof(x)`.\")\n\n    try:\n      attr = getattr(self.aval, name)\n    except AttributeError as err:\n      raise AttributeError(\n          f\"{self.__class__.__name__} has no attribute {name}\"\n      ) from err\n    else:\n      t = type(attr)\n      if t is aval_property:\n        return attr.fget(self)\n      elif t is aval_method:\n        return types.MethodType(attr.fun, self)\n      else:\n        return attr\n","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/core.py#L1165-L1201","documentation":"Raised by JAXTracer.__getattr__ when code accesses the .sharding attribute on a JAX Tracer. Sharding describes how a concrete, dispatched array's data is laid out across devices; during tracing (jit/pmap/vmap/shard_map) the value is abstract and has no committed sharding, so the attribute does not exist and lookup raises AttributeError with a pointer to jax.typeof.","triggerScenarios":"Reading x.sharding inside a jitted/pmap/shard_map-decorated function; generic code (e.g. MultiDeviceHost or data-parallel wrappers) that inspects .sharding on every array-like argument; checks like x.sharding.num_devices or NamedSharding queries applied to traced values.","commonSituations":"Data-parallel training loops that auto-shard based on incoming arrays' sharding, invoked on tracers; migration from pmap (where .sharding probes sometimes slipped through) to jax.jit + sharding constraints; logging or assertion helpers inspecting device placement of any array they touch.","solutions":["Query sharding outside the traced function on concrete arrays, or use jax.typeof(x) inside traces as the message suggests to get abstract type info.","Pass sharding knowledge explicitly (e.g. via in_shardings/out_shardings on jax.jit or jax.lax.with_sharding_constraint) instead of introspecting .sharding.","Guard generic introspection with isinstance(x, jax.Array) before touching .sharding.","For pmap-era code, migrate to jax.jit with sharding-in annotations."],"exampleFix":"# before\n@jax.jit\ndef step(x):\n    nd = x.sharding.num_devices   # AttributeError on Tracer\n    return x / nd\n\n# after\n@jax.jit\ndef step(x):\n    t = jax.typeof(x)             # abstract type available on tracers\n    return x / t.size  # or hoist num_devices out as a static scalar","handlingStrategy":"type-guard","validationCode":"import jax\ndef sharding_or_none(x):\n    if isinstance(x, jax.core.Tracer):\n        return None  # no sharding during tracing; use jax.typeof(x) if type info needed\n    return x.sharding","typeGuard":"import jax\ndef has_concrete_sharding(x) -> bool:\n    return isinstance(x, jax.Array) and not isinstance(x, jax.core.Tracer)","tryCatchPattern":"try:\n    s = x.sharding\nexcept AttributeError:\n    s = None  # tracer inside jit/pmap; use jax.typeof(x) for abstract type","preventionTips":["Query .sharding only on concrete arrays outside transformations.","Inside traces use jax.typeof(x) or jax.lax.with_sharding_constraint to express sharding intent.","Express layouts via jit in_shardings/out_shardings rather than introspecting arguments.","Guard generic distributed helpers with an isinstance(x, jax.Array) tracer check."],"tags":["jax","tracer","sharding","attribute-access","distributed"],"backgroundTag":"method-missing-on-duck-typed-object","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}