{"record":{"id":"a82ceb447f407d0b","repo":"apache/beam","slug":"ordering-of-entries-in-a-map-may-be-non-deterministic-a82ceb","errorCode":null,"errorMessage":"Ordering of entries in a Map may be non-deterministic.","messagePattern":"Ordering of entries in a Map may be non-deterministic\\.","errorType":"exception","errorClass":"NonDeterministicException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SortedMapCoder.java","lineNumber":152,"sourceCode":"   * {@inheritDoc}\n   *\n   * @return a {@link List} containing the key coder at index 0 at the and value coder at index 1.\n   */\n  @Override\n  public List<? extends Coder<?>> getCoderArguments() {\n    return Arrays.asList(keyCoder, valueCoder);\n  }\n\n  /**\n   * {@inheritDoc}\n   *\n   * @throws NonDeterministicException always. Not all maps have a deterministic encoding. For\n   *     example, {@code HashMap} comparison does not depend on element order, so two {@code\n   *     HashMap} instances may be equal but produce different encodings.\n   */\n  @Override\n  public void verifyDeterministic() throws NonDeterministicException {\n    throw new NonDeterministicException(\n        this, \"Ordering of entries in a Map may be non-deterministic.\");\n  }\n\n  @Override\n  public boolean consistentWithEquals() {\n    return keyCoder.consistentWithEquals() && valueCoder.consistentWithEquals();\n  }\n\n  @Override\n  public Object structuralValue(SortedMap<K, V> value) {\n    if (consistentWithEquals()) {\n      return value;\n    } else {\n      Map<Object, Object> ret = Maps.newHashMapWithExpectedSize(value.size());\n      for (Map.Entry<K, V> entry : value.entrySet()) {\n        ret.put(\n            keyCoder.structuralValue(entry.getKey()), valueCoder.structuralValue(entry.getValue()));\n      }","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/coders/SortedMapCoder.java#L134-L170","documentation":"SortedMapCoder.verifyDeterministic() throws NonDeterministicException by default with this message. Although a SortedMap's key order is defined, Beam conservatively refuses to mark it deterministic (the generic Map message) unless the coder is explicitly verified via a subclass override; any grouping operation using this coder fails validation.","triggerScenarios":"Using SortedMapCoder as key/element coder in GroupByKey, Combine, or calling verifyDeterministic() during pipeline validation.","commonSituations":"Grouping keyed by a TreeMap; pipeline submit-time determinism validation; tests calling verifyDeterministic on Map-like coders.","solutions":["Override verifyDeterministic in a custom coder subclass that checks keyCoder and valueCoder determinism (as Beam's own verified coders do) and use that coder","Use KV<K,V> with deterministic key/value coders instead of a Map","Encode as a sorted List of KVs with ListCoder for deterministic grouping"],"exampleFix":"// before\nCoder<SortedMap<K,V>> c = SortedMapCoder.of(kCoder, vCoder);\nc.verifyDeterministic(); // throws\n// after\npublic class VerifiedSortedMapCoder<K,V> extends SortedMapCoder<K,V> {\n  @Override public void verifyDeterministic() throws NonDeterministicException {\n    verifyDeterministic(keyCoder, \"key coder\", (m) -> m.entrySet());\n    verifyDeterministic(valueCoder, \"value coder\", (m) -> m.values());\n  }\n}","handlingStrategy":"try-catch","validationCode":"try { sortedMapCoder.verifyDeterministic(); } catch (NonDeterministicException e) { /* use KV or verified custom coder */ }","typeGuard":"null","tryCatchPattern":"try { coder.verifyDeterministic(); } catch (NonDeterministicException e) { use custom subclass that verifies key/value coders }","preventionTips":["Prefer KV<K,V> with deterministic coders for grouping","Write a verified subclass overriding verifyDeterministic when maps must be keys","Test coders with CoderProperties.deterministicInMemory"],"tags":["java","beam","coders","determinism"],"backgroundTag":"unsupported-operation","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"}