apache/flink · error · InvalidTypesException

Cannot extract the type argument with index {} because the t

Error message

Cannot extract the type argument with index {} because the type has only {} type arguments.

What it means

Thrown by TypeExtractionUtils.extractTypeArgument when the requested type argument index is negative or exceeds the number of actual type arguments on a ParameterizedType. For example, requesting index 2 from a type with only 1 type argument (like List<String> which has 1 type argument). This is an internal extraction failure indicating the extraction logic's assumption about the type parameter count is wrong.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractionUtils.java:212

    }

    /**
     * This method extracts the n-th type argument from the given type. An InvalidTypesException is
     * thrown if the type does not have any type arguments or if the index exceeds the number of
     * type arguments.
     *
     * @param t Type to extract the type arguments from
     * @param index Index of the type argument to extract
     * @return The extracted type argument
     * @throws InvalidTypesException if the given type does not have any type arguments or if the
     *     index exceeds the number of type arguments.
     */
    public static Type extractTypeArgument(Type t, int index) throws InvalidTypesException {
        if (t instanceof ParameterizedType) {
            Type[] actualTypeArguments = ((ParameterizedType) t).getActualTypeArguments();

            if (index < 0 || index >= actualTypeArguments.length) {
                throw new InvalidTypesException(
                        "Cannot extract the type argument with index "
                                + index
                                + " because the type has only "
                                + actualTypeArguments.length
                                + " type arguments.");
            } else {
                return actualTypeArguments[index];
            }
        } else {
            throw new InvalidTypesException(
                    "The given type " + t + " is not a parameterized type.");
        }
    }

    /**
     * Extracts a Single Abstract Method (SAM) as defined in Java Specification (4.3.2. The Class
     * Object, 9.8 Functional Interfaces, 9.4.3 Interface Method Body) from given class.
     *

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Specify the output type explicitly using .returns(TypeInformation.of(MyOutput.class)).
  2. Ensure your function implements the correct Flink interface with the expected number of type parameters.
  3. Replace the lambda with an anonymous class that has explicit generics.
  4. If using a custom interface, verify its generic signature matches what Flink's extractor expects.

Example fix

// before
ds.map(new MyRichMapFunction<String, Integer>() {
    public Integer map(String s) { return Integer.parseInt(s); }
})
// after
ds.map(new MapFunction<String, Integer>() {
    public Integer map(String s) { return Integer.parseInt(s); }
}).returns(TypeInformation.of(Integer.class));
Defensive patterns

Strategy: fallback

Try / catch

try {
    ds.map(fn);
} catch (InvalidTypesException e) {
    if (e.getMessage().contains("Cannot extract the type argument")) {
        ds.map(fn).returns(TypeInformation.of(OUT.class));
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: The type extraction logic computes an incorrect type argument index based on the base interface's generics. A function implements a generic interface with fewer type parameters than expected. Lambda type information is partially erased, causing the ParameterizedType to report fewer actualTypeArguments than the code expects.

Common situations: Custom function interfaces with different generic arity than Flink's built-in ones. Using a MapFunction or similar with unexpected generic bounds. Type information computed from a complex generic hierarchy where bridge methods or type erasure reduce the visible type argument count.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/09aa9699ecb4c843. Report an issue: GitHub.