{"record":{"id":"6d2993471470353e","repo":"apache/beam","slug":"input-to-s-must-be-compatible-with-kv-any-any-found-s","errorCode":null,"errorMessage":"Input to %s must be compatible with KV[Any, Any]. Found %s.","messagePattern":"Input to (.+?) must be compatible with KV\\[Any, Any\\]\\. Found (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/typehints/typehints.py","lineNumber":1638,"sourceCode":"    return KV[Any, Any]\n  elif isinstance(element_type, TupleHint.TupleConstraint):\n    if len(element_type.tuple_types) == 2:\n      return element_type\n    else:\n      raise ValueError(\n          \"Tuple input to %s must have two components. \"\n          \"Found %s.\" % (consumer, element_type))\n  elif isinstance(element_type, AnyTypeConstraint):\n    # `Any` type needs to be replaced with a KV[Any, Any] to\n    # satisfy the KV form.\n    return KV[Any, Any]\n  elif isinstance(element_type, UnionConstraint):\n    union_types = [coerce_to_kv_type(t) for t in element_type.union_types]\n    return KV[Union[tuple(t.tuple_types[0] for t in union_types)],\n              Union[tuple(t.tuple_types[1] for t in union_types)]]\n  else:\n    # TODO: Possibly handle other valid types.\n    raise ValueError(\n        \"Input to %s must be compatible with KV[Any, Any]. \"\n        \"Found %s.\" % (consumer, element_type))\n","sourceCodeStart":1620,"sourceCodeEnd":1641,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/typehints/typehints.py#L1620-L1641","documentation":"coerce_to_kv_type cannot convert the given type hint into a KV[Any, Any] form. After checking empty, tuple, Any, and Union hints, anything else (dict, list, scalar hints, etc.) raises. Beam requires key/value-shaped input for keyed transforms.","triggerScenarios":"Feeding a PCollection hinted as Dict[K, V], List[V], str, or int into GroupByKey / CombinePerKey / CoGroupByKey, or calling coerce_to_kv_type directly with such a hint.","commonSituations":"Treating a dict side input as KV pairs for CombinePerKey; forgetting to emit key/value tuples upstream; hint mismatch after refactoring a DoFn's output type.","solutions":["Emit 2-tuples upstream: pc | beam.Map(lambda d: (d['k'], d['v'])).","Add an explicit with_output_types(KV[K, V]) on the producing stage so errors surface earlier.","If input is a dict side input, iterate pvalue.AsDict(...).items() inside a DoFn instead of using keyed transforms.","Use beam.KV keys explicitly, e.g. beam.pvalue.TaggedOutput or KV hints, for clarity."],"exampleFix":"// before\nside = p | 'side' >> beam.Create([{'a': 1}])\npc | beam.CombinePerKey(sum)  # input is dict hint\n// after\npc2 = side | beam.FlatMap(lambda d: d.items())\npc | beam.CombinePerKey(sum)  # input is KV tuples","handlingStrategy":"validation","validationCode":"if not isinstance(element, tuple) or len(element) != 2:\n    raise TypeError(f'expected KV 2-tuple, got {type(element).__name__}')","typeGuard":"def is_kv_pair(x) -> bool:\n    return isinstance(x, tuple) and len(x) == 2","tryCatchPattern":"try:\n    pc | beam.CombinePerKey(fn)\nexcept ValueError:\n    pc = pc | beam.Map(lambda x: (x[0], x[1:])) | beam.CombinePerKey(fn)","preventionTips":["Convert dict side inputs to items() before keyed transforms","Annotate PCollection output types explicitly with KV[K, V]","Keep a single DoFn responsible for producing KV-shaped output"],"tags":["python","apache-beam","type-hints","kv"],"backgroundTag":"type-mismatch","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"}