{"record":{"id":"5a24f007f0f46453","repo":"apache/beam","slug":"metadata-value-type-must-be-one-of-string-long-or-byte-found-5a24f0","errorCode":null,"errorMessage":"Metadata value type must be one of String, Long, or byte[]. Found ${v.getClass().getSimpleName()}","messagePattern":"Metadata value type must be one of String, Long, or byte\\[\\]\\. Found (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/io/AvroSink.java","lineNumber":134,"sourceCode":"      CodecFactory codec = dynamicDestinations.getCodec(destination);\n      Schema schema = dynamicDestinations.getSchema(destination);\n      Map<String, Object> metadata = dynamicDestinations.getMetadata(destination);\n      DatumWriter<OutputT> datumWriter =\n          Optional.ofNullable(dynamicDestinations.getDatumWriterFactory(destination))\n              .orElse(AvroDatumFactory.of(type))\n              .apply(schema);\n\n      dataFileWriter = new DataFileWriter<>(datumWriter).setCodec(codec);\n      for (Map.Entry<String, Object> entry : metadata.entrySet()) {\n        Object v = entry.getValue();\n        if (v instanceof String) {\n          dataFileWriter.setMeta(entry.getKey(), (String) v);\n        } else if (v instanceof Long) {\n          dataFileWriter.setMeta(entry.getKey(), (Long) v);\n        } else if (v instanceof byte[]) {\n          dataFileWriter.setMeta(entry.getKey(), (byte[]) v);\n        } else {\n          throw new IllegalStateException(\n              \"Metadata value type must be one of String, Long, or byte[]. Found \"\n                  + v.getClass().getSimpleName());\n        }\n      }\n      dataFileWriter.setSyncInterval(syncInterval);\n      dataFileWriter.create(schema, Channels.newOutputStream(channel));\n    }\n\n    @Override\n    public void write(OutputT value) throws Exception {\n      dataFileWriter.append(value);\n    }\n\n    @Override\n    protected void finishWrite() throws Exception {\n      dataFileWriter.flush();\n    }\n  }","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/io/AvroSink.java#L116-L152","documentation":"AvroSink (the file-based sink used by AvroIO.Write) validates metadata map values when preparing the writer: only String, Long, and byte[] are accepted by Avro's DataFileWriter.setMeta. Any other type throws IllegalStateException.","triggerScenarios":"Providing a metadata map with values like Integer, Float, or Boolean to the AvroSink used by AvroIO.Write; thrown during prepareWrite on workers.","commonSituations":"Same as the AvroIO variant: autoboxed integers, deserialized JSON config where numbers became Integer, dynamically built metadata maps.","solutions":["Normalize metadata values to String, Long, or byte[] before building the Write transform","Replace boxed Integer values with Long","Convert unsupported types to their String representation"],"exampleFix":"// before\nmetadata.put(\"ratio\", 0.5);\n// after\nmetadata.put(\"ratio\", \"0.5\");","handlingStrategy":"type-guard","validationCode":"for (Map.Entry<String, Object> e : metadata.entrySet()) {\n  Object v = e.getValue();\n  if (!(v instanceof String || v instanceof Long || v instanceof byte[]))\n    throw new IllegalArgumentException(e.getKey() + \" is \" + v.getClass());\n}","typeGuard":"static boolean validMeta(Object v) {\n  return v instanceof String || v instanceof Long || v instanceof byte[];\n}","tryCatchPattern":"try { sink.prepareWrite(...); } catch (IllegalStateException e) { /* sanitize metadata map */ }","preventionTips":["Sanitize metadata maps coming from JSON/config sources","Never box ints into metadata maps without casting to Long","Add unit tests covering metadata types"],"tags":["avro","metadata","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-20T03:17:13.778Z"}