Build AI methods with your coding agent, then run them anywhere: as an MCP for chatbots, as a webapp for people, or via API for your software.
Fix template preprocessor when there is a dot after the variable name
Louis Choquel17e98095c4b3
diff --git a/pipelex/tools/templating/template_preprocessor.py b/pipelex/tools/templating/template_preprocessor.pyindex a7b82174b..7cfdadc5b 100644--- a/pipelex/tools/templating/template_preprocessor.py+++ b/pipelex/tools/templating/template_preprocessor.py@@ -38,12 +38,20 @@ def _detect_non_existent_filters(template_str: str) -> None:# Handle @variable patternsdef replace_at_variable(match: Match[str]) -> str:variable: str = match.group(1)+ if variable.endswith("."):+ # trailing dot can't be in a variable name so it must be a punctuation in the template sentence, so we remove it+ variable = variable[:-1]+ return f'{{{{ {variable}|tag("{variable}") }}}}.'return f'{{{{ {variable}|tag("{variable}") }}}}'# Handle @?variable patterns (optional insertion)def replace_optional_at_variable(match: Match[str]) -> str:variable: str = match.group(1)+ if variable.endswith("."):+ # trailing dot can't be in a variable name so it must be a punctuation in the template sentence, so we remove it+ variable = variable[:-1]+ return f'{{% if {variable} %}}{{{{ {variable}|tag("{variable}") }}}}{{% endif %}}.'return f'{{% if {variable} %}}{{{{ {variable}|tag("{variable}") }}}}{{% endif %}}'diff --git a/tests/unit/pipelex/tools/templating/test_template_preprocessor.py b/tests/unit/pipelex/tools/templating/test_template_preprocessor.pyindex 76f56f99b..ba7b67028 100644--- a/tests/unit/pipelex/tools/templating/test_template_preprocessor.py+++ b/tests/unit/pipelex/tools/templating/test_template_preprocessor.py@@ -120,3 +120,24 @@ Optional notes:'{% if optional_var %}{{ optional_var|tag("optional_var") }}{% endif %}')assert result == expected++ def test_at_variable_with_trailing_dot(self):+ """Test @variable pattern with trailing dot (punctuation)."""+ template = "Extract employee information from this invoice text: @invoice_text."+ result = preprocess_template(template)+ expected = 'Extract employee information from this invoice text: {{ invoice_text|tag("invoice_text") }}.'+ assert result == expected++ def test_optional_at_variable_with_trailing_dot(self):+ """Test @?variable pattern with trailing dot (punctuation)."""+ template = "Optional information: @?optional_data."+ result = preprocess_template(template)+ expected = 'Optional information: {% if optional_data %}{{ optional_data|tag("optional_data") }}{% endif %}.'+ assert result == expected++ def test_multiple_at_variables_with_trailing_dots(self):+ """Test multiple @variable patterns with trailing dots."""+ template = "Extract all articles from this invoice text: @invoice_text. Process the items: @item_list."+ result = preprocess_template(template)+ expected = 'Extract all articles from this invoice text: {{ invoice_text|tag("invoice_text") }}. Process the items: {{ item_list|tag("item_list") }}.'+ assert result == expected