{"record":{"id":"59ee0f76d9f9d7fb","repo":"apache/beam","slug":"could-not-infer-beam-schema-for-class-clazz","errorCode":null,"errorMessage":"Could not infer Beam schema for class: {clazz}","messagePattern":"Could not infer Beam schema for class: (.+?)","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":755,"sourceCode":"        .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. */\n  public static Write<PubsubMessage> writeMessages() {\n    return Write.newBuilder()\n        .setTopicProvider(null)\n        .setTopicFunction(null)\n        .setDynamicDestinations(false)\n        .build();","sourceCodeStart":737,"sourceCodeEnd":773,"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#L737-L773","documentation":"readAvrosWithBeamSchema(clazz) asks AvroUtils.getSchema to derive a Beam Schema from the Avro class. If it returns null (the Avro-generated class cannot be mapped to a Beam schema), this IllegalArgumentException is thrown.","triggerScenarios":"Passing a class whose Avro schema contains types Beam cannot map (unsupported logical types), or a non-Avro-generated class (though AvroCoder.of may fail earlier), to readAvrosWithBeamSchema.","commonSituations":"Using Avro classes generated with unusual compiler settings or older avro-tools versions producing unmappable fields; classes with reflective/union-typed fields.","solutions":["Regenerate the Avro class with a current avro-tools/avro-maven-plugin version so the schema is Beam-mappable.","Simplify unsupported fields (e.g. replace exotic logical types with string/long) in the .proto—rather the .avsc—and regenerate.","Fall back to readAvroGenericRecords(schema) plus manual mapping into a typed PCollection."],"exampleFix":"// before\nPubsubIO.readAvrosWithBeamSchema(LegacyEvent.class); // schema not mappable\n// after (regenerated class with supported types)\nPubsubIO.readAvrosWithBeamSchema(com.example.generated.Event.class);","handlingStrategy":"validation","validationCode":"Schema beamSchema = AvroUtils.getSchema(clazz, AvroCoder.of(clazz).getSchema());\nif (beamSchema == null) {\n  throw new IllegalArgumentException(\"Beam cannot map Avro class \" + clazz + \"; regenerate with supported types\");\n}\nPubsubIO.readAvrosWithBeamSchema(clazz);","typeGuard":"boolean beamCanMapClass(Class<?> clazz) {\n  return AvroUtils.getSchema(clazz, AvroCoder.of(clazz).getSchema()) != null;\n}","tryCatchPattern":"try {\n  return PubsubIO.readAvrosWithBeamSchema(clazz);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Could not infer Beam schema for class\"))\n    throw new ConfigException(\"Avro class not Beam-schema mappable: \" + clazz, e);\n  throw e;\n}","preventionTips":["Pre-check AvroUtils.getSchema for the class","Keep avro codegen plugins up to date","Avoid unsupported logical types in .avsc files"],"tags":["java","avro","schema","beam"],"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"}