Skip to content

The agent engineering platform.

fix(core): fix nested mustache variable extraction and update docs (#34872)

Nandana Dileepef067078b89a
diff --git a/libs/core/langchain_core/prompts/string.py b/libs/core/langchain_core/prompts/string.py
index e0eb642306..a7e1263544 100644
--- a/libs/core/langchain_core/prompts/string.py
+++ b/libs/core/langchain_core/prompts/string.py
@@ -258,7 +258,7 @@ def get_template_variables(template: str, template_format: str) -> list[str]:
template: The template string.
template_format: The template format.
- Should be one of `'f-string'` or `'jinja2'`.
+ Should be one of `'f-string'`, `'mustache'` or `'jinja2'`.
Returns:
The variables from the template.
diff --git a/libs/core/tests/unit_tests/prompts/test_string.py b/libs/core/tests/unit_tests/prompts/test_string.py
index 96c573c72f..c338a12822 100644
--- a/libs/core/tests/unit_tests/prompts/test_string.py
+++ b/libs/core/tests/unit_tests/prompts/test_string.py
@@ -1,7 +1,7 @@
import pytest
from packaging import version
-from langchain_core.prompts.string import mustache_schema
+from langchain_core.prompts.string import get_template_variables, mustache_schema
from langchain_core.utils.pydantic import PYDANTIC_VERSION
PYDANTIC_VERSION_AT_LEAST_29 = version.parse("2.9") <= PYDANTIC_VERSION
@@ -30,3 +30,12 @@ def test_mustache_schema_parent_child() -> None:
}
actual = mustache_schema(template).model_json_schema()
assert expected == actual
+
+
+def test_get_template_variables_mustache_nested() -> None:
+ template = "Hello {{user.name}}, your role is {{user.role}}"
+ template_format = "mustache"
+ # Returns only the top-level key for mustache templates
+ expected = ["user"]
+ actual = get_template_variables(template, template_format)
+ assert actual == expected