Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(rag): validate that the hint template is renderable (#2965)
ANIRUDDHA ADAK904b11bb489e
diff --git a/src/agentscope/middleware/_rag.py b/src/agentscope/middleware/_rag.pyindex 3757965fc..63d91f9bc 100644--- a/src/agentscope/middleware/_rag.py+++ b/src/agentscope/middleware/_rag.py@@ -795,6 +795,12 @@ class RAGMiddleware(MiddlewareBase):"hint_template must contain exactly one '{context}' "f"placeholder; found {count}.",)+ try:+ value.format(context="")+ except (IndexError, KeyError) as e:+ raise ValueError(+ f"hint_template has an unknown placeholder: {e}.",+ ) from ereturn valuedef __init__(diff --git a/tests/middleware_rag_test.py b/tests/middleware_rag_test.pyindex c47629cda..7eed18630 100644--- a/tests/middleware_rag_test.py+++ b/tests/middleware_rag_test.py@@ -1205,3 +1205,26 @@ class SearchAcrossRerankTest(IsolatedAsyncioTestCase):[result.document_id for result in results],["doc-broad"],)+++class RAGHintTemplateValidationTest(IsolatedAsyncioTestCase):+ """``hint_template`` must be renderable, not merely contain a marker."""++ def test_unknown_placeholder_is_rejected(self) -> None:+ """A second, unknown placeholder must fail validation up front."""+ with self.assertRaises(ValueError):+ RAGMiddleware.Parameters(+ hint_template="A {context} B {doc_id}",+ )++ def test_valid_template_is_still_accepted(self) -> None:+ """The documented single-placeholder form keeps validating."""+ params = RAGMiddleware.Parameters(+ hint_template="Sources:\n{context}",+ )+ self.assertIn("{context}", params.hint_template)++ def test_missing_placeholder_is_still_rejected(self) -> None:+ """The pre-existing count check must keep working."""+ with self.assertRaises(ValueError):+ RAGMiddleware.Parameters(hint_template="no placeholder here")