{"record":{"id":"a5781bb1786a212d","repo":"apache/beam","slug":"value-too-large-negative","errorCode":null,"errorMessage":"Value too large (negative).","messagePattern":"Value too large \\(negative\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/coders/slow_stream.py","lineNumber":50,"sourceCode":"    self.data: list[bytes] = []\n    self.byte_count = 0\n\n  def write(self, b: bytes, nested: bool = False) -> None:\n    assert isinstance(b, bytes)\n    if nested:\n      self.write_var_int64(len(b))\n    self.data.append(b)\n    self.byte_count += len(b)\n\n  def write_byte(self, val):\n    self.data.append(chr(val).encode('latin-1'))\n    self.byte_count += 1\n\n  def write_var_int64(self, v: int) -> None:\n    if v < 0:\n      v += 1 << 64\n      if v <= 0:\n        raise ValueError('Value too large (negative).')\n    while True:\n      bits = v & 0x7F\n      v >>= 7\n      if v:\n        bits |= 0x80\n      self.write_byte(bits)\n      if not v:\n        break\n\n  def write_var_int32(self, v: int) -> None:\n    self.write_var_int64(int(v) & 0xFFFFFFFF)\n\n  def write_bigendian_int64(self, v):\n    self.write(struct.pack('>q', v))\n\n  def write_bigendian_uint64(self, v):\n    self.write(struct.pack('>Q', v))\n","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/coders/slow_stream.py#L32-L68","documentation":"OutputStream.write_var_int64 encodes a 64-bit unsigned varint; negative Python ints are first offset by 2**64. If adding 2**64 still yields a non-positive value (i.e. v <= -(2**64)), the value cannot fit in 64 bits and a ValueError 'Value too large (negative).' is raised.","triggerScenarios":"Writing a varint value <= -(2**64) (out of the int64/uint64 varint range) to a slow_stream OutputStream via write_var_int64 (also reached through write_var_int32/write/ByteCountingOutputStream).","commonSituations":"Corrupted or unvalidated numeric values flowing into custom coders/streams; arithmetic bugs producing extreme negative numbers before serialization; manually implementing a coder that writes raw negative values.","solutions":["Validate the value is within int64 range (-(2**63) <= v <= 2**63-1) before calling write_var_int64","If the value should be signed, encode a zig-zag or explicit sign marker rather than raw negatives","Fix upstream computation producing values beyond 64-bit range"],"exampleFix":"// before\nstream.write_var_int64(delta)  # delta may be < -(2**63)\n// after\nassert -(1 << 63) <= delta < (1 << 63), 'value out of int64 range'\nstream.write_var_int64(delta)","handlingStrategy":"validation","validationCode":"if not (-(1 << 64) < v <= (1 << 64) - 1):\n    raise ValueError(f'value {v} cannot be written as a 64-bit varint')","typeGuard":"def in_uint64_varint_range(v: int) -> bool:\n    return -(1 << 64) < v <= (1 << 64) - 1","tryCatchPattern":"try:\n    stream.write_var_int64(v)\nexcept ValueError as e:\n    log.error('varint out of range: %s', v)\n    raise","preventionTips":["Clamp or validate numeric values before serialization","Use signed encodings (zig-zag) for values that can be very negative","Unit-test custom coders with boundary values -2**64+1 .. 2**64-1"],"tags":["python","serialization","integer-overflow","apache-beam"],"backgroundTag":"value-out-of-range","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}