apache/beam · error · CompositeTypeHintError

hint type-constraint violated. The type of element # in the…

Error message

%s hint type-constraint violated. The type of element #%s in the passed tuple is incorrect. Expected an instance of type %s, instead received an instance of type %s.

What it means

Raised by TupleConstraint.type_check when element at position type_pos fails a SimpleTypeHintError check against the declared per-position type. It reports the expected type repr and the actual runtime class of the offending element.

Solutions

  1. Inspect the reported element index and fix its runtime type
  2. Check element ordering against the declared Tuple[...] order
  3. Use Optional[...] in the hint if None is legitimate
  4. Add a validation DoFn before the hinted transform to catch bad rows early

Example fix

# before
Tuple[int, int]: yield (1, 'a')
# after
yield (1, int('a'))  # or correct the data/coerce the type
Defensive patterns

Strategy: validation

Validate before calling

for i, (el, t) in enumerate(zip(tpl, expected_types)):
    if not isinstance(el, t): raise ValueError(f'element #{i} is {type(el).__name__}, expected {t}')

Type guard

def matches(tpl, types): return isinstance(tpl, tuple) and len(tpl)==len(types) and all(isinstance(e,t) for e,t in zip(tpl,types))

Prevention

When it happens

Trigger: A tuple element whose runtime type differs from the corresponding hint entry, e.g. (1, 'a') checked against Tuple[int, int]; a bool/str slipping into a numeric slot.

Common situations: CSV/JSON rows parsed into tuples with mixed-type columns; accidentally swapped element order; None appearing where a concrete type is hinted.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/2c957721ce0b8190. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/typehints/typehints.py:736

        raise CompositeTypeHintError(
            "Tuple type constraint violated. Valid object instance must be of "
            "type 'tuple'. Instead, an instance of '%s' was received." %
            tuple_instance.__class__.__name__)

      if len(tuple_instance) != len(self.tuple_types):
        raise CompositeTypeHintError(
            'Passed object instance is of the proper type, but differs in '
            'length from the hinted type. Expected a tuple of length %s, '
            'received a tuple of length %s.' %
            (len(self.tuple_types), len(tuple_instance)))

      for type_pos, (expected, actual) in enumerate(zip(self.tuple_types,
                                                        tuple_instance)):
        try:
          check_constraint(expected, actual)
          continue
        except SimpleTypeHintError:
          raise CompositeTypeHintError(
              '%s hint type-constraint violated. The type of element #%s in '
              'the passed tuple is incorrect. Expected an instance of '
              'type %s, instead received an instance of type %s.' %
              (repr(self), type_pos, repr(expected), actual.__class__.__name__))
        except CompositeTypeHintError as e:
          raise CompositeTypeHintError(
              '%s hint type-constraint violated. The type of element #%s in '
              'the passed tuple is incorrect. %s' % (repr(self), type_pos, e))

    def match_type_variables(self, concrete_type):
      bindings = {}
      if isinstance(concrete_type, TupleConstraint):
        for a, b in zip(self.tuple_types, concrete_type.tuple_types):
          bindings.update(match_type_variables(a, b))
      return bindings

    def bind_type_variables(self, bindings):
      bound_tuple_types = tuple(

View on GitHub (pinned to 12126d8942)