apache/beam · error · ValueError

Unknown namespace type

Error message

Unknown namespace type

What it means

Metrics.get_namespace normalizes a namespace given as either a class or a plain string; the argument's type is neither, so no namespace string can be derived. This is a type guard on the namespace parameter of metric creation/filtering.

Source

Thrown at sdks/python/apache_beam/metrics/metric.py:70

  from apache_beam.metrics.execution import MetricKey
  from apache_beam.metrics.metricbase import Metric
  from apache_beam.utils.histogram import BucketType

__all__ = ['Metrics', 'MetricsFilter', 'Lineage']

_LOGGER = logging.getLogger(__name__)


class Metrics(object):
  """Lets users create/access metric objects during pipeline execution."""
  @staticmethod
  def get_namespace(namespace: Union[type, str]) -> str:
    if isinstance(namespace, type):
      return '{}.{}'.format(namespace.__module__, namespace.__name__)
    elif isinstance(namespace, str):
      return namespace
    else:
      raise ValueError('Unknown namespace type')

  @staticmethod
  def counter(
      namespace: Union[type, str], name: str) -> 'Metrics.DelegatingCounter':
    """Obtains or creates a Counter metric.

    Args:
      namespace: A class or string that gives the namespace to a metric
      name: A string that gives a unique name to a metric

    Returns:
      A Counter object.
    """
    namespace = Metrics.get_namespace(namespace)
    return Metrics.DelegatingCounter(MetricName(namespace, name))

  @staticmethod
  def distribution(

View on GitHub (pinned to 12126d8942)

Solutions

  1. Pass the class itself (MyClass) not an instance, or pass a plain string namespace.
  2. If you have an instance, use type(instance) or instance.__class__ as the namespace.
  3. Coerce other values to str before passing.

Example fix

- Metrics.counter(self, 'items')
+ Metrics.counter(type(self), 'items')
# or
+ Metrics.counter('mynamespace', 'items')
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(namespace, (str, type)), f'namespace must be str or class, got {type(namespace)}'

Type guard

def is_valid_namespace(ns):
    return isinstance(ns, str) or isinstance(ns, type)

Try / catch

try:
    ns = Metrics.get_namespace(namespace)
except ValueError:
    ns = str(namespace)

Prevention

When it happens

Trigger: Passing an object instance instead of a class (e.g. Metrics.counter(my_obj, 'name')), None, or a non-str type like bytes or int as the namespace argument to Metrics.counter/gauge/distribution.

Common situations: Accidentally passing a class instance (self) rather than the class (self.__class__); passing an enum member or variable holding an unexpected type from config.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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