apache/beam · error · TypeInferenceError

Unknown forbidden type

Error message

Unknown forbidden type: %s

What it means

trivial_inference.instance_to_type maps a concrete value to a Beam type hint; if it encounters a type it explicitly refuses to infer (a 'forbidden' type), it raises TypeInferenceError. Beam deliberately does not infer hints from certain object types, so inference stops here rather than guessing a wrong type.

Solutions

  1. Add explicit type hints (with_output_types / with_input_types) so Beam doesn't need to infer from the forbidden value.
  2. Remove or replace the unsupported constant/object referenced in the callable (e.g. don't pass functions/classes as data).
  3. If dicts are involved, make sure the dict instance is homogeneous or annotate it as Dict[Any, Any] explicitly.
  4. Catch TypeInferenceError and fall back to Any typing if inference is optional.

Example fix

// before
p | beam.FlatMap(lambda x: helper(x))  # helper closes over a forbidden object
// after
p | beam.FlatMap(lambda x: helper(x)).with_output_types(str)
Defensive patterns

Strategy: type-guard

Validate before calling

ALLOWED = (str, int, float, bool, bytes, list, dict, tuple, set)
def inferable(v) -> bool:
    return type(v) in ALLOWED

Type guard

def is_supported_instance(o) -> bool:
    return isinstance(o, (str, int, float, bool, bytes, list, dict, tuple, set))

Try / catch

try:
    hint = infer_return_type(fn, [])
except TypeInferenceError:
    hint = typehints.Any

Prevention

When it happens

Trigger: Calling infer_return_type or element_type on a callable whose body/constant involves an instance of an unsupported/forbidden type (e.g. complex objects, module, functions treated as values), typically triggered when decorating a DoFn/FlatMap whose constants include such values.

Common situations: Applying @with_input_types/@with_output_types-free pipelines where Beam tries to infer types from a lambda that closes over unusual objects; passing callables or class objects as constants; building a schema from an instance containing an exotic type.

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/76dbceb6519490c8. Report an issue: GitHub.

Appendix: source

Thrown at sdks/python/apache_beam/typehints/trivial_inference.py:93

    else:
      return typehints.Set[typehints.Any]
  elif t == frozenset:
    if len(o) > 0:
      return typehints.FrozenSet[typehints.Union[[
          instance_to_type(item) for item in o
      ]]]
    else:
      return typehints.FrozenSet[typehints.Any]
  elif t == dict:
    if len(o) > 0:
      return typehints.Dict[
          typehints.Union[[instance_to_type(k) for k, v in o.items()]],
          typehints.Union[[instance_to_type(v) for k, v in o.items()]],
      ]
    else:
      return typehints.Dict[typehints.Any, typehints.Any]
  else:
    raise TypeInferenceError('Unknown forbidden type: %s' % t)


def union_list(xs, ys):
  assert len(xs) == len(ys)
  return [union(x, y) for x, y in zip(xs, ys)]


class Const(object):
  def __init__(self, value):
    self.value = value
    self.type = instance_to_type(value)

  def __eq__(self, other):
    return isinstance(other, Const) and self.value == other.value

  def __hash__(self):
    return hash(self.value)

View on GitHub (pinned to 12126d8942)