{"record":{"id":"9911747a73d80e48","repo":"apache/beam","slug":"unable-to-encode-element-value-with-coder-this","errorCode":null,"errorMessage":"Unable to encode element '\" + value + \"' with coder '\" + this + \"'.","messagePattern":"Unable to encode element '\" \\+ value \\+ \"' with coder '\" \\+ this \\+ \"'\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java","lineNumber":268,"sourceCode":"   *       {@code equals()} method, even if the input value is {@code null}.\n   * </ul>\n   *\n   * <p>See also {@link #consistentWithEquals()}.\n   *\n   * <p>By default, if this coder is {@link #consistentWithEquals()}, and the value is not null,\n   * returns the provided object. Otherwise, encodes the value into a {@code byte[]}, and returns an\n   * object that performs array equality on the encoded bytes.\n   */\n  public Object structuralValue(T value) {\n    if (value != null && consistentWithEquals()) {\n      return value;\n    } else {\n      try {\n        ByteArrayOutputStream os = new ByteArrayOutputStream();\n        encode(value, os, Context.OUTER);\n        return new StructuralByteArray(os.toByteArray());\n      } catch (Exception exn) {\n        throw new IllegalArgumentException(\n            \"Unable to encode element '\" + value + \"' with coder '\" + this + \"'.\", exn);\n      }\n    }\n  }\n\n  /**\n   * Returns whether {@link #registerByteSizeObserver} cheap enough to call for every element, that\n   * is, if this {@code Coder} can calculate the byte size of the element to be coded in roughly\n   * constant time (or lazily).\n   *\n   * <p>Not intended to be called by user code, but instead by {@link PipelineRunner}\n   * implementations.\n   *\n   * <p>By default, returns false. The default {@link #registerByteSizeObserver} implementation\n   * invokes {@link #getEncodedElementByteSize} which requires re-encoding an element unless it is\n   * overridden. This is considered expensive.\n   */\n  public boolean isRegisterByteSizeObserverCheap(T value) {","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/Coder.java#L250-L286","documentation":"Coder.structuralValue falls back to encoding the value to bytes when the coder does not override structuralValue; if that encode throws, it rethrows IllegalArgumentException naming the value and coder. The real cause is the nested exception, typically a null or unsupported value for the coder.","triggerScenarios":"Calling structuralValue(value) (directly or via keyedValues / side-input helpers) on a value the coder cannot encode — commonly null elements for non-nullable coders like ByteCoder or ByteArrayCoder.","commonSituations":"Null values in GBK keys, side inputs containing nulls, coders that throw on encode for special values, equality/hashing machinery encountering bad elements.","solutions":["Inspect getCause() to find the real encode failure (often CoderException for null)","Filter/normalize null or unsupported elements before they reach keyed or side-input paths","Use a coder that supports the values (e.g. NullableCoder) or override structuralValue in a custom coder","Fix upstream data so all elements are encodable"],"exampleFix":"// before\nKV<Byte, String> kv = KV.of(nullableByte, v); // structuralValue throws IAE\n// after\nKV<Byte, String> kv = KV.of(nullableByte == null ? (byte) 0 : nullableByte, v);","handlingStrategy":"try-catch","validationCode":"// pre-encode to verify encodability\nByteArrayOutputStream test = new ByteArrayOutputStream();\ncoder.encode(value, test, Coder.Context.OUTER);","typeGuard":"static <T> boolean encodable(Coder<T> c, T v) { try { c.encode(v, new ByteArrayOutputStream(), Coder.Context.OUTER); return true; } catch (Exception e) { return false; } }","tryCatchPattern":"try { Object sv = coder.structuralValue(v); } catch (IllegalArgumentException e) { LOG.error(\"value not encodable: \" + e.getCause(), e); }","preventionTips":["Never emit nulls into coded PCollections without NullableCoder","Override structuralValue in custom coders to avoid the encode fallback"],"tags":["java","beam","coders","encoding"],"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"}