Model Serialization
Author: Mark
Peer Review : Jaemin Hong, Dooil Kwak
Proofread : Two-Jay
This is a part of LangChain Open Tutorial
Overview
Serialization is the process of converting an object into a format that can be easily stored, shared, or transmitted, and later reconstructed. In the LangChain framework, classes implement standard methods for serialization, providing several advantages:
Separation of Secrets: Sensitive information, such as API keys, is separated from other parameters and can be securely reloaded into the object during deserialization.
Version Compatibility: Deserialization remains compatible across different package versions, ensuring that objects serialized with one version of LangChain can be properly deserialized with another.
All LangChain objects inheriting from Serializable are JSON-serializable, including messages, document objects (e.g., those returned from retrievers), and most Runnables such as chat models, retrievers, and chains implemented with the LangChain Expression Language.
Saving and Loading LangChain Objects
To effectively manage LangChain objects, you can serialize and deserialize them using the following functions:
dumpd: Returns a dictionary representation of an object, suitable for JSON serialization.dumps: Returns a JSON string representation of an object.load: Reconstructs an object from its dictionary representation.loads: Reconstructs an object from its JSON string representation.
Table of Contents
References
Environment Setup
Set up the environment. You may refer to Environment Setup for more details.
[Note]
langchain-opentutorialis a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.You can checkout the
langchain-opentutorialfor more details.
Dumps and Loads
Dumps and Loadsdumps: LangChain object into a JSON-formatted stringloads: JSON-formatted string into a LangChain object
Dumpd and Load
Dumpd and Loaddumpd: LangChain object into a dictionaryload: dictionary into a LangChain object
Serialization with pickle
pickleThe pickle module in Python is used for serializing and deserializing Python object structures, also known as pickling and unpickling. Serialization involves converting a Python object hierarchy into a byte stream, while deserialization reconstructs the object hierarchy from the byte stream.
pickle - Python object serialization for more details
Key Functions
pickle.dump(obj, file): Serializesobjand writes it to the open file objectfile.pickle.load(file): Reads a byte stream from the open file objectfileand deserializes it back into a Python object.
Is Every Runnable Serializable?
LangChain's dumps and dumpd methods attempt to serialize objects as much as possible, but the resulting data may be incomplete.
Even if the
is_lc_serializablemethod does not exist or returnsFalse, a result is still generated.Even if the
is_lc_serializablemethod returnsTrueand serialization is successful, deserialization may fail.
After serialization, it is essential to check if the JSON data contains "type": "not_implemented". Only then can the load or loads functions be used safely.
Last updated