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:
parent
357d13b1a9
commit
8e2342cf21
1 changed files with 7 additions and 0 deletions
|
|
@ -25,6 +25,7 @@ from langflow.interface.toolkits.base import toolkits_creator
|
||||||
from langflow.interface.types import get_type_list
|
from langflow.interface.types import get_type_list
|
||||||
from langflow.interface.utils import load_file_into_dict
|
from langflow.interface.utils import load_file_into_dict
|
||||||
from langflow.utils import util, validate
|
from langflow.utils import util, validate
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
def instantiate_class(node_type: str, base_type: str, params: Dict) -> Any:
|
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)
|
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":
|
if base_type == "agents":
|
||||||
# We need to initialize it differently
|
# We need to initialize it differently
|
||||||
return load_agent_executor(class_object, params)
|
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 load_toolkits_executor(node_type, loaded_toolkit, params)
|
||||||
return loaded_toolkit
|
return loaded_toolkit
|
||||||
elif base_type == "embeddings":
|
elif base_type == "embeddings":
|
||||||
|
# ? Why remove model from params?
|
||||||
params.pop("model")
|
params.pop("model")
|
||||||
return class_object(**params)
|
return class_object(**params)
|
||||||
elif base_type == "vectorstores":
|
elif base_type == "vectorstores":
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue