diff --git a/sdks/python/apache_beam/coders/coder_impl.py b/sdks/python/apache_beam/coders/coder_impl.py index 35d67258b560..03514bb50db0 100644 --- a/sdks/python/apache_beam/coders/coder_impl.py +++ b/sdks/python/apache_beam/coders/coder_impl.py @@ -1014,7 +1014,14 @@ class VarIntCoderImpl(StreamCoderImpl): A coder for int objects.""" def encode_to_stream(self, value, out, nested): # type: (int, create_OutputStream, bool) -> None - out.write_var_int64(value) + try: + out.write_var_int64(value) + except OverflowError as e: + raise OverflowError( + f"Integer value '{value}' is out of the encodable range for " + f"VarIntCoder. This coder is limited to values that fit " + f"within a 64-bit signed integer (-(2**63) to 2**63 - 1). " + f"Original error: {e}") from e def decode_from_stream(self, in_stream, nested): # type: (create_InputStream, bool) -> int @@ -1036,7 +1043,13 @@ def decode(self, encoded): def estimate_size(self, value, nested=False): # type: (Any, bool) -> int # Note that VarInts are encoded the same way regardless of nesting. - return get_varint_size(value) + try: + return get_varint_size(value) + except OverflowError as e: + raise OverflowError( + f"Cannot estimate size for integer value '{value}'. " + f"Value is out of the range for VarIntCoder (64-bit signed integer). " + f"Original error: {e}") from e class VarInt32CoderImpl(StreamCoderImpl):