apache/beam · error · CannotProvideCoderException

Unable to provide coder for %s, this factory can only provid

Error message

Unable to provide coder for %s, this factory can only provide coders for %s

What it means

CoderProviders.coderFor throws CannotProvideCoderException when the requested TypeDescriptor's raw type does not match the raw type this factory was built for. The factory only supplies coders for its single registered type, so a mismatch means it cannot serve the request.

Source

Thrown at sdks/java/core/src/main/java/org/apache/beam/sdk/coders/CoderProviders.java:65

  }

  /** Creates a {@link CoderProvider} that always returns the given coder for the specified type. */
  public static CoderProvider forCoder(TypeDescriptor<?> type, Coder<?> coder) {
    return new CoderProviderForCoder(type, coder);
  }

  /**
   * See {@link #fromStaticMethods} for a detailed description of the characteristics of this {@link
   * CoderProvider}.
   */
  private static class CoderProviderFromStaticMethods extends CoderProvider {

    @SuppressFBWarnings("DCN_NULLPOINTER_EXCEPTION") // TODO(#35312)
    @Override
    public <T> Coder<T> coderFor(TypeDescriptor<T> type, List<? extends Coder<?>> componentCoders)
        throws CannotProvideCoderException {
      if (!this.rawType.equals(type.getRawType())) {
        throw new CannotProvideCoderException(
            String.format(
                "Unable to provide coder for %s, this factory can only provide coders for %s",
                type, this.rawType));
      }
      try {
        return (Coder<T>)
            Preconditions.checkStateNotNull(
                factoryMethod.invoke(this.rawType /* ignored */, componentCoders.toArray()));
      } catch (IllegalAccessException
          | IllegalArgumentException
          | InvocationTargetException
          | NullPointerException
          | ExceptionInInitializerError exn) {
        throw new IllegalStateException(
            "error when invoking Coder factory method " + factoryMethod, exn);
      }
    }

View on GitHub (pinned to 12126d8942)

Solutions

  1. Register the correct CoderProvider for the actual type (e.g. CoderProviders.fromStaticMethods(MyType.class, "getCoder"))
  2. Ensure the factory's rawType matches the requested type exactly
  3. Add a CoderRegistry fallback chain so other providers can handle other types
  4. Provide the coder explicitly with setCoder instead of relying on provider lookup

Example fix

// before
CoderProviders.fromStaticMethods(String.class, "getCoder") // asked for Integer
// after
CoderProviders.fromStaticMethods(Integer.class, "getCoder");
Defensive patterns

Strategy: validation

Validate before calling

if (!factorySupports(rawType)) { throw new IllegalArgumentException("no CoderProvider registered for " + rawType); }

Type guard

static <T> boolean providerSupports(CoderProviderFactory f, TypeDescriptor<T> t) { return f.getRawType().equals(t.getRawType()); }

Try / catch

try { Coder<T> c = provider.coderFor(type, comps); } catch (CannotProvideCoderException e) { /* fall through to next provider or registry.defaultCoder(type) */ }

Prevention

When it happens

Trigger: Calling coderFor with a TypeDescriptor whose raw type differs from the factory's rawType — e.g. asking a factory registered for String to provide a coder for Integer, or a subclass/generic variant that changes the raw type.

Common situations: Registering a CoderProvider for one class but expecting it to cover subclasses or generics, coder auto-detection through a provider list with the wrong type wired in, refactors renaming or generifying the coded class.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of apache/beam@12126d8942 (2026-09-13). Data as JSON: /api/errors/1d9777054e6b2626. Report an issue: GitHub.