Skip to content

Commit

Permalink
fix: check if variable is valid before converting (#3514)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
ogabrielluiz authored Aug 22, 2024
1 parent 9d5f97a commit 4e15090
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
14 changes: 13 additions & 1 deletion src/backend/base/langflow/api/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
25 changes: 12 additions & 13 deletions src/backend/base/langflow/api/v1/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

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,
Expand Down Expand Up @@ -45,8 +49,6 @@
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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 4e15090

Please sign in to comment.