{"record":{"id":"0f0234136026ba18","repo":"apache/beam","slug":"expected-an-instance-of-shardedkeytypeconstraint-but-got-a-s","errorCode":null,"errorMessage":"Expected an instance of ShardedKeyTypeConstraint, but got a %s","messagePattern":"Expected an instance of ShardedKeyTypeConstraint, but got a (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/coders/coders.py","lineNumber":1781,"sourceCode":"  def _create_impl(self):\n    return coder_impl.ShardedKeyCoderImpl(self._key_coder.get_impl())\n\n  def is_deterministic(self):\n    # type: () -> bool\n    return self._key_coder.is_deterministic()\n\n  def to_type_hint(self):\n    from apache_beam.typehints import sharded_key_type\n    return sharded_key_type.ShardedKeyTypeConstraint(\n        self._key_coder.to_type_hint())\n\n  @classmethod\n  def from_type_hint(cls, typehint, registry):\n    from apache_beam.typehints import sharded_key_type\n    if isinstance(typehint, sharded_key_type.ShardedKeyTypeConstraint):\n      return cls(registry.get_coder(typehint.key_type))\n    else:\n      raise ValueError((\n          'Expected an instance of ShardedKeyTypeConstraint'\n          ', but got a %s' % typehint))\n\n  def __eq__(self, other):\n    return type(self) == type(other) and self._key_coder == other._key_coder\n\n  def __hash__(self):\n    return hash(type(self)) + hash(self._key_coder)\n\n  def __repr__(self):\n    return 'ShardedKeyCoder[%s]' % self._key_coder\n\n\nCoder.register_structured_urn(\n    common_urns.coders.SHARDED_KEY.urn, ShardedKeyCoder)\n\n\nclass TimestampPrefixingWindowCoder(FastCoder):","sourceCodeStart":1763,"sourceCodeEnd":1799,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/coders/coders.py#L1763-L1799","documentation":"ShardedKeyCoder encodes sharded keys and its from_type_hint only accepts typehints that are ShardedKeyTypeConstraint instances, from which it extracts the inner key coder. Passing any other typehint raises ValueError.","triggerScenarios":"Requesting a coder for a typehint that is not sharded_key_type.ShardedKeyTypeConstraint, e.g. registering ShardedKeyCoder for a plain dict/KV hint or a hand-built type constraint.","commonSituations":"Using sharded-key features (e.g. stateful/locking transforms expecting sharded keys) without wrapping the key type via ShardedKey; type-inference producing a bare key type.","solutions":["Wrap the key typehint with apache_beam.typehints.sharded_key_type.ShardedKey[T] so the constraint instance is produced","Ensure the transform emitting the data is annotated with the ShardedKey typehint","Use the registry's normal coder resolution rather than forcing ShardedKeyCoder for non-sharded keys"],"exampleFix":"// before\nShardedKeyCoder.from_type_hint(str, registry)\n// after\nShardedKeyCoder.from_type_hint(ShardedKey[str], registry)","handlingStrategy":"type-guard","validationCode":"from apache_beam.typehints import sharded_key_type\nis_sharded = isinstance(typehint, sharded_key_type.ShardedKeyTypeConstraint)","typeGuard":"def is_sharded_key_hint(th) -> bool:\n    from apache_beam.typehints import sharded_key_type\n    return isinstance(th, sharded_key_type.ShardedKeyTypeConstraint)","tryCatchPattern":"try:\n    coder = ShardedKeyCoder.from_type_hint(typehint, registry)\nexcept ValueError:\n    coder = registry.get_coder(typehint)","preventionTips":["Use ShardedKey[T] type hints for sharded-key transforms","Do not force ShardedKeyCoder for non-sharded key types","Let the coder registry resolve coders from annotations"],"tags":["python","type-system","coders","apache-beam"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}