allow other namespaces (#14606)

This commit is contained in:
Harrison Chase 2023-12-12 09:09:59 -08:00 committed by GitHub
parent ce61a8ca98
commit ad8d8f71aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,21 +63,27 @@ class Reviver:
if len(namespace) == 1 and namespace[0] == "langchain": if len(namespace) == 1 and namespace[0] == "langchain":
raise ValueError(f"Invalid namespace: {value}") raise ValueError(f"Invalid namespace: {value}")
# Get the importable path # If namespace is in known namespaces, try to use mapping
key = tuple(namespace + [name]) if namespace[0] in DEFAULT_NAMESPACES:
if key not in SERIALIZABLE_MAPPING: # Get the importable path
raise ValueError( key = tuple(namespace + [name])
"Trying to deserialize something that cannot " if key not in SERIALIZABLE_MAPPING:
"be deserialized in current version of langchain-core: " raise ValueError(
f"{key}" "Trying to deserialize something that cannot "
) "be deserialized in current version of langchain-core: "
import_path = SERIALIZABLE_MAPPING[key] f"{key}"
# Split into module and name )
import_dir, import_obj = import_path[:-1], import_path[-1] import_path = SERIALIZABLE_MAPPING[key]
# Import module # Split into module and name
mod = importlib.import_module(".".join(import_dir)) import_dir, import_obj = import_path[:-1], import_path[-1]
# Import class # Import module
cls = getattr(mod, import_obj) mod = importlib.import_module(".".join(import_dir))
# Import class
cls = getattr(mod, import_obj)
# Otherwise, load by path
else:
mod = importlib.import_module(".".join(namespace))
cls = getattr(mod, name)
# The class must be a subclass of Serializable. # The class must be a subclass of Serializable.
if not issubclass(cls, Serializable): if not issubclass(cls, Serializable):