{"record":{"id":"5182fa76da429b94","repo":"apache/beam","slug":"could-not-encode-the-default-value-s-with-the-provided-coder","errorCode":null,"errorMessage":"Could not encode the default value %s with the provided coder %s","messagePattern":"Could not encode the default value (.+?) with the provided coder (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/View.java","lineNumber":530,"sourceCode":"  }\n\n  private static class SingletonCombineFn<T> extends Combine.BinaryCombineFn<T> {\n    private final boolean hasDefault;\n    private final @Nullable Coder<T> valueCoder;\n    private final byte @Nullable [] defaultValue;\n\n    private SingletonCombineFn(boolean hasDefault, Coder<T> coder, T defaultValue) {\n      this.hasDefault = hasDefault;\n      if (hasDefault) {\n        if (defaultValue == null) {\n          this.defaultValue = null;\n          this.valueCoder = coder;\n        } else {\n          this.valueCoder = coder;\n          try {\n            this.defaultValue = CoderUtils.encodeToByteArray(coder, defaultValue);\n          } catch (CoderException e) {\n            throw new IllegalArgumentException(\n                String.format(\n                    \"Could not encode the default value %s with the provided coder %s\",\n                    defaultValue, coder));\n          }\n        }\n      } else {\n        this.valueCoder = null;\n        this.defaultValue = null;\n      }\n    }\n\n    @Override\n    public T apply(T left, T right) {\n      throw new IllegalArgumentException(\n          \"PCollection with more than one element \"\n              + \"accessed as a singleton view. Consider using Combine.globally().asSingleton() to \"\n              + \"combine the PCollection into a single value\");\n    }","sourceCodeStart":512,"sourceCodeEnd":548,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/transforms/View.java#L512-L548","documentation":"When View.asSingleton(defaultValue) supplies a default, SingletonCombineFn eagerly encodes the default with the input PCollection's coder via CoderUtils.encodeToByteArray to store it safely. If the coder cannot encode the default (CoderException), Beam throws IllegalArgumentException stating the value and coder that failed. This catches coder/value mismatches at pipeline construction.","triggerScenarios":"Calling View.asSingleton(default) where the PCollection's Coder cannot encode `default` — e.g. the coder is for a different type than the default value, the default is null with a non-nullable coder, or a custom coder's encode() throws for that value.","commonSituations":"Mismatches between coder inferred for the PCollection and the default value type; nulls passed with a coder that rejects null; custom Coder implementations that throw on encode for edge-case values; pipeline refactor changed the element type but the default remained old-typed.","solutions":["Ensure the default value's type matches the PCollection's element type and coder","Test the coder with CoderUtils.encodeToByteArray(coder, defaultValue) before use","Use a coder that supports the default (e.g. nullable coder like NullableCoder.of) or fix a custom coder's encode method"],"exampleFix":"// before\nPCollectionView<String> view = input.apply(View.asSingleton(null)); // coder cannot encode null\n// after\nPCollectionView<String> view = input.setCoder(NullableCoder.of(StringUtf8Coder.of()))\n    .apply(View.asSingleton(null));","handlingStrategy":"validation","validationCode":"try { CoderUtils.encodeToByteArray(coder, defaultValue); } catch (CoderException e) { /* fix coder/default mismatch before View.asSingleton(default) */ }","typeGuard":null,"tryCatchPattern":"try { view = input.apply(View.asSingleton(defaultValue)); } catch (IllegalArgumentException e) { throw new IllegalStateException(\"Default value not encodable by PCollection coder\", e); }","preventionTips":["Verify default value type matches the PCollection element type exactly","Use NullableCoder when the default is null","Round-trip test custom coders (encode then decode) in unit tests"],"tags":["java","apache-beam","coder","pipeline-construction"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}