apache/beam · error · java.lang.IllegalArgumentException

Row expected <fieldCount> fields (<fields>). initialized wit

Error message

Row expected <fieldCount> fields (<fields>). initialized with <values.size> fields.

What it means

Row.Builder.build() verifies that the number of accumulated values equals the schema's field count. Mismatched value counts produce an IllegalArgumentException listing the expected fields and the supplied values. Rows are schema-checked records in Beam, so field count must match exactly (unless the builder is intentionally left empty for null-only rows).

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/values/Row.java:852

    public int nextFieldId() {
      return values.size();
    }

    @Internal
    public <T> Row withFieldValueGetters(
        Factory<List<FieldValueGetter<T, Object>>> fieldValueGetterFactory,
        T getterTarget,
        TypeDescriptor<?> getterTargetType) {
      checkState(getterTarget != null, "getters require withGetterTarget.");
      return new RowWithGetters<>(schema, fieldValueGetterFactory, getterTarget, getterTargetType);
    }

    public Row build() {
      checkNotNull(schema);

      if (!values.isEmpty() && values.size() != schema.getFieldCount()) {
        throw new IllegalArgumentException(
            "Row expected "
                + schema.getFieldCount()
                + String.format(
                    " fields (%s).",
                    schema.getFields().stream()
                        .map(Object::toString)
                        .collect(Collectors.joining(", ")))
                + " initialized with "
                + values.size()
                + " fields.");
      }

      if (!values.isEmpty()) {
        FieldOverrides fieldOverrides = new FieldOverrides(schema, this.values);
        if (!fieldOverrides.isEmpty()) {
          return (Row)
              new RowFieldMatcher()
                  .match(

View on GitHub (pinned to 12126d8942)

Solutions

  1. Add exactly one value per schema field, using null for missing/NULL values.
  2. Regenerate the schema from the current class (Schema.of / avro reflection) and align builder calls with it.
  3. Use Row.withSchema(schema).addValues(...) with a list whose size equals schema.getFieldCount().
  4. If fields were added to the schema, update all Row construction sites (search for withSchema usages).

Example fix

// before
Row row = Row.withSchema(schema) // schema has 3 fields
    .addValue("a")
    .addValue(1)
    .build(); // IllegalArgumentException: expected 3 fields
// after
Row row = Row.withSchema(schema)
    .addValue("a")
    .addValue(1)
    .addValue(null) // third field
    .build();
Defensive patterns

Strategy: validation

Validate before calling

if (values.size() != schema.getFieldCount()) {
  throw new IllegalArgumentException("Row needs " + schema.getFieldCount() + " values, got " + values.size());
}

Type guard

static boolean matchesSchema(List<Object> values, Schema schema) {
  return values == null ? schema.getFieldCount() == 0 : values.size() == schema.getFieldCount();
}

Try / catch

try {
  Row row = builder.build();
} catch (IllegalArgumentException e) {
  throw new IllegalStateException("Row/Schema field count mismatch: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: Calling Row.withSchema(...).addValue(...) a different number of times than schema.getFieldCount(); building a Row from a list whose size differs from the schema; schema evolved (fields added/removed) but row-construction code not updated.

Common situations: Schema drift after adding a field to an Avro/Beam schema without updating hand-built Rows; constructing Rows in tests with partial values; mapping a database record with NULL columns skipped instead of added as null values.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/1ac16cf8b361b305. Report an issue: GitHub.