{"record":{"id":"b0eee842e24f173a","repo":"apache/beam","slug":"for-genericrecord-please-call-readavrogenericrecords","errorCode":null,"errorMessage":"For GenericRecord, please call readAvroGenericRecords","messagePattern":"For GenericRecord, please call readAvroGenericRecords","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIO.java","lineNumber":749,"sourceCode":"        .setCoder(\n            SchemaCoder.of(\n                schema,\n                TypeDescriptor.of(GenericRecord.class),\n                AvroUtils.getToRowFunction(GenericRecord.class, avroSchema),\n                AvroUtils.getFromRowFunction(GenericRecord.class)))\n        .build();\n  }\n\n  /**\n   * Returns a {@link PTransform} that continuously reads binary encoded Avro messages of the\n   * specific type.\n   *\n   * <p>Beam will infer a schema for the Avro schema. This allows the output to be used by SQL and\n   * by the schema-transform library.\n   */\n  public static <T> Read<T> readAvrosWithBeamSchema(Class<T> clazz) {\n    if (clazz.equals(GenericRecord.class)) {\n      throw new IllegalArgumentException(\"For GenericRecord, please call readAvroGenericRecords\");\n    }\n    AvroCoder<T> coder = AvroCoder.of(clazz);\n    org.apache.avro.Schema avroSchema = coder.getSchema();\n    Schema schema = AvroUtils.getSchema(clazz, avroSchema);\n    if (schema == null) {\n      throw new IllegalArgumentException(\"Could not infer Beam schema for class: \" + clazz);\n    }\n    return Read.newBuilder(parsePayloadUsingCoder(coder))\n        .setCoder(\n            SchemaCoder.of(\n                schema,\n                TypeDescriptor.of(clazz),\n                AvroUtils.getToRowFunction(clazz, avroSchema),\n                AvroUtils.getFromRowFunction(clazz)))\n        .build();\n  }\n\n  /** Returns A {@link PTransform} that writes to a Google Cloud Pub/Sub stream. */","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIO.java#L731-L767","documentation":"readAvrosWithBeamSchema(Class) is meant for generated Avro classes (specific records). GenericRecord is schema-less at the type level, so the API explicitly rejects it and directs users to readAvroGenericRecords, where the Avro schema is supplied explicitly.","triggerScenarios":"Calling PubsubIO.readAvrosWithBeamSchema(GenericRecord.class) or any variable typed as GenericRecord.class.","commonSituations":"Developers who don't have generated Avro classes reaching for the generic reader with GenericRecord.class instead of the Avro-schema-based variant.","solutions":["Call PubsubIO.readAvroGenericRecords(avroSchema) passing your org.apache.avro.Schema instead.","Alternatively generate a specific Avro class (avro-maven-plugin/avro-tools) and pass that class to readAvrosWithBeamSchema."],"exampleFix":"// before\nPubsubIO.readAvrosWithBeamSchema(GenericRecord.class);\n// after\nPubsubIO.readAvroGenericRecords(new Schema.Parser().parse(schemaJson));","handlingStrategy":"type-guard","validationCode":"if (clazz == GenericRecord.class) {\n  return PubsubIO.readAvroGenericRecords(schema);\n}\nreturn PubsubIO.readAvrosWithBeamSchema(clazz);","typeGuard":"static <T> PubsubIO.Read<T> pickReader(Class<T> clazz, org.apache.avro.Schema schema) {\n  if (clazz.equals(GenericRecord.class)) {\n    throw new IllegalArgumentException(\"Use readAvroGenericRecords(schema) for GenericRecord\");\n  }\n  return null; // caller uses readAvrosWithBeamSchema(clazz)\n}","tryCatchPattern":"try {\n  return PubsubIO.readAvrosWithBeamSchema(clazz);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().contains(\"please call readAvroGenericRecords\"))\n    throw new UsageException(\"Use PubsubIO.readAvroGenericRecords(avroSchema) for GenericRecord\", e);\n  throw e;\n}","preventionTips":["Never pass GenericRecord.class to class-based Avro readers","Document which reader variant your team uses for schema-less records","Add a compile-time-style helper that dispatches on the class"],"tags":["java","avro","api-misuse","beam"],"backgroundTag":"invalid-argument-value","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"}