apache/beam · error · ValueError

Parsed object is not a Chunk instance

Error message

Parsed object is not a Chunk instance

What it means

parse_chunk_strings evaluates input strings in a restricted environment and expects each parsed object to be a legacy Chunk instance. If eval() yields any other object (int, dict, list, different class), the function raises ValueError, which is then re-raised wrapped with the offending raw string.

Solutions

  1. Ensure each input string evaluates to a Chunk(...), e.g. "Chunk(content=Content(text='...'), embedding=[0.1, ...])"
  2. Expose the correct class in the safe eval environment if using a custom Chunk subclass
  3. Pre-validate/pre-parse the side input file and skip or fix lines that don't produce Chunk instances
  4. Catch ValueError from parse_chunk_strings to log the offending raw_str and continue

Example fix

// before
parse_chunk_strings(["{'id': 1, 'text': 'hi'}"])
// after
parse_chunk_strings(["Chunk(content=Content(text='hi'), embedding=[0.1, 0.2])"])
Defensive patterns

Strategy: try-catch

Validate before calling

import ast
for s in raw_strings:
    node = ast.parse(s, mode='eval')
    if not (isinstance(node.body, ast.Call) and getattr(node.body.func, 'id', '') == 'Chunk'):
        raise ValueError(f"Not a Chunk literal: {s!r}")

Type guard

def is_chunk_literal(s: str) -> bool:
    import ast
    try:
        body = ast.parse(s, mode='eval').body
    except SyntaxError:
        return False
    return isinstance(body, ast.Call) and getattr(body.func, 'id', '') == 'Chunk'

Try / catch

try:
    chunks = parse_chunk_strings(raw_strings)
except ValueError as e:
    logging.error("Bad chunk side-input: %s", e)
    chunks = []

Prevention

When it happens

Trigger: Passing strings that evaluate to non-Chunk objects — e.g. a plain dict, a list, a number, or an object of a class not named Chunk in the safe globals (such as EmbeddableItem or a custom class not exposed to the eval environment).

Common situations: Reading side-input data written by a newer pipeline that serialized EmbeddableItem instead of Chunk; typos or corrupted lines in the input file; pickled/repr'd objects of another type fed into the parser.

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

Appendix: source

Thrown at sdks/python/apache_beam/ml/rag/utils.py:115

      'Content': Content,
      'Embedding': Embedding,
      'defaultdict': defaultdict,
      'list': list,
      '__builtins__': {}
  }

  for raw_str in chunk_str_list:
    try:
      # replace "<class 'list'>" with actual list reference.
      cleaned_str = re.sub(
          r"defaultdict\(<class 'list'>", "defaultdict(list", raw_str)

      # Evaluate string in restricted environment.
      chunk = eval(cleaned_str, safe_globals)  # pylint: disable=eval-used
      if isinstance(chunk, Chunk):
        parsed_chunks.append(chunk)
      else:
        raise ValueError("Parsed object is not a Chunk instance")
    except Exception as e:
      raise ValueError(f"Error parsing string:\n{raw_str}\n{e}")

  return parsed_chunks


def unpack_dataclass_with_kwargs(dataclass_instance):
  """Unpacks dataclass fields into a flat dict, merging kwargs with precedence.

  Args:
    dataclass_instance: Dataclass instance to unpack.

  Returns:
    dict: Flattened dictionary with kwargs taking precedence over fields.
  """
  # Create a copy of the dataclass's __dict__.
  params_dict: dict = dataclass_instance.__dict__.copy()

View on GitHub (pinned to 12126d8942)