{"record":{"id":"74a1afefd996e5aa","repo":"apache/beam","slug":"field-keyfield-should-of-type-bytes-please-change-the-type","errorCode":null,"errorMessage":"Field `{keyField}` should of type `BYTES`. Please change the type or specify a field to store the KEY value.","messagePattern":"Field `(.+?)` should of type `BYTES`\\. Please change the type or specify a field to store the KEY value\\.","errorType":"validation","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/datastore/EntityToRow.java","lineNumber":54,"sourceCode":"import org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\n\n/** A {@code PTransform} to perform a conversion of {@link Entity} to {@link Row}. */\n@SuppressWarnings({\n  \"nullness\" // TODO(https://github.com/apache/beam/issues/20497)\n})\npublic class EntityToRow extends PTransform<PCollection<Entity>, PCollection<Row>> {\n  private final Schema schema;\n  private final String keyField;\n  private static final Logger LOG = LoggerFactory.getLogger(EntityToRow.class);\n\n  private EntityToRow(Schema schema, String keyField) {\n    this.schema = schema;\n    this.keyField = keyField;\n\n    if (schema.getFieldNames().contains(keyField)) {\n      if (!schema.getField(keyField).getType().getTypeName().equals(Schema.TypeName.BYTES)) {\n        throw new IllegalStateException(\n            \"Field `\"\n                + keyField\n                + \"` should of type `BYTES`. Please change the type or specify a field to\"\n                + \" store the KEY value.\");\n      }\n      LOG.info(\"Entity KEY will be stored under `{}` field.\", keyField);\n    }\n  }\n\n  /**\n   * Create a PTransform instance.\n   *\n   * @param schema {@code Schema} of the target row.\n   * @param keyField A name of the row field to store the {@code Key} in.\n   * @return {@code PTransform} instance for Entity to Row conversion.\n   */\n  public static EntityToRow create(Schema schema, String keyField) {\n    return new EntityToRow(schema, keyField);","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/datastore/EntityToRow.java#L36-L72","documentation":"EntityToRow converts a Datastore Entity into a Beam Row. If the schema declares a field for storing the Entity KEY, that field must be of Beam type BYTES. This error is thrown from the private constructor when the chosen keyField exists in the schema but its type is anything other than BYTES, because the raw key bytes could not be stored correctly.","triggerScenarios":"Constructing an EntityToRow via withSchema/descendant factory methods with a keyField name that exists in the schema but is declared as e.g. STRING, INT64, or ROW instead of BYTES.","commonSituations":"Schema imported from BigQuery/Avro where the key column was inferred as STRING; user renamed the key field but left the old column type; copy-paste of a schema where the key field was given a human-readable type.","solutions":["Change the keyField column type in the schema to Schema.FieldType.BYTES","Or pick a different (BYTES-typed) field via the keyField option, or add a new BYTES field to hold the key","Regenerate the schema from your pipeline so the key field is defined as BYTES from the start"],"exampleFix":"// before\nSchema schema = Schema.of(Schema.Field.of(\"id\", Schema.FieldType.STRING));\n// after\nSchema schema = Schema.of(Schema.Field.of(\"id\", Schema.FieldType.BYTES));","handlingStrategy":"validation","validationCode":"if (schema.getFieldNames().contains(keyField)\n    && !schema.getField(keyField).getType().getTypeName().equals(Schema.TypeName.BYTES)) {\n  throw new IllegalArgumentException(\n      \"keyField `\" + keyField + \"` must be BYTES, got \"\n      + schema.getField(keyField).getType().getTypeName());\n}","typeGuard":"boolean isValidKeyField(Schema schema, String keyField) {\n  return !schema.getFieldNames().contains(keyField)\n      || schema.getField(keyField).getType().getTypeName().equals(Schema.TypeName.BYTES);\n}","tryCatchPattern":null,"preventionTips":["Define the KEY-holding field as Schema.FieldType.BYTES when authoring schemas","Validate schema field types at pipeline construction time, before expand/run","When importing schemas from BigQuery/Avro, override the key column type to BYTES"],"tags":["gcp","datastore","schema","type-mismatch","beam"],"backgroundTag":"type-mismatch","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"}