{"record":{"id":"4f3ed42ab368a410","repo":"apache/beam","slug":"only-strings-allowed-as-map-keys-when-converting-from-avro","errorCode":null,"errorMessage":"Only strings allowed as map keys when converting from AVRO, found {beam_type}","messagePattern":"Only strings allowed as map keys when converting from AVRO, found (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/io/avroio.py","lineNumber":666,"sourceCode":"          to_row)\n\n\ndef avro_value_to_beam_value(\n    beam_type: schema_pb2.FieldType) -> Callable[[Any], Any]:\n  type_info = beam_type.WhichOneof(\"type_info\")\n  if type_info == \"atomic_type\":\n    return lambda value: value\n  elif type_info == \"array_type\":\n    element_converter = avro_value_to_beam_value(\n        beam_type.array_type.element_type)\n    return lambda value: [element_converter(e) for e in value]\n  elif type_info == \"iterable_type\":\n    element_converter = avro_value_to_beam_value(\n        beam_type.iterable_type.element_type)\n    return lambda value: [element_converter(e) for e in value]\n  elif type_info == \"map_type\":\n    if beam_type.map_type.key_type.atomic_type != schema_pb2.STRING:\n      raise TypeError(\n          f'Only strings allowed as map keys when converting from AVRO, '\n          f'found {beam_type}')\n    value_converter = avro_value_to_beam_value(beam_type.map_type.value_type)\n    return lambda value: {k: value_converter(v) for (k, v) in value.items()}\n  elif type_info == \"row_type\":\n    converters = {\n        field.name: avro_value_to_beam_value(field.type)\n        for field in beam_type.row_type.schema.fields\n    }\n    return lambda value: beam.Row(\n        **\n        {name: convert(value[name])\n         for (name, convert) in converters.items()})\n  elif type_info == \"logical_type\":\n    return lambda value: value\n  else:\n    raise ValueError(f\"Unrecognized type_info: {type_info!r}\")\n","sourceCodeStart":648,"sourceCodeEnd":684,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/io/avroio.py#L648-L684","documentation":"avroio.avro_value_to_beam_value builds per-type converter lambdas from Avro dicts into Beam Row values. For Beam map types, Avro (JSON) maps may only have string keys; if the Beam schema's map_type.key_type is not STRING, the function raises TypeError because there is no valid Avro representation for non-string map keys.","triggerScenarios":"Converting Avro data (avro_dict_to_beam_row or nested avro_value_to_beam_value) where the target Beam schema declares a map whose key type is INTEGER/BOOLEAN/etc. instead of STRING.","commonSituations":"Programmatically generated Beam schemas with non-string map keys being fed from Avro sources; building a Beam schema by hand that ignores Avro's key-type restriction; reading Avro with a schema derived elsewhere that used map<int, T>.","solutions":["Change the map key type to STRING in the Beam schema (schema_pb2.FieldType(atomic_type=STRING))","Restructure the data as an array of rows with explicit key/value fields instead of a non-string map","Convert at your own boundary: read the Avro dict, then Map to your desired typed structure manually instead of avro_dict_to_beam_row"],"exampleFix":"// before\nfield = schemas.schema_field('counts', schema_pb2.FieldType(map_type=schema_pb2.MapType(key_type=schema_pb2.FieldType(atomic_type=schema_pb2.INT64), value_type=...)))\n// after\nfield = schemas.schema_field('counts', schema_pb2.FieldType(map_type=schema_pb2.MapType(key_type=schema_pb2.FieldType(atomic_type=schema_pb2.STRING), value_type=...)))","handlingStrategy":"validation","validationCode":"from apache_beam.portability.api import schema_pb2\nassert all(f.type.map_type.key_type.atomic_type == schema_pb2.STRING\n           for f in beam_schema.fields if f.type.WhichOneof('type_info') == 'map_type')","typeGuard":"def has_string_map_keys(beam_type):\n    return beam_type.WhichOneof('type_info') != 'map_type' or beam_type.map_type.key_type.atomic_type == schema_pb2.STRING","tryCatchPattern":"try:\n    row = avroio.avro_dict_to_beam_row(avro_dict, beam_schema)\nexcept TypeError as e:\n    logging.error('map key conversion failed: %s', e)\n    row = None  # restructure data as key/value rows instead","preventionTips":["Design Beam schemas so all maps use STRING keys","Prefer arrays of {key,value} rows over non-string maps when bridging Avro","Validate generated schemas before wiring them into Avro reads"],"tags":["python","apache-beam","avro","schema","map"],"backgroundTag":"type-mismatch","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}