How Do You Resolve This Error When You Pickle a Langchain Vectorstoreindexcreator Object?

I want to pickle/save my VectorStore index in LangChain. Below is my code:

import pickle
from langchain.document_loaders import PyPDFLoader
from langchain.indexes import VectorstoreIndexCreator

# Load the PDF file
pdf_path = "Los Angeles County, CA Code of Ordinances.pdf"
loader = PyPDFLoader(pdf_path)

# Create the index
index = VectorstoreIndexCreator().from_loaders([loader])

# Serialize and save the loader and index objects
with open("data.pickle", "wb") as f:
    pickle.dump((loader, index), f)

# To load the objects from the file:
with open("data.pickle", "rb") as f:
    loader, index = pickle.load(f)

but when I run it I get this error:

~/LangChain-exploredoc-explore$ python pickle_file.py
Using embedded DuckDB without persistence: data will be transient
Traceback (most recent call last):
  File "/home/runner/LangChain-exploredoc-explore/pickle_file.py", line 14, in <module>
    pickle.dump((index), f)
TypeError: cannot pickle 'duckdb.DuckDBPyConnection' object
Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article