Skip to content

Commit 7daefb8

Browse files
authored
fix: Improve error messages for non-string templates in ConditionalRouter (#10189)
* Improve error messages for non-string templates in ConditionalRouter * Add reno note * Minor test fix
1 parent 5efa691 commit 7daefb8

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

haystack/components/routers/conditional_router.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,23 @@ def _validate_routes(self, routes: list[Route]):
398398

399399
# Validate templates
400400
if not self._validate_template(self._env, route["condition"]):
401-
raise ValueError(f"Invalid template for condition: {route['condition']}")
401+
condition_value = route["condition"]
402+
if not isinstance(condition_value, str):
403+
raise ValueError(
404+
f"Invalid template for condition: {condition_value!r} (type: {type(condition_value).__name__})."
405+
f"Condition must be a string representing a valid Jinja2 template. "
406+
f"For example, use {str(condition_value)!r} instead of {condition_value!r}."
407+
)
408+
raise ValueError(f"Invalid template for condition: {condition_value}")
402409

403410
for output in outputs:
404411
if not self._validate_template(self._env, output):
412+
if not isinstance(output, str):
413+
raise ValueError(
414+
f"Invalid template for output: {output!r} (type: {type(output).__name__}). "
415+
f"Output must be a string representing a valid Jinja2 template. "
416+
f"For example, use {str(output)!r} instead of {output!r}."
417+
)
405418
raise ValueError(f"Invalid template for output: {output}")
406419

407420
@staticmethod
@@ -429,6 +442,9 @@ def _validate_template(self, env: Environment, template_text: str):
429442
:param template_text: A Jinja template string.
430443
:returns: `True` if the template is valid, `False` otherwise.
431444
"""
445+
# Check if template_text is a string before attempting to parse
446+
if not isinstance(template_text, str):
447+
return False
432448
try:
433449
env.parse(template_text)
434450
return True
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Improved error messages in ConditionalRouter when non-string values are provided as route outputs.
5+
Users now receive clear guidance (e.g., "use '2' instead of 2") instead of the cryptic "Can't compile non template nodes" error.

test/components/routers/test_conditional_router.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,47 @@ def test_invalid_condition_field(self):
4040
with pytest.raises(ValueError, match="Invalid template"):
4141
ConditionalRouter(routes)
4242

43+
def test_invalid_output_template_non_string(self):
44+
"""
45+
ConditionalRouter init raises a ValueError with helpful error message when output is not a string
46+
"""
47+
# output is an int instead of a string template
48+
routes = [
49+
{
50+
"condition": '{{ flag == "double" }}',
51+
"output": 2,
52+
"output_name": "num_additional_outputs",
53+
"output_type": int,
54+
}
55+
]
56+
with pytest.raises(ValueError) as exc_info:
57+
ConditionalRouter(routes)
58+
error_message = str(exc_info.value)
59+
assert "Invalid template for output" in error_message
60+
assert "string" in error_message
61+
assert "Jinja2 template" in error_message
62+
assert "2" in error_message
63+
64+
def test_invalid_output_template_non_string_list(self):
65+
"""
66+
ConditionalRouter init raises a ValueError with helpful error message when output in list is not a string
67+
"""
68+
# output list contains an int instead of a string template
69+
routes = [
70+
{
71+
"condition": '{{ flag == "double" }}',
72+
"output": ["{{streams}}", 2],
73+
"output_name": ["streams", "num"],
74+
"output_type": [list[int], int],
75+
}
76+
]
77+
with pytest.raises(ValueError) as exc_info:
78+
ConditionalRouter(routes)
79+
error_message = str(exc_info.value)
80+
assert "Invalid template for output" in error_message
81+
assert "string" in error_message
82+
assert "Jinja2 template" in error_message
83+
4384
def test_no_vars_in_output_route_but_with_output_name(self):
4485
"""
4586
Router can't accept a route with no variables used in the output field

0 commit comments

Comments
 (0)