{"record":{"id":"c3f31cdf4557d79b","repo":"apache/beam","slug":"runtime-type-violation-detected-within-s-s-typecheck","errorCode":null,"errorMessage":"Runtime type violation detected within %s: %s","messagePattern":"Runtime type violation detected within (.+?): (.+?)","errorType":"validation","errorClass":"TypeCheckError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/typecheck.py","lineNumber":242,"sourceCode":"    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):\n    if self._input_type_hint:\n      try:\n        _check_instance_type(\n            self._input_type_hint[0][0].tuple_types[1],\n            element,\n            'element',\n            True)\n      except TypeCheckError as e:\n        error_msg = (\n            'Runtime type violation detected within %s: '\n            '%s' % (self._label, e))\n        _, _, tb = sys.exc_info()\n        raise TypeCheckError(error_msg).with_traceback(tb)\n    return self._combinefn.add_input(accumulator, element, *args, **kwargs)\n\n  def merge_accumulators(self, accumulators, *args, **kwargs):\n    return self._combinefn.merge_accumulators(accumulators, *args, **kwargs)\n\n  def compact(self, accumulator, *args, **kwargs):\n    return self._combinefn.compact(accumulator, *args, **kwargs)\n\n  def extract_output(self, accumulator, *args, **kwargs):\n    result = self._combinefn.extract_output(accumulator, *args, **kwargs)\n    if self._output_type_hint:\n      try:\n        _check_instance_type(\n            self._output_type_hint.tuple_types[1], result, None, True)\n      except TypeCheckError as e:\n        error_msg = (\n            'Runtime type violation detected within %s: '\n            '%s' % (self._label, e))","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/typecheck.py#L224-L260","documentation":"TypeCheckCombineFn.add_input validates each element (and accumulator) against the combine fn's input type hint before delegating to the wrapped CombineFn. A TypeCheckError from that validation is re-raised with the label: 'Runtime type violation detected within %s'. It identifies which combine transform received a wrongly-typed element.","triggerScenarios":"During Combine/CombineGlobally with runtime type checking, add_input receives an element (or accumulator) whose type violates the declared input hint (e.g. adding a str to an int sum combine).","commonSituations":"Combining PCollections parsed from JSON/CSV where numbers arrive as strings; accumulate/merge mixing float and Decimal; hints declared at the transform not matching actual element types.","solutions":["Fix the message to find the bad element and coerce types upstream (e.g. Map(int)).","Adjust the CombineFn's input type hint to match actual data.","Add a validation step to filter/convert malformed elements before combining.","Wrap add_input with defensive conversion in a custom CombineFn."],"exampleFix":"// before\np | beam.CombineGlobally(beam.combiners.SumCombineFn()).with_input_types(int)\n// after\np | beam.Map(lambda x: int(x)) | beam.CombineGlobally(beam.combiners.SumCombineFn()).with_input_types(int)","handlingStrategy":"validation","validationCode":"def coerce_numeric(elements, t=int):\n    return [t(x) if not isinstance(x, t) else x for x in elements]","typeGuard":"def all_ints(xs) -> bool:\n    return all(isinstance(x, int) and not isinstance(x, bool) for x in xs)","tryCatchPattern":"try:\n    combined = pcoll | beam.CombineGlobally(fn)\nexcept TypeCheckError as e:\n    log.error('Combine input violation: %s', e)\n    raise","preventionTips":["Convert stringly-typed numbers from JSON/CSV to proper types at ingest","Declare with_input_types on CombineFns and test with representative data","Filter or dead-letter malformed records before combining"],"tags":["python","apache-beam","typecheck","combinefn"],"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"}