apache/beam · error · ValueError

Failed to convert Beam type

Error message

Failed to convert Beam type: %s

What it means

convert_to_python_type converts internal Beam type-hint constraint objects (e.g. TypeVariable constraints, IterableTypeConstraint) back to native Python typing constructs. It raises ValueError when it receives a Beam type it has no conversion branch for. This indicates an unsupported or unexpected Beam type hint object was passed in.

Solutions

  1. Inspect the Beam type object passed in (print(type(typ))) and use a supported equivalent (e.g. IterableTypeConstraint instead of a custom constraint).
  2. Add a conversion branch for the new constraint type in native_type_compatibility.convert_to_python_type.
  3. Catch ValueError and fall back to typehints.Any when exact round-tripping is not required.
  4. Verify both sides of the serialization run the same Beam version so constraint classes match.

Example fix

// before
py_type = convert_to_python_type(my_custom_constraint)  # ValueError
// after
try:
    py_type = convert_to_python_type(my_custom_constraint)
except ValueError:
    py_type = typing.Any  # fallback
Defensive patterns

Strategy: try-catch

Validate before calling

from apache_beam.typehints import typehints
SUPPORTED = (typehints.IterableTypeConstraint, typehints.MapTypeConstraint)
def is_convertible_beam_type(typ) -> bool:
    return isinstance(typ, SUPPORTED + tuple(typehints.__all__ and [])) or hasattr(typehints, type(typ).__name__)

Type guard

def is_beam_constraint(typ) -> bool:
    from apache_beam.typehints import typehints
    return isinstance(typ, typehints.TypeConstraint)

Try / catch

try:
    py_type = convert_to_python_type(beam_type)
except ValueError:
    py_type = typing.Any  # degrade gracefully instead of failing serialization

Prevention

When it happens

Trigger: Calling convert_to_python_type or convert_to_python_types with a Beam typehints constraint class not handled by the isinstance chain (e.g. a custom or newly-added Beam constraint), or via to_runner_api_parameter when serializing a PCollection whose type hint is unsupported.

Common situations: Writing a custom runner or pipeline fragment serialization that round-trips type hints; using exotic Beam type constraints (e.g. SetTypeConstraint variants) in coders/runner-api conversion; upgrading Beam so new constraint types appear in old converters.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at sdks/python/apache_beam/typehints/native_type_compatibility.py:633

      return typing.Any
    return typing.Union[tuple(convert_to_python_types(typ.union_types))]
  if isinstance(typ, typehints.SetTypeConstraint):
    return set[convert_to_python_type(typ.inner_type)]
  if isinstance(typ, typehints.FrozenSetTypeConstraint):
    return frozenset[convert_to_python_type(typ.inner_type)]
  if isinstance(typ, typehints.TupleConstraint):
    return tuple[tuple(convert_to_python_types(typ.tuple_types))]
  if isinstance(typ, typehints.TupleSequenceConstraint):
    return tuple[convert_to_python_type(typ.inner_type), ...]
  if isinstance(typ, typehints.ABCSequenceTypeConstraint):
    return collections.abc.Sequence[convert_to_python_type(typ.inner_type)]
  if isinstance(typ, typehints.IteratorTypeConstraint):
    return collections.abc.Iterator[convert_to_python_type(typ.yielded_type)]
  if isinstance(typ, typehints.MappingTypeConstraint):
    return collections.abc.Mapping[convert_to_python_type(typ.key_type),
                                   convert_to_python_type(typ.value_type)]

  raise ValueError('Failed to convert Beam type: %s' % typ)


def convert_to_python_types(args):
  """Convert the given list or dictionary of args to python types.

  Args:
    args: Either an iterable of types, or a dictionary where the values are
    types.

  Returns:
    If given an iterable, a list of converted types. If given a dictionary,
    a dictionary with the same keys, and values which have been converted.
  """
  if isinstance(args, dict):
    return {k: convert_to_python_type(v) for k, v in args.items()}
  else:
    return [convert_to_python_type(v) for v in args]

View on GitHub (pinned to 12126d8942)