{"record":{"id":"2d5b54febb788b55","repo":"apache/beam","slug":"expected-weight-to-be-0-for-s-but-received-d","errorCode":null,"errorMessage":"Expected weight to be > 0 for %s but received %d","messagePattern":"Expected weight to be > 0 for (.+?) but received (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/runners/worker/statecache.py","lineNumber":62,"sourceCode":"    # Do not measure lambdas as they typically share lots of state\n    types.FunctionType,\n    types.LambdaType,\n    # Do not measure weak references as they will be deleted and not counted\n    *weakref.ProxyTypes,\n    weakref.ReferenceType)\n\n\nclass WeightedValue(object):\n  \"\"\"Value type that stores corresponding weight.\n\n  :arg value The value to be stored.\n  :arg weight The associated weight of the value. If unspecified, the objects\n  size will be used.\n  \"\"\"\n  def __init__(self, value: Any, weight: int) -> None:\n    self._value = value\n    if weight <= 0:\n      raise ValueError(\n          'Expected weight to be > 0 for %s but received %d' % (value, weight))\n    self._weight = weight\n\n  def weight(self) -> int:\n    return self._weight\n\n  def value(self) -> Any:\n    return self._value\n\n\nclass CacheAware(object):\n  \"\"\"Allows cache users to override what objects are measured.\"\"\"\n  def __init__(self) -> None:\n    pass\n\n  def get_referents_for_cache(self) -> list[Any]:\n    \"\"\"Returns the list of objects accounted during cache measurement.\"\"\"\n    raise NotImplementedError()","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/runners/worker/statecache.py#L44-L80","documentation":"StateCacheWeightedValue wraps a cache value together with a weight (its effective cost in the cache). Because weights drive cache eviction arithmetic, a weight of zero or negative would corrupt the cache's size accounting, so the constructor raises ValueError for any weight <= 0.","triggerScenarios":"Calling StateCacheWeightedValue(value, weight) with weight <= 0, e.g. passing 0, a negative size, or a size-computation function that returned 0 or a negative number for an empty/zero-sized object.","commonSituations":"Custom weight functions (object size estimators) returning 0 for empty payloads or returning -1 on error; hardcoding weight=0 to 'disable' caching; integer overflow in size computations producing negative values.","solutions":["Ensure the weight passed is a positive integer; clamp with max(1, computed_weight)","Fix the size-estimation function so empty values still report a minimum positive weight","Remove placeholder weight=0 calls and use the object's real size"],"exampleFix":"// before\nentry = StateCacheWeightedValue(value, len(serialized) - 1)  # may be <= 0\n// after\nweight = max(1, len(serialized))\nentry = StateCacheWeightedValue(value, weight)","handlingStrategy":"validation","validationCode":"def make_cache_value(value, weight_fn):\n    w = weight_fn(value)\n    if not isinstance(w, int) or w <= 0:\n        w = 1  # clamp before constructing\n    return StateCacheWeightedValue(value, w)","typeGuard":"def valid_weight(w) -> bool:\n    return isinstance(w, int) and w > 0","tryCatchPattern":"try:\n    entry = StateCacheWeightedValue(value, computed_weight)\nexcept ValueError as e:\n    if 'Expected weight to be > 0' in str(e):\n        entry = StateCacheWeightedValue(value, 1)\n    else:\n        raise","preventionTips":["Always clamp computed weights with max(1, weight)","Unit-test size-estimation functions against empty and edge-case values","Never pass 0 or negative sentinel weights to cache entries"],"tags":["python","apache-beam","state-cache","cache","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}