temporalio/temporal · error
And requires at least 2 predicates, got %v
Error message
And requires at least 2 predicates, got %v
What it means
The And() constructor in common/predicates builds an AND-composed Predicate from 2+ sub-predicates. It panics when fewer than 2 predicates are passed because an AND of 0 or 1 predicates is meaningless in this library's API contract (callers should use the single predicate directly).
Source
Thrown at common/predicates/and.go:18
package predicates
import (
"fmt"
)
type (
AndImpl[T any] struct {
// TODO: see if we can somehow order arbitrary predicats and store a sorted list
Predicates []Predicate[T]
}
)
func And[T any](
predicates ...Predicate[T],
) Predicate[T] {
if len(predicates) < 2 {
panic(fmt.Sprintf("And requires at least 2 predicates, got %v", len(predicates)))
}
flattened := make([]Predicate[T], 0, len(predicates))
for _, p := range predicates {
switch p := p.(type) {
case *AndImpl[T]:
flattened = appendPredicates(flattened, p.Predicates...)
case *UniversalImpl[T]:
continue
case *EmptyImpl[T]:
return p
default:
flattened = appendPredicates(flattened, p)
}
}
switch len(flattened) {
case 0:View on GitHub (pinned to bde624efd1)
Solutions
- Pass at least 2 predicates to And
- If only one predicate exists, return it directly instead of wrapping in And
- If the input slice may be empty, return a no-op/true predicate or return an error before calling And
Example fix
// before
pred := predicates.And(items...)
// after
var pred predicates.Predicate[int]
switch len(items) {
case 1:
pred = items[0]
default:
pred = predicates.And(items...) // len >= 2 guaranteed by caller
} Defensive patterns
Strategy: validation
Validate before calling
if len(preds) < 2 {
// handle: use preds[0] directly or a defined always-true predicate
return preds[0]
}
pred := predicates.And(preds...) Try / catch
func safeAnd[T any](ps ...predicates.Predicate[T]) (p predicates.Predicate[T]) {
defer func() {
if recover() != nil { p = fallbackPredicate[T]() }
}()
return predicates.And(ps...)
} Prevention
- Check len(predicates) before calling And
- For single-predicate cases, use the predicate directly
- When building from slices, handle the empty case explicitly
When it happens
Trigger: Calling And() or And(p) with zero or one Predicate arguments. e.g. And[int]() or And(isEven).
Common situations: Building predicates programmatically from a dynamically-sized slice that ends up empty or has a single element after filtering; copying an Or(...) call and accidentally leaving one argument.
Related errors
- Or requires at least 2 predicates, got %v
- expect at least one rate limiter
- expect at least one rate limiter
- Current cluster name is empty
- Version increment <= 0 or > 2147483647
AI-assisted analysis of temporalio/temporal@bde624efd1 (2026-09-01).
Data as JSON: /api/errors/cd961580d5c4f776.
Report an issue: GitHub.