From 8c0a2b62a335e32b45fc33d5117840b0113d49c8 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Wed, 20 Mar 2024 17:55:49 -0300 Subject: [PATCH] Refactor custom fields handling in validate.py --- src/backend/langflow/api/v1/validate.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index 09d32cb8e..72d24b3bf 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -44,13 +44,13 @@ def post_validate_prompt(prompt_request: ValidatePromptRequest): input_variables=input_variables, frontend_node=None, ) - if not prompt_request.custom_fields: - prompt_request.custom_fields = defaultdict(list) - old_custom_fields = get_old_custom_fields(prompt_request.custom_fields, prompt_request.name) + if not prompt_request.frontend_node.custom_fields: + prompt_request.frontend_node.custom_fields = defaultdict(list) + old_custom_fields = get_old_custom_fields(prompt_request.frontend_node.custom_fields, prompt_request.name) add_new_variables_to_template( input_variables, - prompt_request.custom_fields, + prompt_request.frontend_node.custom_fields, prompt_request.frontend_node.template, prompt_request.name, ) @@ -58,13 +58,25 @@ def post_validate_prompt(prompt_request: ValidatePromptRequest): remove_old_variables_from_template( old_custom_fields, input_variables, - prompt_request.custom_fields, + prompt_request.frontend_node.custom_fields, prompt_request.frontend_node.template, prompt_request.name, ) update_input_variables_field(input_variables, prompt_request.frontend_node.template) + # If frontend_node.template contains only one field that is type == 'prompt', then we can remove all fields that are not + # 'code', and not in the input_variables list. + prompt_fields = [ + key + for key, field in prompt_request.frontend_node.template.items() + if isinstance(field, dict) and field["type"] == "prompt" + ] + + if len(prompt_fields) == 1: + for key, field in prompt_request.frontend_node.template.copy().items(): + if isinstance(field, dict) and field["type"] != "code" and key not in input_variables + prompt_fields: + del prompt_request.frontend_node.template[key] return PromptValidationResponse( input_variables=input_variables, frontend_node=prompt_request.frontend_node,