{"record":{"id":"fd0a641cf6dfb523","repo":"apache/beam","slug":"only-strings-allowd-as-map-keys-when-converting-from-json","errorCode":null,"errorMessage":"Only strings allowd as map keys when converting from JSON, found {beam_type}","messagePattern":"Only strings allowd as map keys when converting from JSON, found (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/yaml/json_utils.py","lineNumber":191,"sourceCode":"  if beam_type.nullable:\n    non_null_type = schema_pb2.FieldType()\n    non_null_type.CopyFrom(beam_type)\n    non_null_type.nullable = False\n    non_null_converter = json_to_row(non_null_type)\n    return lambda value: None if value is None else non_null_converter(value)\n\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 = json_to_row(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 = json_to_row(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 allowd as map keys when converting from JSON, '\n          f'found {beam_type}')\n    value_converter = json_to_row(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    field_nullable_status = {\n        field.name: field.type.nullable\n        for field in beam_type.row_type.schema.fields\n    }\n\n    converters = {\n        field.name: json_to_row(field.type)\n        for field in beam_type.row_type.schema.fields\n    }\n\n    def convert_row(value):\n      kwargs = {}\n      for name, convert in converters.items():","sourceCodeStart":173,"sourceCodeEnd":209,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/yaml/json_utils.py#L173-L209","documentation":"json_to_row converts parsed JSON values into Beam Rows following a Beam schema. For map-typed fields, JSON objects only support string keys, so if the Beam schema's map key type is not STRING, a TypeError is raised (note the message contains a typo: 'allowd'). The Beam schema is stricter than JSON allows.","triggerScenarios":"A Beam schema field with map_type whose key_type is int/bytes/etc., while feeding JSON input through json_to_row or a json_parser-based source (e.g. yaml transforms consuming JSON with a declared schema).","commonSituations":"Declaring a schema in pipeline YAML with map keys of integer/long and a JSON input source; converting between Avro/Parquet (int-keyed maps) and JSON within the same yaml pipeline.","solutions":["Change the schema so map key_type is string","Pre-convert non-string keys to strings before JSON conversion (e.g. stringify in an earlier step)","Use a row/array representation instead of a map if keys must be non-strings"],"exampleFix":"# before\nschema: \"map<integer, string> counts\"\n# after\nschema: \"map<string, string> counts\"","handlingStrategy":"validation","validationCode":"from apache_beam.typehelp import schema_pb2\ndef assert_string_map_keys(beam_schema):\n    for f in beam_schema.fields:\n        t = f.type\n        if t.WhichOneof('type_info') == 'map_type' and t.map_type.key_type.atomic_type != schema_pb2.STRING:\n            raise TypeError(f'Field {f.name}: JSON maps require string keys')","typeGuard":"def has_string_map_keys(beam_type) -> bool:\n    if beam_type.WhichOneof('type_info') != 'map_type':\n        return True\n    return beam_type.map_type.key_type.atomic_type == schema_pb2.STRING","tryCatchPattern":"try:\n    row = json_parser(beam_schema)(raw)\nexcept TypeError as e:\n    raise TypeError(f'Re-declare map keys as string in schema: {e}') from e","preventionTips":["Declare map fields as map<string, ...> whenever data passes through JSON","Convert non-string keys to strings before JSON conversion","Avoid mixing int-keyed Avro/Parquet maps with JSON sinks in one pipeline"],"tags":["python","apache-beam","json","type-mismatch"],"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"}