apple/pkl · error · DecodeException

Cannot decode Function value

Error message

Cannot decode Function value

What it means

DecodeException thrown by the default doDecodeFunction() implementation, which is deliberately unsupported. Pkl binary encoding can tag a value as FUNCTION, but this decoder cannot reconstruct a function value, so any FUNCTION-tagged member aborts decoding. Subclasses may override doDecodeFunction, but the base class throws by default.

Solutions

  1. Remove function-valued members from the data being encoded (functions cannot cross the binary boundary)
  2. Override doDecodeFunction() in your AbstractPklBinaryDecoder subclass to return a placeholder or handle functions explicitly
  3. Convert the function value to a string/URI representation before encoding
  4. Use a Pkl version/decoder that supports function round-tripping if one is available

Example fix

// before
// base class: throws
// after (subclass)
@Override
protected Object doDecodeFunction() {
  return "<function>"; // or throw a domain-specific error
}
Defensive patterns

Strategy: type-guard

Validate before calling

// Before exporting a Pkl value, ensure it contains no function-valued members
// e.g. in Pkl: check properties are data (String/Int/List/Listing/Object), not (x) -> ...

Try / catch

try {
  Object v = decoder.decode(bytes);
} catch (DecodeException e) {
  if ("Cannot decode Function value".equals(e.getMessage())) {
    // re-request data without function members, or override doDecodeFunction
  } else throw e;
}

Prevention

When it happens

Trigger: Decoding a Pkl value that contains a function (e.g. an object property whose value is a function) via a decoder that does not implement doDecodeFunction — such as decoding evaluator output that includes a function-typed property.

Common situations: Externalizing module values that reference Pkl functions; passing evaluator-rendered output containing function members through the pkl-binary channel; cache tools that snapshot values which happen to include lambdas.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of apple/pkl@f3efcbfc9b (2026-09-08). Data as JSON: /api/errors/a53a1fc47001d1be. Report an issue: GitHub.

Appendix: source

Thrown at pkl-core/src/main/java/org/pkl/core/util/pklbinary/AbstractPklBinaryDecoder.java:127

  protected abstract Object doDecodeSet(CollectionDecodeIterator iter);

  protected abstract Object doDecodeDuration(double value, DurationUnit unit);

  protected abstract Object doDecodeDataSize(double value, DataSizeUnit unit);

  protected abstract Object doDecodePair(Object first, Object second);

  protected abstract Object doDecodeIntSeq(long start, long end, long step);

  protected abstract Object doDecodeRegex(Pattern pattern);

  protected abstract Object doDecodeClass(String qualifiedName, URI moduleUri);

  protected abstract Object doDecodeTypeAlias(String qualifiedName, URI moduleUri);

  protected Object doDecodeFunction() {
    throw new DecodeException("Cannot decode Function value");
  }

  protected abstract Object doDecodeBytes(byte[] bytes);

  private Object doDecode() throws IOException {
    if (!unpacker.hasNext()) {
      throw new DecodeException("Unexpected EOF");
    }

    return switch (unpacker.getNextFormat().getValueType()) {
      // primitives
      case NIL -> {
        unpacker.unpackNil();
        yield doDecodeNull();
      }
      case STRING -> unpacker.unpackString();
      case INTEGER -> unpacker.unpackLong();
      case BOOLEAN -> unpacker.unpackBoolean();

View on GitHub (pinned to f3efcbfc9b)