Negate a Router condition #9787
Replies: 2 comments 2 replies
-
Hi @FrancescoSaverioZuppichini the condition is in Jinja2 syntax and to correctly negate it, you need If you are interested, here are the lines of code in Haystack where the condition in Jinja2 syntax is first rendered and then evaluated: haystack/haystack/components/routers/conditional_router.py Lines 324 to 326 in e3d4e9e The step of rendering the condition is a simple step or more complex depending on the condition as the condition only needs to be valid Jinja2 syntax and might contain multiple functions. For example, the following can be easily negated routes = [
{
"condition": "{{messages[-1].meta.finish_reason == 'function_call'}}",
"output": "{{streams}}",
"output_type": list[int],
"output_name": "streams",
},
{
"condition": "{{True}}",
"output": "{{query}}",
"output_type": str,
"output_name": "query",
}, # catch-all condition
]
router = ConditionalRouter(routes) However, the following is more complex: routes = [
{
"condition": "{{streams|length > 2}}",
"output": "{{streams}}",
"output_name": "enough_streams",
"output_type": list[int],
},
{
"condition": "{{streams|length <= 2}}",
"output": "{{streams}}",
"output_name": "insufficient_streams",
"output_type": list[int],
},
]
router = ConditionalRouter(routes) |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for the reply! My issue is that if I put |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi people,
What is the right syntax to negate a router condition?
Thanks a lot
Beta Was this translation helpful? Give feedback.
All reactions