From 526f5847c6a4dce8609a8e98db913d0292b593c6 Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Tue, 27 Jun 2023 20:54:00 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(validate.py):=20remove=20unu?= =?UTF-8?q?sed=20variables=20from=20prompt.frontend=5Fnode.template=20?= =?UTF-8?q?=E2=9C=A8=20feat(validate.py):=20add=20support=20for=20dynamic?= =?UTF-8?q?=20template=20fields=20in=20prompt=20validation=20The=20changes?= =?UTF-8?q?=20in=20this=20commit=20remove=20unused=20variables=20from=20th?= =?UTF-8?q?e=20prompt.frontend=5Fnode.template.=20The=20commit=20also=20ad?= =?UTF-8?q?ds=20support=20for=20dynamic=20template=20fields=20in=20prompt?= =?UTF-8?q?=20validation.=20The=20new=20variables=20are=20added=20to=20the?= =?UTF-8?q?=20template=20and=20the=20custom=5Ffields=20list.=20The=20old?= =?UTF-8?q?=20custom=5Ffields=20list=20is=20copied=20and=20then=20updated?= =?UTF-8?q?=20with=20the=20new=20variables.=20The=20variables=20that=20are?= =?UTF-8?q?=20not=20in=20the=20template=20anymore=20are=20removed=20from?= =?UTF-8?q?=20the=20prompt.frontend=5Fnode.template.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/backend/langflow/api/v1/validate.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/backend/langflow/api/v1/validate.py b/src/backend/langflow/api/v1/validate.py index 802ee3e3d..431945602 100644 --- a/src/backend/langflow/api/v1/validate.py +++ b/src/backend/langflow/api/v1/validate.py @@ -31,6 +31,10 @@ def post_validate_code(code: Code): def post_validate_prompt(prompt: ValidatePromptRequest): try: input_variables = validate_prompt(prompt.template) + # Reinitialize custom_fields + old_custom_fields = prompt.frontend_node.custom_fields.copy() + prompt.frontend_node.custom_fields = [] + # Add new variables to the template for variable in input_variables: try: template_field = TemplateField( @@ -39,13 +43,20 @@ def post_validate_prompt(prompt: ValidatePromptRequest): prompt.frontend_node.template[variable] = template_field.to_dict() prompt.frontend_node.custom_fields.append(variable) - for key in prompt.frontend_node.template: - if key not in input_variables: - prompt.frontend_node.template.pop(key, None) + except Exception as exc: logger.exception(exc) raise HTTPException(status_code=500, detail=str(exc)) from exc + # Remove variables that are not in the template anymore + for variable in old_custom_fields: + if variable not in input_variables: + try: + prompt.frontend_node.template.pop(variable, None) + except Exception as exc: + logger.exception(exc) + raise HTTPException(status_code=500, detail=str(exc)) from exc + return PromptValidationResponse( input_variables=input_variables, frontend_node=prompt.frontend_node,