apache/flink · error · RuntimeException

The underlying input format to this ReplicatingInputFormat i

Error message

The underlying input format to this ReplicatingInputFormat isn't context aware

What it means

Thrown by ReplicatingInputFormat.getRuntimeContext() when the wrapped input format is not a RichInputFormat. ReplicatingInputFormat broadcasts one input format across all parallel subtasks; it delegates getRuntimeContext to the wrapped format, but only RichInputFormat instances actually carry a RuntimeContext. A plain (non-rich) InputFormat has no context to return, so the call is rejected.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/io/ReplicatingInputFormat.java:133

    @Override
    public void close() throws IOException {
        this.replicatedIF.close();
    }

    @Override
    public void setRuntimeContext(RuntimeContext context) {
        if (this.replicatedIF instanceof RichInputFormat) {
            ((RichInputFormat) this.replicatedIF).setRuntimeContext(context);
        }
    }

    @Override
    public RuntimeContext getRuntimeContext() {
        if (this.replicatedIF instanceof RichInputFormat) {
            return ((RichInputFormat) this.replicatedIF).getRuntimeContext();
        } else {
            throw new RuntimeException(
                    "The underlying input format to this ReplicatingInputFormat isn't context aware");
        }
    }

    @Override
    public void openInputFormat() throws IOException {
        if (this.replicatedIF instanceof RichInputFormat) {
            ((RichInputFormat) this.replicatedIF).openInputFormat();
        }
    }

    @Override
    public void closeInputFormat() throws IOException {
        if (this.replicatedIF instanceof RichInputFormat) {
            ((RichInputFormat) this.replicatedIF).closeInputFormat();
        }
    }
}

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Make the wrapped InputFormat extend RichInputFormat (it then gets setRuntimeContext/getRuntimeContext for free).
  2. Avoid calling getRuntimeContext on the ReplicatingInputFormat; instead obtain the context before wrapping and pass needed values explicitly.
  3. If you control the wrapped format, register it as rich and ensure setRuntimeContext is propagated (the wrapper already does this for RichInputFormat).

Example fix

// before: wrapped format is a plain InputFormat
public class MyFormat implements InputFormat<...> { ... }
new ReplicatingInputFormat<>(new MyFormat(), ...).getRuntimeContext(); // throws
// after: extend RichInputFormat
public class MyFormat extends RichInputFormat<...> { ... }
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(replicatedIF instanceof RichInputFormat)) {
    throw new IllegalStateException(
        "Cannot use getRuntimeContext: wrapped format " + replicatedIF.getClass()
        + " is not a RichInputFormat. Extend RichInputFormat to enable context access.");
}

Type guard

static RichInputFormat requireRich(InputFormat<?, ?> f) {
    if (!(f instanceof RichInputFormat)) {
        throw new IllegalArgumentException(
            "Expected RichInputFormat, got " + f.getClass().getName());
    }
    return (RichInputFormat) f;
}

Try / catch

RuntimeContext ctx;
try {
    ctx = replicating.getRuntimeContext();
} catch (RuntimeException e) {
    if (e.getMessage().contains("context aware")) {
        // obtain context another way or fail fast with guidance
        throw new IllegalStateException("Wrapped format must extend RichInputFormat", e);
    }
    throw e;
}

Prevention

When it happens

Trigger: A ReplicatingInputFormat wraps a non-Rich InputFormat (one that does not extend RichInputFormat), and downstream code calls getRuntimeContext() on the replicating wrapper — directly or via a RichFunction/operator that requests the context.

Common situations: Wrapping a third-party or legacy InputFormat that implements InputFormat directly instead of extending RichInputFormat; using broadcast/replicate() with a custom source that was not made rich; refactor that changed the wrapped format's base class.

Related errors


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