The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
core(patch):fix partial_variables not working with SystemMessagePromptTemplate (#20711)
Guangdong Liubc7e32f315bc
diff --git a/libs/core/langchain_core/prompts/chat.py b/libs/core/langchain_core/prompts/chat.pyindex eea59202cf..b43c4b07e1 100644--- a/libs/core/langchain_core/prompts/chat.py+++ b/libs/core/langchain_core/prompts/chat.py@@ -407,6 +407,8 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):cls: Type[_StringImageMessagePromptTemplateT],template: Union[str, List[Union[str, _TextTemplateParam, _ImageTemplateParam]]],template_format: str = "f-string",+ *,+ partial_variables: Optional[Dict[str, Any]] = None,**kwargs: Any,) -> _StringImageMessagePromptTemplateT:"""Create a class from a string template.@@ -414,6 +416,7 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):Args:template: a template.template_format: format of the template.+ partial_variables: A dictionary of variables that can be used too partially.**kwargs: keyword arguments to pass to the constructor.Returns:@@ -421,10 +424,16 @@ class _StringImageMessagePromptTemplate(BaseMessagePromptTemplate):"""if isinstance(template, str):prompt: Union[StringPromptTemplate, List] = PromptTemplate.from_template(- template, template_format=template_format+ template,+ template_format=template_format,+ partial_variables=partial_variables,)return cls(prompt=prompt, **kwargs)elif isinstance(template, list):+ if (partial_variables is not None) and len(partial_variables) > 0:+ raise ValueError(+ "Partial variables are not supported for list of templates."+ )prompt = []for tmpl in template:if isinstance(tmpl, str) or isinstance(tmpl, dict) and "text" in tmpl:diff --git a/libs/core/tests/unit_tests/prompts/test_chat.py b/libs/core/tests/unit_tests/prompts/test_chat.pyindex 2cb19695e4..e5fddbb485 100644--- a/libs/core/tests/unit_tests/prompts/test_chat.py+++ b/libs/core/tests/unit_tests/prompts/test_chat.py@@ -99,6 +99,79 @@ def test_create_chat_prompt_template_from_template_partial() -> None:assert output_prompt.prompt == expected_prompt+def test_create_system_message_prompt_template_from_template_partial() -> None:+ """Create a system message prompt template with partials."""++ graph_creator_content = """+ Your instructions are:+ {instructions}+ History:+ {history}+ """+ json_prompt_instructions: dict = {}+ graph_analyst_template = SystemMessagePromptTemplate.from_template(+ template=graph_creator_content,+ input_variables=["history"],+ partial_variables={"instructions": json_prompt_instructions},+ )+ assert graph_analyst_template.format(history="history") == SystemMessage(+ content="\n Your instructions are:\n "+ " {}\n History:\n "+ "history\n "+ )+++def test_create_system_message_prompt_list_template() -> None:+ graph_creator_content1 = """+ This is the prompt for the first test:+ {variables}+ """+ graph_creator_content2 = """+ This is the prompt for the second test:+ {variables}+ """+ graph_analyst_template = SystemMessagePromptTemplate.from_template(+ template=[graph_creator_content1, graph_creator_content2],+ input_variables=["variables"],+ )+ assert graph_analyst_template.format(variables="foo") == SystemMessage(+ content=[+ {+ "type": "text",+ "text": "\n This is the prompt for the first test:\n foo\n ",+ },+ {+ "type": "text",+ "text": "\n This is the prompt for "+ "the second test:\n foo\n ",+ },+ ]+ )+++def test_create_system_message_prompt_list_template_partial_variables_not_null() -> (+ None+):+ graph_creator_content1 = """+ This is the prompt for the first test:+ {variables}+ """+ graph_creator_content2 = """+ This is the prompt for the second test:+ {variables}+ """++ try:+ graph_analyst_template = SystemMessagePromptTemplate.from_template(+ template=[graph_creator_content1, graph_creator_content2],+ input_variables=["variables"],+ partial_variables={"variables": "foo"},+ )+ graph_analyst_template.format(variables="foo")+ except ValueError as e:+ assert str(e) == "Partial variables are not supported for list of templates."++def test_message_prompt_template_from_template_file() -> None:expected = ChatMessagePromptTemplate(prompt=PromptTemplate(