{"record":{"id":"5df98c46bbdf162e","repo":"apache/beam","slug":"could-not-encode-avro-from-given-row-s","errorCode":null,"errorMessage":"Could not encode avro from given row: %s","messagePattern":"Could not encode avro from given row: (.+?)","errorType":"exception","errorClass":"AvroRuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java","lineNumber":756,"sourceCode":"\n  private static class RowToAvroBytesFn extends SimpleFunction<Row, byte[]> {\n    private final org.apache.avro.Schema avroSchema;\n    private final AvroCoder<GenericRecord> coder;\n\n    RowToAvroBytesFn(Schema beamSchema) {\n      avroSchema = toAvroSchema(beamSchema);\n      coder = AvroCoder.of(avroSchema);\n    }\n\n    @Override\n    public byte[] apply(Row row) {\n      try {\n        GenericRecord record = toGenericRecord(row, avroSchema);\n        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();\n        coder.encode(record, outputStream);\n        return outputStream.toByteArray();\n      } catch (Exception e) {\n        throw new AvroRuntimeException(\n            String.format(\"Could not encode avro from given row: %s\", row), e);\n      }\n    }\n  }\n\n  /**\n   * Returns a function mapping AVRO {@link GenericRecord}s to Beam {@link Row}s for use in {@link\n   * org.apache.beam.sdk.values.PCollection#setSchema}.\n   */\n  public static SerializableFunction<GenericRecord, Row> getGenericRecordToRowFunction(\n      @Nullable Schema schema) {\n    return new GenericRecordToRowFn(schema);\n  }\n\n  private static class GenericRecordToRowFn implements SerializableFunction<GenericRecord, Row> {\n    private final @Nullable Schema schema;\n\n    GenericRecordToRowFn(@Nullable Schema schema) {","sourceCodeStart":738,"sourceCodeEnd":774,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java#L738-L774","documentation":"AvroUtils.getAvroRowToByteFunction returns a function converting a Beam Row to Avro bytes. It first converts the Row via toGenericRecord, then encodes with the Avro coder; any failure (schema mismatch, unsupported field type, encoding error) is wrapped in an AvroRuntimeException with the offending Row rendered via String.format.","triggerScenarios":"apply(row) invoked on the function returned by AvroUtils.getAvroRowToByteFunction when toGenericRecord(row, avroSchema) fails (e.g. field-count/type mismatch with the supplied schema, unsupported logical types) or coder.encode throws.","commonSituations":"Beam row schema evolved (fields added/removed/retyped) while a fixed AVRO schema is used for encoding; rows containing logical types (timestamps, decimals) incompatible with the target AVRO schema; writing to sinks (e.g. Kafka with Avro serializer) configured with an outdated schema registry subject.","solutions":["Derive the AVRO schema from the current Beam schema (AvroUtils.toAvroSchema(row.getSchema())) instead of passing a hardcoded one.","Check for null/non-nullable fields: ensure nullable Beam fields map to AVRO union [\"null\", T] schemas.","Verify all field types have supported conversions (e.g. use logicalType timestamp-micros for datetime fields) and adjust the schema.","Update the schema registry subject/version to match the schema actually used for encoding."],"exampleFix":"// before\nSchema avroSchema = hardcodedSchema;\nbyte[] out = rowToBytes.apply(row); // AvroRuntimeException on mismatch\n// after\nSchema avroSchema = AvroUtils.toAvroSchema(row.getSchema());\nbyte[] out = AvroUtils.getAvroRowToByteFunction(avroSchema).apply(row);","handlingStrategy":"try-catch","validationCode":"Schema beamSchema = row.getSchema();\nif (avroSchema != null && avroSchema.getFields().size() != beamSchema.getFieldCount()) {\n  avroSchema = AvroUtils.toAvroSchema(beamSchema); // avoid guaranteed failure\n}\n","typeGuard":null,"tryCatchPattern":"try {\n  return avroRowToByteFunction.apply(row);\n} catch (AvroRuntimeException e) {\n  log.error(\"Row failed AVRO encoding: {}\", row);\n  return deadLetter(row, e);\n}","preventionTips":["Generate the AVRO schema from the Beam schema at the same place rows are built.","Declare nullable Beam fields so they map to AVRO [\"null\", T] unions.","Keep registered schema-registry subjects compatible with the schema used for encoding.","Add a round-trip test: encode a sample row then decode it back before deploying."],"tags":["avro","beam","serialization","java"],"backgroundTag":"avro-encode-failed","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"}