{"record":{"id":"c072c5d8b2940b99","repo":"apache/beam","slug":"runtime-type-violation-detected-within-pardo-s-s","errorCode":null,"errorMessage":"Runtime type violation detected within ParDo(%s): %s","messagePattern":"Runtime type violation detected within ParDo\\((.+?)\\): (.+?)","errorType":"validation","errorClass":"TypeCheckError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/typecheck.py","lineNumber":102,"sourceCode":"    return self.dofn.teardown()\n\n\nclass OutputCheckWrapperDoFn(AbstractDoFnWrapper):\n  \"\"\"A DoFn that verifies against common errors in the output type.\"\"\"\n  def __init__(self, dofn, full_label):\n    super().__init__(dofn)\n    self.full_label = full_label\n\n  def wrapper(self, method, args, kwargs):\n    try:\n      result = method(*args, **kwargs)\n    except TypeCheckError as e:\n      # TODO(BEAM-10710): Remove the 'ParDo' prefix for the label name\n      error_msg = (\n          'Runtime type violation detected within ParDo(%s): '\n          '%s' % (self.full_label, e))\n      _, _, tb = sys.exc_info()\n      raise TypeCheckError(error_msg).with_traceback(tb)\n    else:\n      return self._check_type(result)\n\n  @staticmethod\n  def _check_type(output):\n    if output is None:\n      return output\n\n    elif isinstance(output, (dict, bytes, str)):\n      object_type = type(output).__name__\n      raise TypeCheckError(\n          'Returning a %s from a ParDo or FlatMap is '\n          'discouraged. Please use list(\"%s\") if you really '\n          'want this behavior.' % (object_type, output))\n    elif not isinstance(output, abc.Iterable):\n      raise TypeCheckError(\n          'FlatMap and ParDo must return an '\n          'iterable. %s was returned instead.' % type(output))","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/typecheck.py#L84-L120","documentation":"TypeCheckWrapperDoFn's wrapper catches a TypeCheckError raised while running the wrapped DoFn and re-raises it prefixed with the ParDo label, identifying which transform violated the runtime type check. Beam's runtime type checker validates DoFn inputs/outputs against declared hints when type checking is enabled.","triggerScenarios":"Running a pipeline with runtime type checking enabled (--runtime_type_check or TypeCheckWrapperDoFn) where a DoFn's output element fails its declared output type hint, or input elements fail input hints during process().","commonSituations":"Emitting elements of the wrong type from a DoFn (e.g. emitting ints where str was hinted); data arriving from an external source with unexpected types; hints declared on the PTransform not matching actual runtime data.","solutions":["Read the wrapped TypeCheckError message to find the offending element and expected type, then fix the DoFn to emit the hinted type.","Convert elements explicitly (e.g. str(x), int(x)) before emitting.","Correct the declared type hints if the actual data shape is the intended one.","Add a validation/filter step upstream to drop or coerce bad records."],"exampleFix":"// before\nclass F(beam.DoFn):\n    def process(self, x):\n        yield len(x)  # hinted output str\n// after\nclass F(beam.DoFn):\n    def process(self, x):\n        yield str(len(x))","handlingStrategy":"try-catch","validationCode":"def check_output(el, expected_type):\n    if not isinstance(el, expected_type):\n        raise TypeError(f'{el!r} is not {expected_type}')\n    return el","typeGuard":"def is_str_list(xs) -> bool:\n    return all(isinstance(x, str) for x in xs)","tryCatchPattern":"try:\n    result = pipeline.run()\nexcept TypeCheckError as e:\n    log.error('Runtime type violation: %s', e)\n    raise","preventionTips":["Declare input/output hints on every DoFn and test them with small local runs","Run pipelines with --runtime_type_check in CI to catch violations early","Coerce source data (JSON/CSV) to declared types at ingest"],"tags":["python","apache-beam","typecheck","pardo"],"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"}