apache/beam · error · RuntimeException

Can't cast numbers to non-numeric type: +output

Error message

Can't cast numbers to non-numeric type: +output

What it means

Cast.castNumber also validates that the OUTPUT TypeName is numeric; casting a number to e.g. a STRING or BOOLEAN output type is rejected with a RuntimeException rather than silently converting via toString.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/transforms/Cast.java:340

      int fromFieldIdx = inputSchema.indexOf(outputField.getName());
      Schema.Field inputField = inputSchema.getField(fromFieldIdx);

      Object inputValue = input.getValue(fromFieldIdx);
      Object outputValue = castValue(inputValue, inputField.getType(), outputField.getType());

      output.addValue(outputValue);
    }

    return output.build();
  }

  public static Number castNumber(Number value, TypeName input, TypeName output) {
    if (!input.isNumericType()) {
      throw new RuntimeException("Can't cast non-numeric types: " + input);
    }

    if (!output.isNumericType()) {
      throw new RuntimeException("Can't cast numbers to non-numeric type: " + output);
    }

    if (value == null) {
      return null;
    }

    if (input == output) {
      return value;
    }

    switch (output) {
      case BYTE:
        return value.byteValue();

      case INT16:
        return value.shortValue();

      case INT32:

View on GitHub (pinned to 12126d8942)

Solutions

  1. Change the output schema field to a numeric type matching the input.
  2. Convert the numeric field to string explicitly beforehand with a Map transform instead of Cast.
  3. Use a custom cast function (castRow with per-field Function) that handles number->string conversion.
  4. Validate input and output schemas for type compatibility before applying Cast.

Example fix

// before
Schema out = Schema.of(Schema.Field.of("id", FieldType.STRING)); // id was INT64
rows.apply(Cast.to(out));
// after
rows.apply(MapElements.into(TypeDescriptor.of(Row.class))
    .via(r -> Row.withSchema(out).addValue(String.valueOf(r.getInt64("id"))).build()))
Defensive patterns

Strategy: validation

Validate before calling

Schema.Field in = inSchema.getField("id");
Schema.Field out = outSchema.getField("id");
if (in.getType().getTypeName().isNumericType() && !out.getType().getTypeName().isNumericType()) {
  throw new IllegalStateException("id requires number->non-numeric conversion, not Cast");
}

Try / catch

try { rows.apply(Cast.to(outSchema)); } catch (RuntimeException e) { if (e.getMessage().startsWith("Can't cast numbers to non-numeric")) { /* add explicit conversion step */ } else throw e; }

Prevention

When it happens

Trigger: Calling Cast.to(outputSchema) where an input field is numeric (e.g. INT64) but the corresponding output field is a non-numeric type (STRING, BOOLEAN, etc.).

Common situations: Changing a field's type from numeric to string in the target schema and expecting Cast to stringify automatically; copying schemas between systems where one side stores numbers as strings.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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