apache/flink · error · RuntimeException
Could not create TypeInformation for type {type}; please spe
Error message
Could not create TypeInformation for type {type}; please specify the TypeInformation manually via the version of the method that explicitly accepts it as an argument. What it means
Wrapped RuntimeException thrown when TypeExtractor.getForObject(firstElement) fails while inferring the element type for fromCollection. This happens when the first element's class is too complex or generic for reflection-based extraction (e.g. raw generic containers, anonymous classes, tuples with generic fields).
Source
Thrown at flink-datastream/src/main/java/org/apache/flink/datastream/impl/ExecutionEnvironmentImpl.java:243
// Internal Methods
// -----------------------------------------------
private static <OUT> TypeInformation<OUT> extractTypeInfoFromCollection(Collection<OUT> data) {
Preconditions.checkNotNull(data, "Collection must not be null");
if (data.isEmpty()) {
throw new IllegalArgumentException("Collection must not be empty");
}
OUT first = data.iterator().next();
if (first == null) {
throw new IllegalArgumentException("Collection must not contain null elements");
}
TypeInformation<OUT> typeInfo;
try {
typeInfo = TypeExtractor.getForObject(first);
} catch (Exception e) {
throw new RuntimeException(
"Could not create TypeInformation for type "
+ first.getClass()
+ "; please specify the TypeInformation manually via the version of the "
+ "method that explicitly accepts it as an argument.",
e);
}
return typeInfo;
}
@SuppressWarnings("unchecked")
private static <OUT, T extends TypeInformation<OUT>> T getSourceTypeInfo(
org.apache.flink.api.connector.source.Source<OUT, ?, ?> source, String sourceName) {
TypeInformation<OUT> resolvedTypeInfo = null;
if (source instanceof ResultTypeQueryable) {
resolvedTypeInfo = ((ResultTypeQueryable<OUT>) source).getProducedType();
}
if (resolvedTypeInfo == null) {
try {View on GitHub (pinned to 2f3c205e92)
Solutions
- Call the fromCollection overload that takes an explicit TypeInformation (e.g. TypeInformation.of(MyClass.class) or a custom TypeInfoFactory)
- Annotate the element class with @TypeInfo(MyClassTypeInfoFactory.class) so getForObject succeeds
- Inspect the cause chain of the RuntimeException to see exactly which field/type failed extraction
Example fix
// before
env.fromCollection(results); // Result<T> too generic for extraction
// after
env.fromCollection(results, TypeInformation.of(new TypeHint<Result<String>>() {})); Defensive patterns
Strategy: fallback
Try / catch
try {
env.fromCollection(data);
} catch (RuntimeException e) {
if (e.getMessage() != null && e.getMessage().contains("Could not create TypeInformation")) {
env.fromCollection(data, TypeInformation.of(elementClass));
} else { throw e; }
} Prevention
- Default to the explicit-TypeInformation overload for custom/generic element types
- Use @TypeInfo + TypeInfoFactory on complex POJOs
When it happens
Trigger: env.fromCollection(List.of(new Tuple2<String, Long>(...)-like generic holder)) where the element's generic parameter types cannot be recovered by TypeExtractor; elements of anonymous or local classes with non-resolvable generics.
Common situations: Custom POJO wrappers around generics (e.g. Result<T>), Scala/Java interop collections, or elements whose fields use unresolvable type variables. The thrown cause (checked exceptions from TypeExtractor) is attached as the cause.
Related errors
- Type extraction is not possible on Either type as it does no
- Type extraction is not possible on Either type as it does no
- Cannot extract the type argument with index {} because the t
- The given type {} is not a parameterized type.
- Cannot convert type to class
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/1d4eb34969542d48.
Report an issue: GitHub.