Model Serialization

Open in ColabOpen in GitHub

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-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

Dumps and Loads

  • dumps : LangChain object into a JSON-formatted string

  • loads : JSON-formatted string into a LangChain object

Dumpd and Load

  • dumpd : LangChain object into a dictionary

  • load : dictionary into a LangChain object

Serialization with pickle

The 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): Serializes obj and writes it to the open file object file.

  • pickle.load(file): Reads a byte stream from the open file object file and 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.

  1. Even if the is_lc_serializable method does not exist or returns False, a result is still generated.

  2. Even if the is_lc_serializable method returns True and 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