xai-org/x-algorithm · error · SemanticCheckFailure

Count expects input of String, Map or Collection types but %

Error message

Count expects input of String, Map or Collection types but %s received

What it means

Count() accepts a first argument whose static typeBase is Object, String, Collection, or Map. Any other type (Long, Boolean, StructTuple, Pair, etc.) fails semantic checking with this message. Count measures length/size of strings, collections and maps, so scalars are rejected.

Source

Thrown at botmaker/src/java/com/twitter/botmaker/function/collection/Count.java:42

    examples = {
        "Count(ToSet(List(1, 2, 3)))"
    }
)
public class Count extends FunctionNode1<Runtime, Object> {
  private static final Signature SIGNATURE = new Signature(
      ImmutableList.of(Type.OBJECT),
      Type.LONG
  );

  public Count(String exprText, ImmutableList<ASTNode> children) throws SemanticCheckFailure {
    super(exprText, children);

    Class<?> typeBase = children.get(0).getReturnType().typeBase;
    if (!Object.class.equals(typeBase)
        && !String.class.equals(typeBase)
        && !Collection.class.isAssignableFrom(typeBase)
        && !Map.class.isAssignableFrom(typeBase)) {
      throw new SemanticCheckFailure(String.format(
          "Count expects input of String, Map or Collection types but %s received",
          typeBase.getName()));
    }

  }

  @Override
  public Signature getSignature() {
    return SIGNATURE;
  }

  @Override
  protected CacheLevel getCacheLevel() {
    return CacheLevel.Global;
  }

  @Override
  @SuppressWarnings("unchecked")

View on GitHub (pinned to 24c60942c5)

Solutions

  1. Pass a String, Collection, or Map expression to Count
  2. If the value may be untyped, ensure its type is Object so the generic path applies
  3. Use the scalar directly instead of Count for non-sized values

Example fix

// before
Count(Count(items))
// after
Count(items)
Defensive patterns

Strategy: type-guard

Validate before calling

// rule-language: only count sized values
If(IsString(x), Count(x), If(IsCollection(x), Count(x), 1))

Type guard

// host code
Class<?> base = children.get(0).getReturnType().typeBase;
boolean ok = Object.class.equals(base) || String.class.equals(base)
    || Collection.class.isAssignableFrom(base) || Map.class.isAssignableFrom(base);

Prevention

When it happens

Trigger: Calling Count(42), Count(true), Count(pair), or chaining Count onto a function that returns a scalar.

Common situations: Nesting Count(Count(x)), assuming Count is an identity/counting helper for scalars, or a pipeline refactor that changed the input type.

Related errors


AI-assisted analysis of xai-org/x-algorithm@24c60942c5 (2026-08-28). Data as JSON: /api/errors/9148bfde6ce97e15. Report an issue: GitHub.