diff --git a/lib/python/labcomm2014/LabComm.py b/lib/python/labcomm2014/LabComm.py
index 2ac3524a9326abfbc489e063547900cdf7cb98fa..fd4b1fe1bb9143041dc3f04d97cf9950f2e3c736 100644
--- a/lib/python/labcomm2014/LabComm.py
+++ b/lib/python/labcomm2014/LabComm.py
@@ -203,7 +203,7 @@ class length_encoder:
     def __init__(self, encoder):
         self.encoder = encoder
         self.version = encoder.version
-        self.data = ""
+        self.data = b''
 
     def write(self, data):
         self.data += data
@@ -509,7 +509,12 @@ class array(type_decl):
         encoder.encode_type_number(self.decl)
 
     def min_max_shape(self, l, depth, shape):
-        if isinstance(l, types.StringTypes):
+        def isstring(obj):
+            try:
+                return isinstance(obj, basestring)
+            except NameError:
+                return isinstance(obj, str)
+        if isstring(l):
             return shape
         try:
             length = len(l)
@@ -801,7 +806,12 @@ class Encoder(Codec):
         if not isinstance(ref, type_decl):
             # Trying to register a sample class
             ref = ref.signature
-        decl = sample_ref(name=ref.name, decl=ref.decl, sample=ref)
+        if not isinstance(ref, sample_ref):
+            sample = ref
+        else:
+            # Adding a sample_ref, keep sample type
+            sample = ref.sample
+        decl = sample_ref(name=ref.name, decl=ref.decl, sample=sample)
         if index == 0:
             self.writer.mark_begin(ref, None)
             if super(Encoder, self).add_ref(decl, index):
@@ -868,8 +878,8 @@ class Encoder(Codec):
 
     def encode_string(self, v):
         s = v.encode("utf8")
-	self.encode_packed32(len(s));
-	self.pack("%ds" % len(s),s)
+        self.encode_packed32(len(s));
+        self.pack("%ds" % len(s),s)
 
 class Decoder(Codec):
     def __init__(self, reader, version=DEFAULT_VERSION):
@@ -883,7 +893,7 @@ class Decoder(Codec):
 
     def unpack(self, format):
         size = packer.calcsize(format)
-        data = ""
+        data = b''
         while len(data) < size:
             data += self.reader.read(size - len(data))
         result = packer.unpack(format, data)
diff --git a/lib/python/labcomm2014/__init__.py b/lib/python/labcomm2014/__init__.py
index 2e36c218d8fb8b8df01596bf9893ac5a3c0f68df..905cd318b5420ff37c7b2ba82a5421a42677c2c2 100644
--- a/lib/python/labcomm2014/__init__.py
+++ b/lib/python/labcomm2014/__init__.py
@@ -1,9 +1,9 @@
 __all__ = [ 'LabComm' ]
 
-import LabComm
+from . import LabComm
 
-from StreamReader import StreamReader
-from StreamWriter import StreamWriter
+from .StreamReader import StreamReader
+from .StreamWriter import StreamWriter
 
 Decoder = LabComm.Decoder
 Encoder = LabComm.Encoder
diff --git a/test/Makefile b/test/Makefile
index 3694ac5ec68803fb4930d797cda5437251d2d3e6..1d9a7c97c0f4fd48f70261f4fabf092eaefcc495 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -26,6 +26,8 @@ test_%: gen/%/signatures.py \
 		./test_encoder_decoder.py \
 		--signatures=gen/$*/signatures.py \
 		--test tee gen/$*/testdata \
+	        --test python2 python_generic_relay.py /dev/stdin /dev/stdout \
+	        --test python3 python_generic_relay.py /dev/stdin /dev/stdout \
 		--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
 		       gen/$*/c_relay /dev/stdin /dev/stdout \
 		--test mono gen/$*/cs_relay.exe /dev/stdin /dev/stdout \
@@ -42,6 +44,8 @@ test_renaming_%: gen/%/signatures.py \
 	    ./test_renaming_encoder_decoder.py \
 		--signatures=gen/$*/signatures.py \
 		--test tee gen/$*/testdata.renamed \
+	        --test python2 python_generic_relay.py /dev/stdin /dev/stdout \
+	        --test python3 python_generic_relay.py /dev/stdin /dev/stdout \
 		--test $(shell echo $(VALGRIND) | sed -e 's/[-][-]/\\\\--/g') \
 		       gen/$*/c_renaming_relay /dev/stdin /dev/stdout \
 		--test mono gen/$*/cs_renaming_relay.exe \
diff --git a/test/python_generic_relay.py b/test/python_generic_relay.py
new file mode 100644
index 0000000000000000000000000000000000000000..453b9cfe6c677915db141e7d6966abaadd92a810
--- /dev/null
+++ b/test/python_generic_relay.py
@@ -0,0 +1,21 @@
+from __future__ import print_function
+import sys
+import labcomm2014
+
+if __name__ == '__main__':
+    reader = labcomm2014.StreamReader(open(sys.argv[1], 'rb'))
+    writer = labcomm2014.StreamWriter(open(sys.argv[2], 'wb'))
+    decoder = labcomm2014.Decoder(reader)
+    encoder = labcomm2014.Encoder(writer)
+    while True:
+        try:
+            value, decl = decoder.decode()
+        except EOFError:
+            break
+        if value == None:
+            if isinstance(decl, labcomm2014.sample_ref):
+                encoder.add_ref(decl)
+            else:
+                encoder.add_decl(decl)
+        else:
+            encoder.encode(value, decl)