apache/beam · error · ValueError

Invalid pattern for single name: {name_result.group(0)}

Error message

Invalid pattern for single name: {name_result.group(0)}

What it means

_get_code_object_from_single_name_pattern resolves a qual-name pattern expected to contain exactly one name group. If the regex match has more than one capture group, the pattern is not a 'single name' pattern and ValueError is raised, because the resolver only handles one name per step.

Source

Thrown at sdks/python/apache_beam/internal/code_object_pickler.py:350

def _get_code_object_from_single_name_pattern(
    obj: types.ModuleType, name_result: re.Match[str], path: str):
  """Returns the code object from a name pattern.

  Args:
    obj: The object to search within.
    name_result: The result of the name pattern search.
    path: The path to the code object.

  Returns:
    The code object.

  Raises:
    ValueError: If the pattern is invalid.
    AttributeError: If the code object is not found.
  """
  if len(name_result.groups()) > 1:
    raise ValueError(f'Invalid pattern for single name: {name_result.group(0)}')
  # Groups are indexed starting at 1, group(0) is the entire match.
  name = name_result.group(1)
  if hasattr(obj, 'co_consts'):
    for co_const in obj.co_consts:
      if inspect.iscode(co_const) and co_const.co_name == name:
        return co_const
  raise AttributeError(f'Could not find code object with path: {path}')


def _get_code_object_from_lambda_with_args_pattern(
    obj: types.ModuleType, lambda_with_args_result: re.Match[str], path: str):
  """Returns the code object from a lambda with args pattern.

  Args:
    obj: The object to search within.
    lambda_with_args_result: The result of the lambda with args pattern search.
    path: The path to the code object.

View on GitHub (pinned to 12126d8942)

Solutions

  1. Ensure identifiers are resolved one name-part at a time (split compound patterns before calling).
  2. Define callables at module level to avoid compound qual-name patterns.
  3. Catch ValueError and fall back to standard cloudpickle pickling.
  4. If patterns are generated by Beam internals, update/patch apache_beam.

Example fix

// before
co = get_code_from_identifier(obj, 'outer.<locals>.inner.<locals>.deep')
// after
for part in split_pattern('outer.<locals>.inner.<locals>.deep'):
    co = get_code_from_identifier(co, part)
Defensive patterns

Strategy: try-catch

Validate before calling

if identifier.count('<locals>') > 1 or '(' in identifier and identifier.count('(') > 1:
    raise ValueError('identifier must resolve a single name per step')

Type guard

def is_single_name_pattern(match) -> bool:
    return len(match.groups()) == 1

Try / catch

try:
    co = get_code_from_identifier(obj, pattern)
except ValueError as e:
    if 'Invalid pattern for single name' in str(e):
        co = resolve_step_by_step(obj, pattern)
    else:
        raise

Prevention

When it happens

Trigger: get_code_from_identifier receiving a compound pattern (multiple parenthesized groups) — e.g. identifiers covering several nested names at once that should have been split into multiple resolution steps.

Common situations: Internally generated identifiers for deeply nested or dynamically defined callables; custom pickling code passing malformed identifier strings.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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