diff --git a/src/backend/base/langflow/template/field/input_mixin.py b/src/backend/base/langflow/template/field/input_mixin.py index e58749c92..a053c62b4 100644 --- a/src/backend/base/langflow/template/field/input_mixin.py +++ b/src/backend/base/langflow/template/field/input_mixin.py @@ -1,7 +1,7 @@ from enum import Enum -from typing import Any, Optional +from typing import Annotated, Any, Optional -from pydantic import BaseModel, ConfigDict, Field, field_validator +from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator from langflow.field_typing.range_spec import RangeSpec @@ -18,11 +18,14 @@ class FieldTypes(str, Enum): PROMPT = "Prompt" +SerializableFieldTypes = Annotated[FieldTypes, PlainSerializer(lambda v: v.value, return_type=str)] + + # Base mixin for common input field attributes and methods class BaseInputMixin(BaseModel): model_config = ConfigDict(arbitrary_types_allowed=True) - field_type: Optional[FieldTypes] = Field(default=FieldTypes.TEXT) + field_type: Optional[SerializableFieldTypes] = Field(default=FieldTypes.TEXT) required: bool = False """Specifies if the field is required. Defaults to False.""" diff --git a/src/backend/base/langflow/template/field/inputs.py b/src/backend/base/langflow/template/field/inputs.py index c71061f6e..859ee8de3 100644 --- a/src/backend/base/langflow/template/field/inputs.py +++ b/src/backend/base/langflow/template/field/inputs.py @@ -1,46 +1,59 @@ -from pydantic import SecretStr +from typing import Callable, Optional, Union -from langflow.field_typing.constants import NestedDict -from langflow.template.field.base import Input +from pydantic import Field + +from langflow.template.field.input_mixin import ( + BaseInputMixin, + DatabaseLoadMixin, + DropDownMixin, + FieldTypes, + FileMixin, + ListableInputMixin, + RangeMixin, +) -class StrInput(Input): - field_type: str | type | None = str +class PromptInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.PROMPT -class SecretStrInput(Input): - field_type: str | type | None = SecretStr - password = True +# Applying mixins to a specific input type +class StrInput(BaseInputMixin, ListableInputMixin): # noqa: F821 + field_type = FieldTypes.TEXT + multiline: bool = Field(default=False) + """Defines if the field will allow the user to open a text editor. Default is False.""" -class IntInput(Input): - field_type: str | type | None = int +class SecretStrInput(BaseInputMixin, DatabaseLoadMixin): + field_type = FieldTypes.PASSWORD + password: bool = Field(default=True) -class FloatInput(Input): - field_type: str | type | None = float +class IntInput(BaseInputMixin, ListableInputMixin, RangeMixin): + field_type = FieldTypes.INTEGER -class BoolInput(Input): - field_type: str | type | None = bool +class FloatInput(BaseInputMixin, ListableInputMixin, RangeMixin): + field_type = FieldTypes.FLOAT -class NestedDictInput(Input): - field_type: str | type | None = NestedDict +class BoolInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.BOOLEAN -class DictInput(Input): - field_type: str | type | None = dict +class NestedDictInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.NESTED_DICT -class ListInput(Input): - is_list = True +class DictInput(BaseInputMixin, ListableInputMixin): + field_type = FieldTypes.DICT -class DropdownInput(Input): - field_type: str | type | None = str - options = [] +class DropdownInput(BaseInputMixin, DropDownMixin): + field_type = FieldTypes.TEXT + options: Optional[Union[list[str], Callable]] = None + """List of options for the field. Only used when is_list=True. Default is an empty list.""" -class FileInput(Input): - field_type: str | type | None = str +class FileInput(BaseInputMixin, ListableInputMixin, FileMixin): + field_type = FieldTypes.FILE