langflow-ai / langflow

Langflow is a low-code app builder for RAG and multi-agent AI applications. It’s Python-based and agnostic to any model, API, or database.
http://www.langflow.org
MIT License
34.53k stars 4.16k forks source link

`Conditional Router` does not stop correctly #4490

Open 9n opened 1 week ago

9n commented 1 week ago

Bug Description

Conditional Router does not stop correctly. The input message display twice, followed by a error message: "Error parsing message". It's not reproduced every time, try more times.

Screenshot 2024-11-11 at 17-16-33 Langflow

Reproduction

  1. import the flow file
  2. input hello in the playground
  3. it's not reproduced every time, try more times.

Expected behavior

return hi, there I'm a robot directly and no error message

Who can help?

No response

Operating System

manjaro

Langflow Version

v1.0.19.post2

Python Version

None

Screenshot

Screenshot 2024-11-11 at 17-17-52 Langflow

Flow File

Simple Conditional Router.json

dosubot[bot] commented 1 week ago

The issue with the Conditional Router not stopping correctly and causing the input message to display twice, followed by an "Error parsing message," is likely due to a problem in the iterate_and_stop_once method. The method is supposed to stop the opposite route when the maximum number of iterations is reached, but it seems to be incorrectly implemented.

Here's the relevant part of the code:

def iterate_and_stop_once(self, route_to_stop: str):
    if not self.__iteration_updated:
        self.update_ctx({f"{self._id}_iteration": self.ctx.get(f"{self._id}_iteration", 0) + 1})
        self.__iteration_updated = True
        if self.ctx.get(f"{self._id}_iteration", 0) >= self.max_iterations and route_to_stop == self.default_route:
            # We need to stop the other route
            route_to_stop = "true_result" if route_to_stop == "false_result" else "false_result"
        self.stop(route_to_stop)

The problem arises because the route_to_stop is being reassigned to the opposite route when the maximum iterations are reached, but the stop method is still called with the original route_to_stop value. This means that the intended route to stop is not being stopped, leading to the message being processed twice.

To fix this, the stop method should be called with the updated route_to_stop value:

def iterate_and_stop_once(self, route_to_stop: str):
    if not self.__iteration_updated:
        self.update_ctx({f"{self._id}_iteration": self.ctx.get(f"{self._id}_iteration", 0) + 1})
        self.__iteration_updated = True
        if self.ctx.get(f"{self._id}_iteration", 0) >= self.max_iterations and route_to_stop == self.default_route:
            # We need to stop the other route
            route_to_stop = "true_result" if route_to_stop == "false_result" else "false_result"
        self.stop(route_to_stop)  # Ensure this uses the updated route_to_stop

This change ensures that the correct route is stopped, preventing the message from being processed twice and avoiding the "Error parsing message" [1].

To continue talking to Dosu, mention @dosu.

ChunJen commented 5 days ago

Hi, I have the same question. It happened in both of version 1.0.17 and 1.1.0

I change the conditional Router with self.stop('method_name'), but the components of the subsequent process will still be executed.

    def true_response(self) -> Message:
        result = self.evaluate_condition(self.input_text, self.match_text, self.operator, self.case_sensitive)
        if result:
            return self.message
        else:
            self.stop('true_response')

With the workflow:

[python 3.12.3, langflow 1.0.17]

20241114_debug_conditional_router.json 20241114_debug_conditional_router_v2.json

Test Case

1.input=A, expected_output=A, real_output=A None None

image

2.input=B, expected_output=B, real_output=None B None

image

[python 3.12.3, langflow 1.1.0]

Still not work in this version: 20241114_debug_conditional_router_v2_使用langflow1.1.0版本.json

irshadmb commented 5 days ago

@joaoguilhermeS - I have given the details.

https://github.com/langflow-ai/langflow/issues/4590#issuecomment-2475895886