apache/flink · error · InvalidTypesException
The generic type parameters of '{}' are missing. In many cas
Error message
The generic type parameters of '{}' are missing. In many cases lambda methods don't provide enough information for automatic type extraction when Java generics are involved. An easy workaround is to use an (anonymous) class instead that implements the '{}' interface. Otherwise the type has to be specified explicitly using type information. What it means
Thrown by TypeExtractionUtils.validateLambdaType when a lambda's output type is a raw Class that has declared type parameters (getTypeParameters().length > 0) but no actual type arguments were provided. This is the classic 'type erasure in lambda' problem: the JVM erases generic information from lambda implementations, so the extractor sees a raw class (e.g. Tuple2 instead of Tuple2<String, Integer>) and cannot determine the field types.
Source
Thrown at flink-core/src/main/java/org/apache/flink/api/java/typeutils/TypeExtractionUtils.java:366
Type component = ((GenericArrayType) t).getGenericComponentType();
return Array.newInstance(getRawClass(component), 0).getClass();
}
return Object.class;
}
/**
* Checks whether the given type has the generic parameters declared in the class definition.
*
* @param t type to be validated
*/
public static void validateLambdaType(Class<?> baseClass, Type t) {
if (!(t instanceof Class)) {
return;
}
final Class<?> clazz = (Class<?>) t;
if (clazz.getTypeParameters().length > 0) {
throw new InvalidTypesException(
"The generic type parameters of '"
+ clazz.getSimpleName()
+ "' are missing. "
+ "In many cases lambda methods don't provide enough information for automatic type extraction when Java generics are involved. "
+ "An easy workaround is to use an (anonymous) class instead that implements the '"
+ baseClass.getName()
+ "' interface. "
+ "Otherwise the type has to be specified explicitly using type information.");
}
}
/**
* Will return true if the type of the given generic class type matches clazz.
*
* @param clazz The generic class to check against
* @param type The type to be checked
*/
public static boolean isGenericOfClass(Class<?> clazz, Type type) {View on GitHub (pinned to 2f3c205e92)
Solutions
- Specify the return type explicitly: ds.map(...).returns(TypeInformation.of(new TypeHint<Tuple2<String,Integer>>(){})).
- Replace the lambda with an anonymous inner class that has explicit generic type arguments.
- Implement ResultTypeQueryable on a named class and return the TypeInformation from getProducedType().
- Use .returns(Types.TUPLE(Types.STRING, Types.INT)) for concise type specification.
Example fix
// before ds.map(x -> new Tuple2<>(x, x.length())) // after ds.map(x -> new Tuple2<>(x, x.length())) .returns(Types.TUPLE(Types.STRING, Types.INT));
Defensive patterns
Strategy: fallback
Try / catch
try {
ds.map(x -> new Tuple2<>(x, x.length()));
} catch (InvalidTypesException e) {
// lambda erased generics; specify type explicitly
ds.map(x -> new Tuple2<>(x, x.length()))
.returns(Types.TUPLE(Types.STRING, Types.INT));
} Prevention
- Always chain .returns(TypeInformation.of(new TypeHint<...>(){})) after lambda map/flatMap that returns generics.
- Prefer anonymous classes with explicit type arguments for complex generic return types.
- Use Types.* helper for concise type specification (e.g. Types.TUPLE(Types.STRING, Types.INT)).
- Implement ResultTypeQueryable for reusable function classes.
When it happens
Trigger: A lambda returns a generic type without explicit type information: ds.map(x -> new Tuple2(x, x.length())) where Tuple2's generic parameters are erased. Using a method reference to a method that returns a generic type without type witness. Lambda returning a List, Map, or other generic collection without type arguments.
Common situations: Most common type-extraction failure for Flink users. Happens when using lambdas with generics like Tuple2, Tuple3, POJOs with generic fields, or collections. The Java compiler cannot always preserve enough generic metadata through lambda deserialization for Flink's reflect-based extractor.
Related errors
- Cannot extract the type argument with index {} because the t
- Type of TypeVariable '{}' in '{}' could not be determined. T
- The implementation of AbstractDeserializationSchema is using
- Could not create the type information for '{}'. The most com
- Could not create the type information for '{}'. The most com
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/b877e1c18954aa83.
Report an issue: GitHub.