apache/beam · error · ValueError

ReadFromMongDB db param must be specified as a string

Error message

ReadFromMongDB db param must be specified as a string

What it means

ReadFromMongoDB.__init__ validates that the db parameter is a Python str and raises ValueError otherwise. The database name is passed straight into the MongoDB URI/client config, so non-string values (bytes, None, ObjectId) would produce invalid client parameters downstream.

Solutions

  1. Pass the db name as a str, e.g. db='mydb'.
  2. If sourced from config/env, coerce with str(value) and check it is not None/empty first.
  3. Decode bytes with .decode('utf-8') before passing.
  4. Fix argument order so a different value isn't landing in db.

Example fix

// before
p = ReadFromMongoDB(uri=uri, db=os.environ.get('MONGO_DB'), coll='users')
// after
db = os.environ.get('MONGO_DB')
assert isinstance(db, str) and db
p = ReadFromMongoDB(uri=uri, db=db, coll='users')
Defensive patterns

Strategy: type-guard

Validate before calling

if not isinstance(db, str) or not db:
    raise ValueError('db must be a non-empty string')

Type guard

def is_db_name(v) -> bool:
    return isinstance(v, str) and bool(v)

Try / catch

try:
    t = ReadFromMongoDB(uri=uri, db=db, coll=coll)
except ValueError as e:
    raise ConfigError(f'bad Mongo source config: {e}') from e

Prevention

When it happens

Trigger: Calling ReadFromMongoDB(uri=..., db=<non-string>, coll=...) e.g. db=None because a config variable was unset, or db read as bytes from an environment/config file.

Common situations: Loading db name from env vars/config that returned None; passing bytes from os.environb or YAML-parsed values; typos passing a positional arg into db.

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

Appendix: source

Thrown at sdks/python/apache_beam/io/mongodbio.py:153

        specifying elements which must be present for a document to be included
        in the result set.
      projection: A list of field names that should be returned in the result
        set or a dict specifying the fields to include or exclude.
      extra_client_params(dict): Optional `MongoClient
        <https://api.mongodb.com/python/current/api/pymongo/mongo_client.html>`_
        parameters.
      bucket_auto (bool): If :data:`True`, use MongoDB `$bucketAuto` aggregation
        to split collection into bundles instead of `splitVector` command,
        which does not work with MongoDB Atlas.
        If :data:`False` (the default), use `splitVector` command for bundling.

    Returns:
      :class:`~apache_beam.transforms.ptransform.PTransform`
    """
    if extra_client_params is None:
      extra_client_params = {}
    if not isinstance(db, str):
      raise ValueError("ReadFromMongDB db param must be specified as a string")
    if not isinstance(coll, str):
      raise ValueError(
          "ReadFromMongDB coll param must be specified as a string")
    self._mongo_source = _BoundedMongoSource(
        uri=uri,
        db=db,
        coll=coll,
        filter=filter,
        projection=projection,
        extra_client_params=extra_client_params,
        bucket_auto=bucket_auto,
    )

  def expand(self, pcoll):
    return pcoll | iobase.Read(self._mongo_source)


class _ObjectIdRangeTracker(OrderedPositionRangeTracker):

View on GitHub (pinned to 12126d8942)