{"record":{"id":"42e0422662a5038b","repo":"apache/beam","slug":"ptransform-create-refusing-to-treat-string-as-an-iterable","errorCode":null,"errorMessage":"PTransform Create: Refusing to treat string as an iterable. (string=%r)","messagePattern":"PTransform Create: Refusing to treat string as an iterable\\. \\(string=%r\\)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/core.py","lineNumber":4192,"sourceCode":"        pcolls.append(pcoll.pipeline | other)\n      else:\n        raise TypeError(\n            'FlattenWith only takes other PCollections and PTransforms, '\n            f'got {other}')\n    return tuple(pcolls) | Flatten()\n\n\nclass Create(PTransform):\n  \"\"\"A transform that creates a PCollection from an iterable.\"\"\"\n  def __init__(self, values, reshuffle=True):\n    \"\"\"Initializes a Create transform.\n\n    Args:\n      values: An object of values for the PCollection\n    \"\"\"\n    super().__init__()\n    if isinstance(values, (str, bytes)):\n      raise TypeError(\n          'PTransform Create: Refusing to treat string as '\n          'an iterable. (string=%r)' % values)\n    elif isinstance(values, dict):\n      values = values.items()\n    self.values = tuple(values)\n    self.reshuffle = reshuffle\n    self._coder = typecoders.registry.get_coder(self.get_output_type())\n\n  def __getstate__(self):\n    serialized_values = [self._coder.encode(v) for v in self.values]\n    return serialized_values, self.reshuffle, self._coder\n\n  def __setstate__(self, state):\n    serialized_values, self.reshuffle, self._coder = state\n    self.values = [self._coder.decode(v) for v in serialized_values]\n\n  def to_runner_api_parameter(self, context):\n    # type: (PipelineContext) -> typing.Tuple[str, bytes]","sourceCodeStart":4174,"sourceCodeEnd":4210,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/core.py#L4174-L4210","documentation":"Raised by Create.__init__ when values is a str or bytes. Because strings are iterable character-by-character, Create would otherwise silently produce a PCollection of individual characters (or byte ints), which is almost never intended. Beam refuses outright and asks you to pass an explicit iterable of the elements you want.","triggerScenarios":"beam.Create('hello') or beam.Create(b'data'); passing a config value that is a string when a list was expected (e.g. a comma-separated string of items); pipeline start values read from environment variables as strings.","commonSituations":"Loading initial data from env vars/CLI args that arrive as strings; intending a single-element collection containing one string; JSON fields that are strings rather than arrays.","solutions":["Wrap the string in a list if you want one element: beam.Create(['hello']).","Split delimited strings into a list first: beam.Create(csv_str.split(',')).","Parse JSON strings before passing: beam.Create(json.loads(s)).","For bytes content, wrap similarly: beam.Create([b'data'])."],"exampleFix":"// before\npc = p | beam.Create('hello')  # refused\n// after\npc = p | beam.Create(['hello'])  # one element\n# or\npc = p | beam.Create('hello'.split(','))","handlingStrategy":"type-guard","validationCode":"assert not isinstance(values, (str, bytes)), 'Create needs an iterable of elements, wrap strings in a list'","typeGuard":"def is_create_input(v):\n    return not isinstance(v, (str, bytes)) and hasattr(v, '__iter__')","tryCatchPattern":"try:\n    pc = p | beam.Create(values)\nexcept TypeError as e:\n    log.error('Create input error: %s', e)","preventionTips":["Wrap strings in a list to emit them as single elements","Split or parse string config values before Create","Sanity-check the source of initial data (env vars, JSON) for bare strings"],"tags":["python","apache-beam","create","string-iterable"],"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"}