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
- Specify the output type explicitly using .returns(TypeInformation.of(MyOutput.class)).
- Ensure your function implements the correct Flink interface with the expected number of type parameters.
- Replace the lambda with an anonymous class that has explicit generics.
- 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
- Always specify .returns(TypeInformation.of(...)) when using custom generic function interfaces.
- Use Flink's built-in function interfaces which have well-defined generic signatures.
- Avoid complex generic hierarchies in function classes.
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
- The given type {} is not a parameterized type.
- The generic type parameters of '{}' are missing. In many cas
- Type extraction is not possible on Either type as it does no
- Type extraction is not possible on Either type as it does no
- No lambda method found.
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/09aa9699ecb4c843.
Report an issue: GitHub.