{"record":{"id":"c90540fb586c8ed7","repo":"apache/beam","slug":"avrocoder-for-genericrecord-requires-a-schema","errorCode":null,"errorMessage":"AvroCoder for GenericRecord requires a schema","messagePattern":"AvroCoder for GenericRecord requires a schema","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/coders/AvroCoder.java","lineNumber":233,"sourceCode":"\n  /**\n   * Returns an {@code AvroCoder} instance for the provided element class.\n   *\n   * @param <T> the element type\n   */\n  public static <T> AvroCoder<T> of(Class<T> clazz) {\n    return of(clazz, true);\n  }\n\n  /**\n   * Returns an {@code AvroCoder} instance for the given class, respecting whether to use Avro's\n   * Reflect* or Specific* suite for encoding and decoding.\n   *\n   * @param <T> the element type\n   */\n  public static <T> AvroCoder<T> of(Class<T> type, boolean useReflectApi) {\n    if (GenericRecord.class.equals(type)) {\n      throw new IllegalArgumentException(\"AvroCoder for GenericRecord requires a schema\");\n    } else if (SpecificRecord.class.isAssignableFrom(type) && !useReflectApi) {\n      return specific(type);\n    } else {\n      return reflect(type);\n    }\n  }\n\n  /**\n   * Returns an {@code AvroCoder} instance for the provided element type using the provided Avro\n   * schema\n   *\n   * <p>The schema must correspond to the type provided.\n   *\n   * @param <T> the element type\n   */\n  public static <T> AvroCoder<T> of(Class<T> type, Schema schema) {\n    return of(type, schema, true);\n  }","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/extensions/avro/src/main/java/org/apache/beam/sdk/extensions/avro/coders/AvroCoder.java#L215-L251","documentation":"AvroCoder.of(Class) refuses to create a coder for raw GenericRecord.class because a GenericRecord carries no embedded schema; a schema must be supplied explicitly. Calling the class-only factory with GenericRecord is therefore an invalid use and throws IllegalArgumentException.","triggerScenarios":"Calling AvroCoder.of(GenericRecord.class) (optionally with useReflectApi) without going through the schema-taking overload AvroCoder.of(GenericRecord.class, schemaJsonString) or AvroCoder.of(schema).","commonSituations":"Generic pipelines that pass Class objects generically and forget GenericRecord is special; coders inferred from a PCollection element type after a transform widened it to GenericRecord; copying sample code that used SpecificRecord and switching to GenericRecord.","solutions":["Use the schema-taking factory: AvroCoder.of(GenericRecord.class, schemaString) or AvroCoder.of(GenericRecord.class, Schema)","Use AvroCoder.of(Schema) / of(Class<T>, String schema) so the coder has a schema","Define a SpecificRecord (avro-generated class) instead of GenericRecord so no schema argument is needed","Check the pipeline for a generic .setCoder(AvroCoder.of(type)) call and special-case GenericRecord"],"exampleFix":"// before\nAvroCoder.of(GenericRecord.class) // throws\n// after\nAvroCoder.of(GenericRecord.class, schemaString)","handlingStrategy":"validation","validationCode":"if (GenericRecord.class.equals(type) && schemaString == null) {\n  throw new IllegalArgumentException(\"Provide an Avro schema when coding GenericRecord\");\n}","typeGuard":"boolean canCreateClassOnlyAvroCoder(Class<?> type) {\n  return !GenericRecord.class.equals(type);\n}","tryCatchPattern":"try {\n  coder = AvroCoder.of(type);\n} catch (IllegalArgumentException e) {\n  coder = AvroCoder.of(GenericRecord.class, schemaString);\n}","preventionTips":["Never call AvroCoder.of(GenericRecord.class); always keep the schema string alongside GenericRecord pipelines","Prefer SpecificRecord classes generated from your .avsc files","Centralize coder creation in one factory so GenericRecord is special-cased once"],"tags":["java","apache-beam","avro","coder"],"backgroundTag":"missing-required-argument","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}