{"record":{"id":"7818264a05ee8c53","repo":"apache/beam","slug":"could-not-infer-beam-schema-from-avro-schema-avroschema","errorCode":null,"errorMessage":"Could not infer Beam schema from Avro schema: {avroSchema}","messagePattern":"Could not infer Beam schema from Avro schema: (.+?)","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":727,"sourceCode":"   * but with the with addition of making the message attributes available to the ParseFn.\n   */\n  public static <T> Read<T> readMessagesWithAttributesWithCoderAndParseFn(\n      Coder<T> coder, SimpleFunction<PubsubMessage, T> parseFn) {\n    return Read.newBuilder(parseFn).setCoder(coder).setNeedsAttributes(true).build();\n  }\n\n  /**\n   * Returns a {@link PTransform} that continuously reads binary encoded Avro messages into the Avro\n   * {@link GenericRecord} 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 Read<GenericRecord> readAvroGenericRecords(org.apache.avro.Schema avroSchema) {\n    AvroCoder<GenericRecord> coder = AvroCoder.of(avroSchema);\n    Schema schema = AvroUtils.getSchema(GenericRecord.class, avroSchema);\n    if (schema == null) {\n      throw new IllegalArgumentException(\n          \"Could not infer Beam schema from Avro schema: \" + avroSchema);\n    }\n    return Read.newBuilder(parsePayloadUsingCoder(coder))\n        .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.","sourceCodeStart":709,"sourceCodeEnd":745,"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#L709-L745","documentation":"readAvroGenericRecords converts the Avro schema into a Beam Schema via AvroUtils.getSchema. If Beam cannot infer a schema for the given Avro schema (unsupported types or an unusable schema), it returns null and this IllegalArgumentException is thrown.","triggerScenarios":"Calling PubsubIO.readAvroGenericRecords(avroSchema) with an Avro schema containing constructs AvroUtils cannot map to a Beam Schema (e.g. unsupported logical types or exotic unions).","commonSituations":"Avro schemas generated by third-party tools with unusual logical types; deeply nested unions; passing a primitive (non-record) schema that has no mappable Beam representation.","solutions":["Simplify or fix the Avro schema so all fields map to Beam types (avoid unsupported logical types/unions).","Instead of readAvroGenericRecords, use readAvros(specificClass) with a generated Avro class so Beam infers the schema from the class.","Parse the payload manually with AvroCoder and MapElements into your own typed PCollection if schema inference is not required."],"exampleFix":"// before\nSchema avroSchema = new Schema.Parser().parse(exoticSchemaJson);\nPubsubIO.readAvroGenericRecords(avroSchema);\n// after\nPubsubIO.readAvros(com.example.generated.Event.class); // Beam infers schema from class","handlingStrategy":"validation","validationCode":"Schema beamSchema = AvroUtils.getSchema(GenericRecord.class, avroSchema);\nif (beamSchema == null) {\n  throw new IllegalArgumentException(\"Beam cannot map Avro schema; simplify before readAvroGenericRecords\");\n}\nPubsubIO.readAvroGenericRecords(avroSchema);","typeGuard":"boolean beamCanMap(org.apache.avro.Schema avroSchema) {\n  return AvroUtils.getSchema(GenericRecord.class, avroSchema) != null;\n}","tryCatchPattern":"try {\n  return PubsubIO.readAvroGenericRecords(avroSchema);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Could not infer Beam schema from Avro schema\"))\n    throw new ConfigException(\"Unsupported Avro schema for Beam mapping\", e);\n  throw e;\n}","preventionTips":["Pre-check AvroUtils.getSchema before building the transform","Avoid exotic logical types/unions in schemas used with Beam","Prefer generated specific classes over GenericRecord when possible"],"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"}