{"record":{"id":"f798f4a602d828b8","repo":"jax-ml/jax","slug":"invalid-value-received-for-the-sharding-argument","errorCode":null,"errorMessage":"Invalid value received for the sharding argument. Expected values are `None` or an instance of `jax.Sharding`. Got {sharding} of type {type(sharding)}","messagePattern":"Invalid value received for the sharding argument\\. Expected values are `None` or an instance of `jax\\.Sharding`\\. Got (.+?) of type (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"jax/_src/layout.py","lineNumber":154,"sourceCode":"    # If layout is concrete and sharding is not, error.\n    if isinstance(layout, Layout) and sharding is None:\n      raise ValueError(\n          'Sharding has to be concrete when layout is of type'\n          f' {type(layout)}. Please pass a'\n          ' `jax.sharding.NamedSharding` or'\n          ' `jax.sharding.SingleDeviceSharding` to the sharding argument. Got'\n          f' sharding {sharding}'\n      )\n    if not isinstance(\n        layout, (Layout, type(None), AutoLayoutSingleton)):\n      raise TypeError(\n          'Invalid value received for the layout argument.'\n          ' Expected values are `None`, `Layout.AUTO` or an'\n          f' instance of `Layout`. Got {layout} of'\n          f' type {type(layout)}'\n      )\n    if not isinstance(sharding, (Sharding, type(None))):\n      raise TypeError(\n          'Invalid value received for the sharding argument. Expected values'\n          ' are `None` or an instance of `jax.Sharding`. Got'\n          f' {sharding} of type {type(sharding)}')\n\n    self.layout = layout\n    self.sharding = sharding\n\n  def __repr__(self):\n    return f'Format(layout={self.layout}, sharding={self.sharding})'\n\n  def __hash__(self):\n    return hash((self.layout, self.sharding))\n\n  def __eq__(self, other):\n    if not isinstance(other, Format):\n      return False\n    return (self.layout == other.layout and\n            self.sharding == other.sharding)","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/jax-ml/jax/blob/1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb/jax/_src/layout.py#L136-L172","documentation":"Raised by the same layout module constructor when the `sharding` argument is neither None nor a jax.Sharding instance. JAX requires sharding specs to be real Sharding objects (NamedSharding, SingleDeviceSharding, etc.) so it can compute device assignments. Any other value, including duck-typed or pickled objects from mismatched versions, fails this isinstance gate.","triggerScenarios":"Passing a string ('sharding'), a partition spec tuple, or a numpy array as `sharding`; passing a Sharding object created in a different jax version/install whose class identity differs; mixing up argument order with layout.","commonSituations":"Argument mix-ups in jit(..., in_shardings=...) style calls; loading pickled sharding objects across JAX versions; having two jax installs (e.g. pip + conda) so isinstance fails across module identities.","solutions":["Pass a jax.sharding.* instance such as NamedSharding(mesh, P('x')) or None","Verify only one jax installation is imported (check jax.__file__ on both producer and consumer)","Recreate the sharding object in the current process instead of unpickling from another version","Double-check you haven't swapped layout and sharding keyword arguments"],"exampleFix":"# before\nsharding = ('data',)  # tuple, not a Sharding\n\n# after\nimport jax\nsharding = jax.sharding.NamedSharding(mesh, jax.sharding.PartitionSpec('data'))","handlingStrategy":"type-guard","validationCode":"import jax\nok = sharding is None or isinstance(sharding, jax.sharding.Sharding)","typeGuard":"import jax\ndef is_valid_sharding(x) -> bool:\n    return x is None or isinstance(x, jax.sharding.Sharding)","tryCatchPattern":"try:\n    f = jax.jit(fun, in_shardings=sharding)\nexcept TypeError as e:\n    if 'sharding argument' in str(e):\n        f = jax.jit(fun)  # fall back to default sharding\n    else: raise","preventionTips":["Build shardings only via jax.sharding constructors","Check jax.__file__ to detect dual installs when unpickling","Keep sharding and layout keyword arguments straight"],"tags":["jax","sharding","type-validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"1e1c6a8fc06dfcd1247076ec5cae4640cea5d7bb","analyzedAt":"2026-08-27T09:53:25.647Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}