{"record":{"id":"3f34919803c96546","repo":"apache/beam","slug":"according-to-type-hint-expected-s-should-be-of-type-s","errorCode":null,"errorMessage":"According to type-hint expected %s should be of type %s. Instead, received '%s', an instance of type %s.","messagePattern":"According to type-hint expected (.+?) should be of type (.+?)\\. Instead, received '(.+?)', an instance of type (.+?)\\.","errorType":"validation","errorClass":"TypeCheckError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/typecheck.py","lineNumber":211,"sourceCode":"          otherwise.\n\n    Raises:\n      TypeError: If 'datum' fails to type-check according to 'type_constraint'.\n    \"\"\"\n    datum_type = 'input' if is_input else 'output'\n\n    try:\n      check_constraint(type_constraint, datum)\n    except CompositeTypeHintError as e:\n      _, _, tb = sys.exc_info()\n      raise TypeCheckError(e.args[0]).with_traceback(tb)\n    except SimpleTypeHintError:\n      error_msg = (\n          \"According to type-hint expected %s should be of type %s. \"\n          \"Instead, received '%s', an instance of type %s.\" %\n          (datum_type, type_constraint, datum, type(datum)))\n      _, _, tb = sys.exc_info()\n      raise TypeCheckError(error_msg).with_traceback(tb)\n\n\nclass TypeCheckCombineFn(core.CombineFn):\n  \"\"\"A wrapper around a CombineFn performing type-checking of input and output.\n  \"\"\"\n  def __init__(self, combinefn, type_hints, label=None):\n    self._combinefn = combinefn\n    self._input_type_hint = type_hints.input_types\n    self._output_type_hint = type_hints.simple_output_type(label)\n    self._label = label\n\n  def setup(self, *args, **kwargs):\n    self._combinefn.setup(*args, **kwargs)\n\n  def create_accumulator(self, *args, **kwargs):\n    return self._combinefn.create_accumulator(*args, **kwargs)\n\n  def add_input(self, accumulator, element, *args, **kwargs):","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/typecheck.py#L193-L229","documentation":"TypeCheckCombineFn/TypeCheckWrapper's type_check validates a datum against a declared Beam type hint via typehints' validate. When the underlying hint check raises SimpleTypeHintError, it is re-raised as a TypeCheckError stating the expected type and the actual received instance. This is a runtime type-hint violation on inputs or outputs of a transform.","triggerScenarios":"Calling type_check_output/type_check on data whose runtime type doesn't match the PCollection's declared type hint, e.g. a CombineFn input element or accumulator not matching with_input_types/with_output_types declarations.","commonSituations":"Side inputs or sources producing unexpected element types; hints written for one schema but data changed upstream; combining elements of mixed types (int vs float vs str).","solutions":["Inspect the received instance in the message and coerce data to the expected type before the transform.","Update the type hints to reflect the real data types.","Filter or route malformed records with a validation DoFn before the checked transform.","Enable debugging on the hint to get detailed violation info and fix at the source."],"exampleFix":"// before\np | beam.CombineGlobally(SumCount()).with_input_types(int)  # data has '3' strings\n// after\np | beam.Map(int) | beam.CombineGlobally(SumCount()).with_input_types(int)","handlingStrategy":"type-guard","validationCode":"def assert_hint(datum, constraint):\n    apache_beam.typehints.typehints.validate(constraint, datum)\n    return datum","typeGuard":"def matches_hint(datum, constraint) -> bool:\n    try:\n        typehints.validate(constraint, datum)\n        return True\n    except Exception:\n        return False","tryCatchPattern":"try:\n    out = checked_transform(data)\nexcept TypeCheckError as e:\n    log.error('Hint violation: %s', e)\n    raise","preventionTips":["Validate a sample of source data against hints before running the pipeline","Keep hints in sync with upstream schema changes","Enable runtime type checking in staging environments"],"tags":["python","apache-beam","typecheck","type-hints"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}