From 599ef9cc735eea6a7eabb231b8efc7e80cfe30b2 Mon Sep 17 00:00:00 2001 From: ogabrielluiz Date: Tue, 18 Jun 2024 22:11:06 -0300 Subject: [PATCH] refactor: Add MultilineMixin to StrInput for multiline input support and adapt validation --- src/backend/base/langflow/inputs/input_mixin.py | 6 ++++++ src/backend/base/langflow/inputs/inputs.py | 10 ++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/backend/base/langflow/inputs/input_mixin.py b/src/backend/base/langflow/inputs/input_mixin.py index 720b095de..4643f9471 100644 --- a/src/backend/base/langflow/inputs/input_mixin.py +++ b/src/backend/base/langflow/inputs/input_mixin.py @@ -4,6 +4,8 @@ from typing import Annotated, Any, Optional from pydantic import BaseModel, ConfigDict, Field, PlainSerializer, field_validator, model_serializer from langflow.field_typing.range_spec import RangeSpec +from langflow.inputs.validators import CoalesceBool +from langflow.inputs.validators import CoalesceBool class FieldTypes(str, Enum): @@ -123,3 +125,7 @@ class RangeMixin(BaseModel): class DropDownMixin(BaseModel): options: Optional[list[str]] = None """List of options for the field. Only used when is_list=True. Default is an empty list.""" + + +class MultilineMixin(BaseModel): + multiline: CoalesceBool = True diff --git a/src/backend/base/langflow/inputs/inputs.py b/src/backend/base/langflow/inputs/inputs.py index 5f1fc5bde..c8d4f2202 100644 --- a/src/backend/base/langflow/inputs/inputs.py +++ b/src/backend/base/langflow/inputs/inputs.py @@ -1,5 +1,6 @@ from typing import Any, AsyncIterator, Callable, Iterator, Optional, Union +from loguru import logger from pydantic import Field, field_validator from langflow.inputs.validators import CoalesceBool @@ -13,6 +14,7 @@ from .input_mixin import ( FieldTypes, FileMixin, ListableInputMixin, + MultilineMixin, RangeMixin, SerializableFieldTypes, ) @@ -49,12 +51,11 @@ class PromptInput(BaseInputMixin, ListableInputMixin): # Applying mixins to a specific input type -class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa: F821 +class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin, MultilineMixin): field_type: Optional[SerializableFieldTypes] = FieldTypes.TEXT load_from_db: CoalesceBool = False """Defines if the field will allow the user to open a text editor. Default is False.""" - @staticmethod def _validate_value(v: Any, _info): """ Validates the given value and returns the processed value. @@ -70,7 +71,8 @@ class StrInput(BaseInputMixin, ListableInputMixin, DatabaseLoadMixin): # noqa: ValueError: If the value is not of a valid type or if the input is missing a required key. """ if not isinstance(v, str) and v is not None: - raise ValueError(f"Invalid value type {type(v)}") + if _info.data.get("input_types") and v.__class__.__name__ not in _info.data.get("input_types"): + logger.warning(f"Invalid value type {type(v)}") return v @field_validator("value") @@ -160,7 +162,7 @@ class TextInput(StrInput): return value -class MultilineInput(BaseInputMixin): +class MultilineInput(BaseInputMixin, MultilineMixin): """ Represents a multiline input field.