{"record":{"id":"408738d45b7e86a1","repo":"apache/beam","slug":"empty-list-maps-to-model-handler-mh-all-model-handlers-must","errorCode":null,"errorMessage":"Empty list maps to model handler {mh}. All model handlers must have one or more associated keys.","messagePattern":"Empty list maps to model handler (.+?)\\. All model handlers must have one or more associated keys\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/ml/inference/base.py","lineNumber":822,"sourceCode":"            'ignored %s. Batching kwargs are not respected when '\n            'more than one model handler is used in a KeyedModelHandler. If '\n            'you would like to specify resource hints, you can do so by '\n            'overriding the KeyedModelHandler.batch_elements_kwargs() method.',\n            hints,\n            batch_kwargs)\n      env_vars = getattr(mh, '_env_vars', {})\n      if len(env_vars) > 0:\n        logging.warning(\n            'mh %s defines the following _env_vars which will be ignored %s. '\n            '_env_vars are not respected when more than one model handler is '\n            'used in a KeyedModelHandler. If you need env vars set at '\n            'inference time, you can do so with '\n            'a custom inference function.',\n            mh,\n            env_vars)\n\n      if len(keys) == 0:\n        raise ValueError(\n            f'Empty list maps to model handler {mh}. All model handlers must '\n            'have one or more associated keys.')\n      self._id_to_mh_map[keys[0]] = mh\n      for key in keys:\n        if key in self._key_to_id_map:\n          raise ValueError(\n              f'key {key} maps to multiple model handlers. All keys must map '\n              'to exactly one model handler.')\n        self._key_to_id_map[key] = keys[0]\n\n  def load_model(self) -> Union[ModelT, _ModelHandlerManager]:\n    if self._single_model:\n      return self._unkeyed.load_model()\n    return _ModelHandlerManager(self._id_to_mh_map)\n\n  def run_inference(\n      self,\n      batch: Sequence[tuple[KeyT, ExampleT]],","sourceCodeStart":804,"sourceCodeEnd":840,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/ml/inference/base.py#L804-L840","documentation":"In the multi-handler KeyedModelHandler constructor, every model handler must be associated with at least one key so elements can be routed to it. An empty key list makes that handler unreachable, so a ValueError naming the offending handler is raised.","triggerScenarios":"KeyedModelHandler([KeyedModelHandlerTuple(mh, [])]) — a tuple whose keys argument is an empty list, e.g. keys computed dynamically from an empty cohort or an empty config list.","commonSituations":"Cohort keys loaded from a file/config that turned out empty; filtering code that removed all keys for a handler; programmatic construction where the keys list was initialized but never populated.","solutions":["Provide a non-empty list of keys for every KeyedModelHandlerTuple.","Validate key lists before construction and drop (or fix) handlers with no keys.","Check the source of the keys (config, side input) for empty values at pipeline-build time."],"exampleFix":"# before\nKeyedModelHandler([KeyedModelHandlerTuple(mh, cohort_keys)])  # cohort_keys == []\n\n# after\nif not cohort_keys:\n  raise ValueError('cohort_keys must be non-empty')\nKeyedModelHandler([KeyedModelHandlerTuple(mh, cohort_keys)])","handlingStrategy":"validation","validationCode":"for t in tuples:\n    if len(t.keys) == 0:\n        raise ValueError(f'Handler {t.mh} has no associated keys')","typeGuard":"def all_keys_nonempty(tuples):\n    return all(len(t.keys) > 0 for t in tuples)","tryCatchPattern":"try:\n    keyed = KeyedModelHandler(tuples)\nexcept ValueError as e:\n    if 'Empty list maps to model handler' in str(e):\n        raise ConfigError('Check cohort key config; a handler received no keys') from e\n    raise","preventionTips":["Validate cohort/key config files before building the pipeline.","Fail fast at config-load time when a cohort's key list is empty.","Log the source of each key list so empty lists are traceable."],"tags":["python","apache-beam","ml-inference","validation","empty-list"],"backgroundTag":"empty-required-field","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}