apache/flink · error · TypeExtractionException

Could not extract lambda method out of function: {} - {}

Error message

Could not extract lambda method out of function: {} - {}

What it means

A catch-all TypeExtractionException wrapping any unexpected exception (ClassNotFoundException, IllegalAccessException, InvocationTargetException, etc.) that occurs during lambda serialization or method lookup in getLambdaExecutable. The original exception is chained as the cause. The message includes the wrapped exception's simple class name and message.

Source

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

                for (Constructor<?> constructor : constructors) {
                    if (getConstructorDescriptor(constructor).equals(methodSig)) {
                        return new LambdaExecutable(constructor);
                    }
                }
            }
            // find method
            else {
                List<Method> methods = getAllDeclaredMethods(implClass);
                for (Method method : methods) {
                    if (method.getName().equals(methodName)
                            && getMethodDescriptor(method).equals(methodSig)) {
                        return new LambdaExecutable(method);
                    }
                }
            }
            throw new TypeExtractionException("No lambda method found.");
        } catch (Exception e) {
            throw new TypeExtractionException(
                    "Could not extract lambda method out of function: "
                            + e.getClass().getSimpleName()
                            + " - "
                            + e.getMessage(),
                    e);
        }
    }

    /**
     * Extracts type from given index from lambda. It supports nested types.
     *
     * @param baseClass SAM function that the lambda implements
     * @param exec lambda function to extract the type from
     * @param lambdaTypeArgumentIndices position of type to extract in type hierarchy
     * @param paramLen count of total parameters of the lambda (including closure parameters)
     * @param baseParametersLen count of lambda interface parameters (without closure parameters)
     * @return extracted type
     */

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Set the thread context classloader to the user-code classloader before submitting the job.
  2. Replace the lambda with an anonymous class to bypass reflective lambda extraction entirely.
  3. If on Java 9+, ensure the Flink runtime has --add-opens java.base/java.lang.invoke=ALL-UNNAMED.
  4. Explicitly specify the output type using .returns(TypeInformation.of(...)).

Example fix

// before
ds.map(x -> transform(x))
// after
ds.map(x -> transform(x)).returns(TypeInformation.of(MyType.class));
Defensive patterns

Strategy: try-catch

Try / catch

try {
    TypeInformation<OUT> ti = TypeExtractor.getMapReturnTypes(fn, inType);
} catch (InvalidTypesException e) {
    logger.warn("Lambda extraction failed: " + e.getCause().getMessage());
    // fall back to explicit type
    ti = TypeInformation.of(OUT.class);
}

Prevention

When it happens

Trigger: ClassNotFoundException when the lambda's implementing class cannot be loaded by the thread context classloader. SecurityException when setAccessible(true) on writeReplace is denied. ReflectiveOperationException when the SerializedLambda structure is unexpected.

Common situations: Running Flink in a restrictive security manager environment. Deploying with a custom classloader that does not expose user-code classes to the context classloader. Running on a JVM that restricts reflective access (Java 9+ module system without --add-opens).

Related errors


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