From 3441bb4e5bf43147d56b5551b1554acc6fdd659a Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sat, 8 Jul 2023 16:45:33 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(utils.py):=20improve=20error?= =?UTF-8?q?=20handling=20and=20file=20type=20detection=20in=20load=5Ffile?= =?UTF-8?q?=5Finto=5Fdict=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/interface/utils.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/backend/langflow/interface/utils.py b/src/backend/langflow/interface/utils.py index 1ab2b4ce5..9203915cf 100644 --- a/src/backend/langflow/interface/utils.py +++ b/src/backend/langflow/interface/utils.py @@ -16,17 +16,15 @@ def load_file_into_dict(file_path: str) -> dict: if not os.path.exists(file_path): raise FileNotFoundError(f"File not found: {file_path}") - file_extension = os.path.splitext(file_path)[1].lower() - - if file_extension == ".json": - with open(file_path, "r") as json_file: - data = json.load(json_file) - elif file_extension in [".yaml", ".yml"]: - with open(file_path, "r") as yaml_file: - data = yaml.safe_load(yaml_file) - else: - raise ValueError("Unsupported file type. Please provide a JSON or YAML file.") - + # Files names are UUID, so we can't find the extension + with open(file_path, "r") as file: + try: + data = json.load(file) + except json.JSONDecodeError: + file.seek(0) + data = yaml.safe_load(file) + except ValueError as exc: + raise ValueError("Invalid file type. Expected .json or .yaml.") from exc return data