{"record":{"id":"9ee7caa21ec57443","repo":"apache/beam","slug":"length-of-parameters-to-a-dict-type-hint-must-be-exactly-2","errorCode":null,"errorMessage":"Length of parameters to a Dict type-hint must be exactly 2. Passed parameters: %s, have a length of %s.","messagePattern":"Length of parameters to a Dict type-hint must be exactly 2\\. Passed parameters: (.+?), have a length of (.+?)\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/typehints.py","lineNumber":954,"sourceCode":"        return bindings\n      return {}\n\n    def bind_type_variables(self, bindings):\n      bound_key_type = bind_type_variables(self.key_type, bindings)\n      bound_value_type = bind_type_variables(self.value_type, bindings)\n      if (bound_key_type, self.key_type) == (bound_value_type, self.value_type):\n        return self\n      return Dict[bound_key_type, bound_value_type]\n\n  def __getitem__(self, type_params):\n    # Type param must be a (k, v) pair.\n    if not isinstance(type_params, tuple):\n      raise TypeError(\n          'Parameter to Dict type-hint must be a tuple of types: '\n          'Dict[.., ..].')\n\n    if len(type_params) != 2:\n      raise TypeError(\n          'Length of parameters to a Dict type-hint must be exactly 2. Passed '\n          'parameters: %s, have a length of %s.' %\n          (type_params, len(type_params)))\n\n    key_type, value_type = type_params\n\n    validate_composite_type_param(\n        key_type, error_msg_prefix='Key-type parameter to a Dict hint')\n    validate_composite_type_param(\n        value_type, error_msg_prefix='Value-type parameter to a Dict hint')\n\n    return self.DictConstraint(key_type, value_type)\n\n\nDictConstraint = DictHint.DictConstraint\n\n\nclass SetHint(CompositeTypeHint):","sourceCodeStart":936,"sourceCodeEnd":972,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/typehints.py#L936-L972","documentation":"Apache Beam's Dict[...] type-hint requires exactly two type parameters (key type and value type). The Dict class __getitem__ validates the subscription expression and raises TypeError when a tuple of length != 2 is passed. This happens at pipeline construction time when the type hint is written.","triggerScenarios":"Writing Dict[int] (missing value type), Dict[int, str, float] (extra parameter), or passing a non-parameterized hint list/tuple of wrong length to Dict[...] in @DoFn.process_element type annotations or ApplyOptions(type_check=True).","commonSituations":"Hand-writing annotations where the value type is forgotten (Dict[str] instead of Dict[str, int]); generating hints programmatically by passing a list like Dict[[int, str]] which is not a tuple, or a tuple of three types; copy-paste from typing.Dict habits.","solutions":["Pass exactly two type parameters: Dict[key_type, value_type]","If only one dimension is known, use the wildcard Any for the other: Dict[int, Any]","Ensure the parameters form a tuple — write Dict[int, str], not Dict[[int, str]]"],"exampleFix":"// before\nresult = p | beam.Map(lambda x: x) .with_output_types(Dict[str])\n// after\nresult = p | beam.Map(lambda x: x) .with_output_types(Dict[str, int])","handlingStrategy":"type-guard","validationCode":"def is_valid_dict_hint(params):\n    return isinstance(params, tuple) and len(params) == 2\n\n# call Dict[params] only if is_valid_dict_hint(params)","typeGuard":"def valid_dict_params(params):\n    return isinstance(params, tuple) and len(params) == 2","tryCatchPattern":"try:\n    hint = Dict[params]\nexcept TypeError as e:\n    if 'Dict type-hint' in str(e):\n        hint = Dict[Any, Any]","preventionTips":["Always write Dict[K, V] with two parameters","Use Any for unconstrained sides","Lint annotations with mypy which flags wrong generic arity"],"tags":["python","apache-beam","type-hints","dict"],"backgroundTag":"invalid-argument-value","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"}