fix: check if variable is valid before converting (#3514)

* feat: Add logging and session imports to endpoints.py

Import logging and session modules for better error handling and database interaction in endpoints.py.

* refactor: Improve custom component parameter handling in update endpoint

* feat: Add parse_value function to parse values based on input type

Add a new function `parse_value` to `utils.py` that parses values based on the input type provided. The function handles different input types such as 'IntInput' and 'FloatInput' to ensure proper parsing.

* feat: Refactor custom_component_update() to use parse_value utility function

Use parse_value utility function to handle different input types and empty values in custom_component_update() for better readability and maintainability.
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-08-22 18:27:36 -03:00 • committed by GitHub
commit 4e15090927
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 14 deletions

View file

@ -1,6 +1,6 @@
import uuid
import warnings
from typing import TYPE_CHECKING, Optional, Dict
from typing import TYPE_CHECKING, Any, Optional, Dict
from fastapi import HTTPException
from sqlmodel import Session
@ -227,3 +227,15 @@ def get_suggestion_message(outdated_components: list[str]) -> str:
else:
components = ", ".join(outdated_components)
return f"The flow contains {count} outdated components. We recommend updating the following components: {components}."
def parse_value(value: Any, input_type: str) -> Any:
"""Helper function to parse the value based on input type."""
if value == "":
return value
elif input_type == "IntInput":
return int(value) if value is not None else None
elif input_type == "FloatInput":
return float(value) if value is not None else None
else:
return value

View file

@ -6,6 +6,10 @@ from uuid import UUID
import sqlalchemy as sa
from fastapi import APIRouter, BackgroundTasks, Body, Depends, HTTPException, Request, UploadFile, status
from loguru import logger
from sqlmodel import Session, select
from langflow.api.utils import parse_value
from langflow.api.v1.schemas import (
ConfigResponse,
CustomComponentRequest,
@ -45,8 +49,6 @@ from langflow.services.task.service import TaskService
from langflow.services.telemetry.schema import RunPayload
from langflow.services.telemetry.service import TelemetryService
from langflow.utils.version import get_version_info
from loguru import logger
from sqlmodel import Session, select
if TYPE_CHECKING:
from langflow.services.cache.base import CacheService
@ -590,17 +592,14 @@ async def custom_component_update(
)
if hasattr(cc_instance, "set_attributes"):
template = code_request.get_template()
params = {
key: value_dict.get("value")
if value_dict.get("_input_type") != "IntInput"
else (
int(value_dict.get("value")) # type: ignore
if value_dict.get("_input_type") != "FloatInput"
else float(value_dict.get("value")) # type: ignore
)
for key, value_dict in template.items()
if isinstance(value_dict, dict)
}
params = {}
for key, value_dict in template.items():
if isinstance(value_dict, dict):
value = value_dict.get("value")
input_type = str(value_dict.get("_input_type"))
params[key] = parse_value(value, input_type)
load_from_db_fields = [
field_name
for field_name, field_dict in template.items()