{"record":{"id":"6fde2bd46799d882","repo":"apache/beam","slug":"avro-schema-doesn-t-match-row-schema-row-schema-beamschema","errorCode":null,"errorMessage":"AVRO schema doesn't match row schema. Row schema ${beamSchema}. AVRO schema + ${avroSchema}","messagePattern":"AVRO schema doesn't match row schema\\. Row schema (.+?)\\. AVRO schema \\+ (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/schemas/utils/AvroUtils.java","lineNumber":651,"sourceCode":"  /**\n   * Convert from a Beam Row to an AVRO GenericRecord. The Avro Schema is inferred from the Beam\n   * schema on the row.\n   */\n  public static GenericRecord toGenericRecord(Row row) {\n    return toGenericRecord(row, null);\n  }\n\n  /**\n   * Convert from a Beam Row to an AVRO GenericRecord. If a Schema is not provided, one is inferred\n   * from the Beam schema on the row.\n   */\n  public static GenericRecord toGenericRecord(\n      Row row, org.apache.avro.@Nullable Schema avroSchema) {\n    Schema beamSchema = row.getSchema();\n    // Use the provided AVRO schema if present, otherwise infer an AVRO schema from the row\n    // schema.\n    if (avroSchema != null && avroSchema.getFields().size() != beamSchema.getFieldCount()) {\n      throw new IllegalArgumentException(\n          \"AVRO schema doesn't match row schema. Row schema \"\n              + beamSchema\n              + \". AVRO schema + \"\n              + avroSchema);\n    }\n    if (avroSchema == null) {\n      avroSchema = toAvroSchema(beamSchema);\n    }\n\n    GenericRecordBuilder builder = new GenericRecordBuilder(avroSchema);\n    for (int i = 0; i < beamSchema.getFieldCount(); ++i) {\n      Field field = beamSchema.getField(i);\n      NullnessCheckerWorkarounds.builderSet(\n          builder,\n          field.getName(),\n          genericFromBeamField(\n              field.getType(), avroSchema.getField(field.getName()).schema(), row.getValue(i)));\n    }","sourceCodeStart":633,"sourceCodeEnd":669,"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#L633-L669","documentation":"AvroUtils.toGenericRecord converts a Beam Row to an Avro GenericRecord. When an explicit AVRO schema is supplied, it requires that the number of top-level fields exactly equal the Beam row schema's field count; otherwise the record could not be mapped field-by-field. A mismatch throws this IllegalArgumentException naming both schemas.","triggerScenarios":"Calling AvroUtils.toGenericRecord(row, avroSchema) (directly or via genericFromBeamField / the getRowToAvroFunction path) where avroSchema.getFields().size() != row.getSchema().getFieldCount().","commonSituations":"Passing a stale Avro schema after adding/removing a Beam schema field; schema evolution where the writer's Avro schema is one revision older than the pipeline's inferred Beam schema; mixing a specific record schema with a differently-shaped row (e.g. nested rows flattened differently).","solutions":["Regenerate or update the AVRO schema from the current Beam schema (AvroUtils.toAvroSchema(beamSchema)) and pass that instead.","Pass null as the AVRO schema so toGenericRecord infers a matching schema from the row schema.","Align the Beam schema: add/remove fields so the field count matches the AVRO schema.","If schemas legitimately evolved, map fields explicitly (build the GenericRecord manually or project the Row) before conversion."],"exampleFix":"// before\nGenericRecord rec = AvroUtils.toGenericRecord(row, staleAvroSchema);\n// after\nSchema avroSchema = AvroUtils.toAvroSchema(row.getSchema());\nGenericRecord rec = AvroUtils.toGenericRecord(row, avroSchema);","handlingStrategy":"validation","validationCode":"Schema beamSchema = row.getSchema();\nif (avroSchema != null && avroSchema.getFields().size() != beamSchema.getFieldCount()) {\n  avroSchema = AvroUtils.toAvroSchema(beamSchema); // re-derive matching schema\n}\nGenericRecord rec = AvroUtils.toGenericRecord(row, avroSchema);\n","typeGuard":null,"tryCatchPattern":"try {\n  return AvroUtils.toGenericRecord(row, avroSchema);\n} catch (IllegalArgumentException e) {\n  throw new SchemaMismatchException(\"Re-derive AVRO schema from beam schema\", e);\n}","preventionTips":["Never cache AVRO schemas across Beam schema changes; derive them via AvroUtils.toAvroSchema.","Run a schema-compatibility check (field count/names/types) in unit tests for schema-evolving pipelines.","Pass null to let the library infer the schema when you do not need a specific one.","Track schema versions explicitly when using a schema registry."],"tags":["avro","beam","schema-mismatch","java"],"backgroundTag":"schema-validation-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"}