{"record":{"id":"121266be05479ab8","repo":"apache/beam","slug":"could-not-encode-elements-from-to-bytes","errorCode":null,"errorMessage":"Could not encode elements from \"{}\" to bytes: {}","messagePattern":"Could not encode elements from \"(.+?)\" to bytes: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"sdks/java/harness/src/main/java/org/apache/beam/fn/harness/debug/DataSampler.java","lineNumber":176,"sourceCode":"    BeamFnApi.SampleDataRequest sampleDataRequest = request.getSampleData();\n\n    List<String> pcollections = sampleDataRequest.getPcollectionIdsList();\n\n    // Safe to iterate as the ConcurrentHashMap will return each element at most once and will not\n    // throw ConcurrentModificationException.\n    BeamFnApi.SampleDataResponse.Builder response = BeamFnApi.SampleDataResponse.newBuilder();\n    outputSamplers.forEach(\n        (pcollectionId, outputSampler) -> {\n          if (!pcollections.isEmpty() && !pcollections.contains(pcollectionId)) {\n            return;\n          }\n\n          try {\n            response.putElementSamples(\n                pcollectionId,\n                ElementList.newBuilder().addAllElements(outputSampler.samples()).build());\n          } catch (IOException e) {\n            LOG.warn(\n                \"Could not encode elements from \\\"{}\\\" to bytes: {}\", pcollectionId, e.toString());\n          }\n        });\n\n    return BeamFnApi.InstructionResponse.newBuilder().setSampleData(response);\n  }\n}\n","sourceCodeStart":158,"sourceCodeEnd":184,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/debug/DataSampler.java#L158-L184","documentation":"DataSampler's handleDataSampleRequest collects sampled elements for a PCollection and must encode them to bytes for the response; if the coder's encoding throws IOException, this warning is logged and that PCollection's samples are omitted from the response. Data sampling continues for other PCollections.","triggerScenarios":"A registered data sampler for pcollectionId has samples whose type/ coder cannot be re-encoded when building the SampleData instruction response (coder IOException during samples() materialization).","commonSituations":"Custom coders that fail on re-encoding sampled values; objects mutated after being sampled so their coder now fails; debugging sessions with data sampling enabled on PCollections with fragile coders.","solutions":["Fix the custom coder throwing IOException so it can encode the sampled elements.","Disable data sampling (--dataSampler) if it is only used for debugging and not needed.","Check that sampled elements were not mutated after emission in your DoFn.","Capture the full stack trace (enable debug logging) to identify which coder failed."],"exampleFix":"// before\npublic class BadCoder<T> extends CustomCoder<T> {\n  public void encode(T value, OutputStream out) {\n    // forgot to handle IOException / serialized object that changed\n  }\n}\n// after\npublic class GoodCoder<T> extends CustomCoder<T> {\n  public void encode(T value, OutputStream out) throws IOException {\n    out.write(serialize(value)); // handle the actual bytes safely\n  }\n}","handlingStrategy":"validation","validationCode":"// Verify your coder round-trips sampled values before enabling data sampling\nCoder<T> coder = ...;\nByteArrayOutputStream out = new ByteArrayOutputStream();\ncoder.encode(sample, out);\nassert coder.decode(new ByteArrayInputStream(out.toByteArray())) != null;","typeGuard":null,"tryCatchPattern":"try {\n  response.putElementSamples(pcollectionId, ...);\n} catch (IOException e) {\n  LOG.warn(\"Could not encode elements: {}\", e.toString());\n}","preventionTips":["Test custom coders with CoderTester / round-trip unit tests","Never mutate elements after emitting them","Disable data sampling in production if unneeded","Log the full stack trace to find the failing coder"],"tags":["java","apache-beam","data-sampling","coder"],"backgroundTag":"coder-encode-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}