Serialization
dumpd
¶
dumps
¶
Return a JSON string representation of an object.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object to dump.
TYPE:
|
pretty
|
Whether to pretty print the json. If
TYPE:
|
**kwargs
|
Additional arguments to pass to
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A JSON string representation of the object. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
load
¶
load(
obj: Any,
*,
secrets_map: dict[str, str] | None = None,
valid_namespaces: list[str] | None = None,
secrets_from_env: bool = True,
additional_import_mappings: dict[tuple[str, ...], tuple[str, ...]] | None = None,
ignore_unserializable_fields: bool = False,
) -> Any
Revive a LangChain class from a JSON object.
Use this if you already have a parsed JSON object,
eg. from json.load or orjson.loads.
| PARAMETER | DESCRIPTION |
|---|---|
obj
|
The object to load.
TYPE:
|
secrets_map
|
A map of secrets to load. If a secret is not found in the map, it will be loaded from the environment
if |
valid_namespaces
|
A list of additional namespaces (modules) to allow to be deserialized. |
secrets_from_env
|
Whether to load secrets from the environment.
TYPE:
|
additional_import_mappings
|
A dictionary of additional namespace mappings You can use this to override default mappings or add new mappings.
TYPE:
|
ignore_unserializable_fields
|
Whether to ignore unserializable fields.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Revived LangChain objects. |
loads
¶
loads(
text: str,
*,
secrets_map: dict[str, str] | None = None,
valid_namespaces: list[str] | None = None,
secrets_from_env: bool = True,
additional_import_mappings: dict[tuple[str, ...], tuple[str, ...]] | None = None,
ignore_unserializable_fields: bool = False,
) -> Any
Revive a LangChain class from a JSON string.
Equivalent to load(json.loads(text)).
| PARAMETER | DESCRIPTION |
|---|---|
text
|
The string to load.
TYPE:
|
secrets_map
|
A map of secrets to load. If a secret is not found in the map, it will be loaded from the environment
if |
valid_namespaces
|
A list of additional namespaces (modules) to allow to be deserialized. |
secrets_from_env
|
Whether to load secrets from the environment.
TYPE:
|
additional_import_mappings
|
A dictionary of additional namespace mappings You can use this to override default mappings or add new mappings.
TYPE:
|
ignore_unserializable_fields
|
Whether to ignore unserializable fields.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
Revived LangChain objects. |
Serializable
¶
Serializable base class.
This class is used to serialize objects to JSON.
It relies on the following methods and properties:
is_lc_serializable: Is this class serializable? By design, even if a class inherits fromSerializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.-
get_lc_namespace: Get the namespace of the LangChain object.During deserialization, this namespace is used to identify the correct class to instantiate.
Please see the
Reviverclass inlangchain_core.load.loadfor more details. During deserialization an additional mapping is handle classes that have moved or been renamed across package versions. -
lc_secrets: A map of constructor argument names to secret ids. lc_attributes: List of additional attribute names that should be included as part of the serialized representation.
| METHOD | DESCRIPTION |
|---|---|
__init__ |
|
is_lc_serializable |
Is this class serializable? |
get_lc_namespace |
Get the namespace of the LangChain object. |
lc_id |
Return a unique identifier for this class for serialization purposes. |
to_json |
Serialize the object to JSON. |
to_json_not_implemented |
Serialize a "not implemented" object. |
lc_secrets
property
¶
A map of constructor argument names to secret ids.
For example, {"openai_api_key": "OPENAI_API_KEY"}
lc_attributes
property
¶
lc_attributes: dict
List of attribute names that should be included in the serialized kwargs.
These attributes must be accepted by the constructor.
Default is an empty dictionary.
is_lc_serializable
classmethod
¶
is_lc_serializable() -> bool
Is this class serializable?
By design, even if a class inherits from Serializable, it is not serializable
by default. This is to prevent accidental serialization of objects that should
not be serialized.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether the class is serializable. Default is |
get_lc_namespace
classmethod
¶
lc_id
classmethod
¶
Return a unique identifier for this class for serialization purposes.
The unique identifier is a list of strings that describes the path to the object.
For example, for the class langchain.llms.openai.OpenAI, the id is
["langchain", "llms", "openai", "OpenAI"].
to_json
¶
Serialize the object to JSON.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the class has deprecated attributes. |
| RETURNS | DESCRIPTION |
|---|---|
SerializedConstructor | SerializedNotImplemented
|
A JSON serializable object or a |
to_json_not_implemented
¶
Serialize a "not implemented" object.
| RETURNS | DESCRIPTION |
|---|---|
SerializedNotImplemented
|
|