feat(loading.py): add validation for pydantic BaseModel subclasses

This commit adds validation for pydantic BaseModel subclasses in the
instantiate_class function. The function now checks if the class_object
is a subclass of BaseModel and if so, it validates the params passed to
it against the fields of the class_object.
This commit is contained in:
Gabriel Almeida 2023-04-28 17:22:51 -03:00
commit 8e2342cf21

View file

@ -25,6 +25,7 @@ from langflow.interface.toolkits.base import toolkits_creator
from langflow.interface.types import get_type_list
from langflow.interface.utils import load_file_into_dict
from langflow.utils import util, validate
from pydantic import BaseModel
def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any:
@ -35,6 +36,11 @@ def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any:
class_object = import_by_type(_type=base_type, name=node_type)
if issubclass(class_object, BaseModel):
# validate params
fields = class_object.__fields__
params = {key: value for key, value in params.items() if key in fields}
if base_type == "agents":
# We need to initialize it differently
return load_agent_executor(class_object, params)
@ -66,6 +72,7 @@ def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any:
return load_toolkits_executor(node_type, loaded_toolkit, params)
return loaded_toolkit
elif base_type == "embeddings":
# ? Why remove model from params?
params.pop("model")
return class_object(**params)
elif base_type == "vectorstores":