diff --git a/src/backend/base/langflow/template/field/base.py b/src/backend/base/langflow/template/field/base.py index 2fe1a7a9d..e82576230 100644 --- a/src/backend/base/langflow/template/field/base.py +++ b/src/backend/base/langflow/template/field/base.py @@ -1,11 +1,12 @@ from enum import Enum -from typing import Optional # type: ignore -from typing import Any, Callable, Union +from typing import Any, Callable, GenericAlias, Optional, Union, _GenericAlias, _UnionGenericAlias # type: ignore from pydantic import BaseModel, ConfigDict, Field, field_serializer, field_validator, model_serializer, model_validator from langflow.field_typing import Text from langflow.field_typing.range_spec import RangeSpec +from langflow.helpers.custom import format_type +from langflow.type_extraction.type_extraction import post_process_type class UndefinedType(Enum): @@ -139,6 +140,19 @@ class Input(BaseModel): for file_type in value ] + @field_validator("field_type", mode="before") + @classmethod + def validate_type(cls, v): + # If the user passes CustomComponent as a type insteado of "CustomComponent" we need to convert it to a string + # this should be done for all types + # How to check if v is a type? + if isinstance(v, (type, _GenericAlias, GenericAlias, _UnionGenericAlias)): + v = post_process_type(v)[0] + v = format_type(v) + elif not isinstance(v, str): + raise ValueError(f"type must be a string or a type, not {type(v)}") + return v + class Output(BaseModel): types: Optional[list[str]] = Field(default=[])