apple/pkl · error · PklException

Could not read annotation. Invalid object type:

Error message

Could not read annotation. Invalid object type: 

What it means

A catch-all PklException at the end of parsePObject, reached when the input object matches none of the recognized shapes (not a JsArray, or a JsObject whose "type" is not Set/Map/PObject/Pattern/DataSize/Duration/Pair, or a JsObject without a "type" string). It reports the Java class of the offending object; the comment notes it 'should never be reached' for well-formed metadata.

Source

Thrown at pkl-core/src/main/java/org/pkl/core/packages/DependencyMetadata.java:280

          }
          var unit = DurationUnit.parse(symbol);
          if (unit == null) {
            throw new PklException("Invalid Duration unit symbol: " + symbol);
          }
          if (!(value instanceof Double num)) {
            throw new FormatException("double", value.getClass());
          }
          return new Duration(num, unit);
        }
        case "Pair" -> {
          var first = parsePObject(jsObj.get("first"));
          var second = parsePObject(jsObj.get("second"));
          return new Pair<>(first, second);
        }
      }
    }
    // should never be reached
    throw new PklException("Could not read annotation. Invalid object type: " + obj.getClass());
  }

  public static Checksums parseChecksums(Object obj) throws JsonParseException {
    if (!(obj instanceof JsObject jsObj)) {
      throw new FormatException("object", obj.getClass());
    }
    var sha256 = jsObj.getString("sha256");
    return new Checksums(sha256);
  }

  public static List<String> parseAuthors(Object obj) throws JsonParseException {
    if (!(obj instanceof JsArray arr)) {
      throw new FormatException("array", obj.getClass());
    }
    var ret = new ArrayList<String>(arr.size());
    for (var elem : arr) {
      if (!(elem instanceof String string)) {
        throw new FormatException("string", elem != null ? elem.getClass() : Void.class);

View on GitHub (pinned to f3efcbfc9b)

Solutions

  1. Ensure every object in the metadata JSON has a recognized "type" field (Set, Map, PObject, Pattern, DataSize, Duration, Pair)
  2. Regenerate the metadata file using a Pkl version compatible with this library
  3. Check the class name in the message to identify the unexpected object and fix the upstream serializer

Example fix

// before
{"unit": "s", "value": 1.0}
// after
{"type": "Duration", "unit": "s", "value": 1.0}
Defensive patterns

Strategy: try-catch

Validate before calling

Set<String> TYPES = Set.of("Set","Map","PObject","Pattern","DataSize","Duration","Pair");
boolean hasKnownType(Object o) {
  return o instanceof Map<?,?> m && m.get("type") instanceof String t && TYPES.contains(t);
}

Type guard

if (!(o instanceof Map<?,?> m) || !(m.get("type") instanceof String)) return fallback(o);

Try / catch

try {
  return DependencyMetadata.parsePObject(obj);
} catch (PklException e) {
  if (e.getMessage().startsWith("Could not read annotation")) {
    throw new IOException("Metadata contains an object type unsupported by this Pkl version; regenerate metadata", e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Passing a scalar (String, Number, Boolean), a JsObject lacking a "type" key, or a JsObject with an unknown type name (e.g. {"type":"List"}) into parsePObject via obj/first/second.

Common situations: A dependency metadata file written by a newer Pkl version with a type this reader does not know, or a hand-crafted JSON structure that skips the "type" discriminator.

Related errors


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