💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Change default behavior for pickle serialization to disabled, closes #949
davidmezzettiad55344f262f
diff --git a/src/python/txtai/serialize/pickle.py b/src/python/txtai/serialize/pickle.pyindex b6915afa..1091e59c 100644--- a/src/python/txtai/serialize/pickle.py+++ b/src/python/txtai/serialize/pickle.py@@ -74,7 +74,7 @@ class Pickle(Serialize):path: optional path to add to generated error messages"""- enablepickle = self.allowpickle or os.environ.get("ALLOW_PICKLE", "True") in ("True", "1")+ enablepickle = self.allowpickle or os.environ.get("ALLOW_PICKLE", "False") in ("True", "1")if not enablepickle:raise ValueError((@@ -88,11 +88,11 @@ class Pickle(Serialize):if not self.allowpickle:warnings.warn((- "Pickled index data formats are deprecated and loading will be disabled by default in the future. "- "Set the env variable `ALLOW_PICKLE=False` to disable the loading of pickled index data formats. "+ "Loading of pickled data enabled through `ALLOW_PICKLE=True` env variable. "+ "This setting should only be used with trusted and/or local data. ""Saving this index will replace pickled index data formats with the latest index formats and remove this warning."),- FutureWarning,+ RuntimeWarning,)return enablepicklediff --git a/test/python/testdatabase/testencoder.py b/test/python/testdatabase/testencoder.pyindex 57b7c074..08e1ddaa 100644--- a/test/python/testdatabase/testencoder.py+++ b/test/python/testdatabase/testencoder.py@@ -7,6 +7,8 @@ import osimport unittestimport tempfile+from unittest.mock import patch+from io import BytesIOfrom PIL import Image@@ -84,6 +86,7 @@ class TestEncoder(unittest.TestCase):self.assertTrue(result["id"].endswith("stars.jpg"))self.assertTrue(isinstance(result["object"], Image.Image))+ @patch.dict(os.environ, {"ALLOW_PICKLE": "True"})def testPickle(self):"""Test an index with pickle encoderdiff --git a/test/python/testdatabase/testrdbms.py b/test/python/testdatabase/testrdbms.pyindex 35899a84..21fbdee7 100644--- a/test/python/testdatabase/testrdbms.py+++ b/test/python/testdatabase/testrdbms.py@@ -8,6 +8,8 @@ import osimport tempfileimport unittest+from unittest.mock import patch+from txtai.embeddings import Embeddings, IndexNotFoundErrorfrom txtai.database import Embedded, RDBMS, SQLError@@ -512,6 +514,7 @@ class Common:obj = embeddings.search("select object from txtai where id = 0")[0]["object"]self.assertEqual(str(obj.getvalue(), "utf-8"), "binary data")+ @patch.dict(os.environ, {"ALLOW_PICKLE": "True"})def testPickle(self):"""Test pickle configurationdiff --git a/test/python/testembeddings.py b/test/python/testembeddings.pyindex acd9e479..e3cfa15f 100644--- a/test/python/testembeddings.py+++ b/test/python/testembeddings.py@@ -277,6 +277,7 @@ class TestEmbeddings(unittest.TestCase):# Check that ids is not in configself.assertTrue("ids" not in self.embeddings.config)+ @patch.dict(os.environ, {"ALLOW_PICKLE": "True"})def testIdsPickle(self):"""Test legacy pickle ids@@ -296,8 +297,7 @@ class TestEmbeddings(unittest.TestCase):serializer = SerializeFactory.create("pickle", allowpickle=True)serializer.save(self.embeddings.ids.ids, path)- # Reload index- with self.assertWarns(FutureWarning):+ with self.assertWarns(RuntimeWarning):self.embeddings.load(index)# Run search@@ -397,6 +397,7 @@ class TestEmbeddings(unittest.TestCase):reducer(query)self.assertFalse(np.array_equal(query, original))+ @patch.dict(os.environ, {"ALLOW_PICKLE": "True"})def testReducerLegacy(self):"""Test reducer model with legacy model formatdiff --git a/test/python/testserialize.py b/test/python/testserialize.pyindex 07a34a49..ad70bdd5 100644--- a/test/python/testserialize.py+++ b/test/python/testserialize.py@@ -35,7 +35,6 @@ class TestSerialize(unittest.TestCase):serializer = SerializeFactory.create()self.assertEqual(serializer.loadbytes(serializer.savebytes("test")), "test")- @patch.dict(os.environ, {"ALLOW_PICKLE": "False"})def testPickleDisabled(self):"""Test disabled pickle serialization@@ -43,6 +42,20 @@ class TestSerialize(unittest.TestCase):# Validate an error is raisedwith self.assertRaises(ValueError):+ serializer = SerializeFactory.create("pickle", allowpickle=True)+ data = serializer.savebytes("Test")++ serializer = SerializeFactory.create("pickle")+ serializer.loadbytes(data)++ @patch.dict(os.environ, {"ALLOW_PICKLE": "True"})+ def testPickleEnabled(self):+ """+ Test enabled pickle serialization+ """++ # Validate a warning is raised+ with self.assertWarns(RuntimeWarning):serializer = SerializeFactory.create("pickle")data = serializer.savebytes("Test")serializer.loadbytes(data)