{"record":{"id":"0bb13479c9c0769b","repo":"apache/beam","slug":"error-parsing-string-raw-str-e","errorCode":null,"errorMessage":"Error parsing string:\n{raw_str}\n{e}","messagePattern":"Error parsing string:\n(.+?)\n(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/rag/utils.py","lineNumber":117,"sourceCode":"      'defaultdict': defaultdict,\n      'list': list,\n      '__builtins__': {}\n  }\n\n  for raw_str in chunk_str_list:\n    try:\n      # replace \"<class 'list'>\" with actual list reference.\n      cleaned_str = re.sub(\n          r\"defaultdict\\(<class 'list'>\", \"defaultdict(list\", raw_str)\n\n      # Evaluate string in restricted environment.\n      chunk = eval(cleaned_str, safe_globals)  # pylint: disable=eval-used\n      if isinstance(chunk, Chunk):\n        parsed_chunks.append(chunk)\n      else:\n        raise ValueError(\"Parsed object is not a Chunk instance\")\n    except Exception as e:\n      raise ValueError(f\"Error parsing string:\\n{raw_str}\\n{e}\")\n\n  return parsed_chunks\n\n\ndef unpack_dataclass_with_kwargs(dataclass_instance):\n  \"\"\"Unpacks dataclass fields into a flat dict, merging kwargs with precedence.\n\n  Args:\n    dataclass_instance: Dataclass instance to unpack.\n\n  Returns:\n    dict: Flattened dictionary with kwargs taking precedence over fields.\n  \"\"\"\n  # Create a copy of the dataclass's __dict__.\n  params_dict: dict = dataclass_instance.__dict__.copy()\n\n  # Extract the nested kwargs dictionary.\n  nested_kwargs = params_dict.pop('kwargs', {})","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/rag/utils.py#L99-L135","documentation":"parse_chunk_strings evaluates each cleaned string with eval() inside safe_globals and expects the result to be a langchain Chunk instance. Any exception during parsing/eval, or a parsed object that is not a Chunk, is re-raised as this ValueError with the original raw string and the underlying error.","triggerScenarios":"Calling parse_chunk_strings with strings that are not valid Python literals/expressions, strings that evaluate to something other than a Chunk (e.g. a plain dict or str), or strings referencing names not present in safe_globals.","commonSituations":"Embedding data serialized by a different pipeline version that stored dicts instead of Chunk objects; corrupted or truncated chunk strings in a Milvus-backed RAG index; hand-edited chunk records.","solutions":["Inspect the raw_str in the message and fix the stored string so it evaluates to a Chunk instance.","Re-serialize the chunks with the same Chunk class/version used to write them, then rerun the pipeline.","If the source data is plain dicts, wrap them in Chunk(...) before passing strings to parse_chunk_strings.","Catch ValueError per string and skip/log malformed entries instead of failing the whole batch."],"exampleFix":"// before\nchunks = parse_chunk_strings(['{\"content\": \"text\"}'])\n// after\nfrom apache_beam.ml.rag.chunking import Chunk\nraw = '{\"content\": \"text\"}'\nimport json\nchunks = [Chunk(content=d['content']) for d in [json.loads(raw)]]","handlingStrategy":"validation","validationCode":"import ast\nfor s in strings:\n    try:\n        ast.parse(s)\n    except SyntaxError as e:\n        raise ValueError(f'Unparseable chunk string: {s[:80]}...') from e","typeGuard":"def is_chunk_str(s: str) -> bool:\n    import ast\n    try:\n        ast.parse(s)\n        return True\n    except SyntaxError:\n        return False","tryCatchPattern":"try:\n    chunks = parse_chunk_strings(raw_strings)\nexcept ValueError as e:\n    logger.error('Chunk parse failed: %s', e)\n    chunks = []","preventionTips":["Serialize chunks with the same Chunk class used at parse time","Validate stored strings parse before running the pipeline","Keep a single serialization format for chunk records"],"tags":["python","parsing","rag","eval"],"backgroundTag":"invalid-argument-format","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}