{"record":{"id":"787f7326eb38e2ff","repo":"apache/beam","slug":"failed-to-encode-values-for-multimap-user-state-id-s","errorCode":null,"errorMessage":"Failed to encode values for multimap user state id %s.","messagePattern":"Failed to encode values for multimap user state id (.+?)\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/MultimapUserState.java","lineNumber":505,"sourceCode":"        KV<K, CachingStateIterable<V>> value = persistedValues.get(entry.getKey());\n        // We don't do anything for keys that haven't been loaded since we have no knowledge whether\n        // the key is empty or not.\n        if (value != null) {\n          value.getValue().append(entry.getValue().getValue());\n        }\n      }\n    }\n  }\n\n  private ByteString encodeValues(Iterable<V> values) {\n    try {\n      ByteStringOutputStream output = new ByteStringOutputStream();\n      for (V value : values) {\n        valueCoder.encode(value, output);\n      }\n      return output.toByteString();\n    } catch (IOException e) {\n      throw new IllegalStateException(\n          String.format(\n              \"Failed to encode values for multimap user state id %s.\",\n              keysStateRequest.getStateKey().getMultimapKeysUserState().getUserStateId()),\n          e);\n    }\n  }\n\n  private StateRequest createUserStateRequest(K key) {\n    try {\n      ByteStringOutputStream output = new ByteStringOutputStream();\n      mapKeyCoder.encode(key, output);\n      StateRequest.Builder request = userStateRequest.toBuilder();\n      request.getStateKeyBuilder().getMultimapUserStateBuilder().setMapKey(output.toByteString());\n      return request.build();\n    } catch (IOException e) {\n      throw new IllegalStateException(\n          String.format(\n              \"Failed to encode key for multimap user state id %s.\",","sourceCodeStart":487,"sourceCodeEnd":523,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/state/MultimapUserState.java#L487-L523","documentation":"MultimapUserState.encodeValues serializes each pending value of a multimap user state entry with the value Coder when flushing writes to the Fn API state service. An IOException during encoding is wrapped in an IllegalStateException naming the multimap user state id. The values written are not encodable by the declared value coder.","triggerScenarios":"Calling write/add on multimap user state and later flush/startStateApiWrites where any value V fails valueCoder.encode — null values, coder/type mismatch, or a custom coder throwing on that instance.","commonSituations":"User state populated with values of a different runtime type than the declared coder; null values added to the multimap; coder changes after pipeline refactor; Kryo/Java-serialized objects that became non-serializable after code changes.","solutions":["Ensure all values written match the declared value Coder type and are non-null if the coder forbids nulls.","Fix custom Coders that throw for the values being written.","Filter or sanitize values before adding them to user state.","Verify the state id's coder spec in the pipeline graph matches what user code writes."],"exampleFix":"// before\nuserState.put(key, maybeNullValue);\n// after\nif (maybeNullValue != null) {\n  userState.put(key, maybeNullValue);\n}","handlingStrategy":"validation","validationCode":"for (V v : values) {\n  if (v == null || !valueCoder.getType().isInstance(v)) {\n    throw new IllegalArgumentException(\"Value not encodable for user state: \" + v);\n  }\n}","typeGuard":"boolean encodableValue(V v, Coder<V> coder) {\n  return v != null && coder.getType().isInstance(v);\n}","tryCatchPattern":"try {\n  userState.flush();\n} catch (IllegalStateException e) {\n  if (e.getMessage() != null && e.getMessage().startsWith(\"Failed to encode values\")) {\n    LOG.error(\"User state values unencodable: {}\", e.getMessage(), e);\n  } else { throw e; }\n}","preventionTips":["Only write values matching the declared value coder type","Exclude null values from multimap user state","Re-test coders after pipeline refactors"],"tags":["user-state","encoding","coder","beam"],"backgroundTag":"json-marshal-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"}