_create_template_from_message_type() — langchain Function Reference
Architecture documentation for the _create_template_from_message_type() function in chat.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD dee074bd_3404_de66_dbc9_422f215993ec["_create_template_from_message_type()"] d735cdf0_4dd6_b10d_3d65_7f753809639d["chat.py"] dee074bd_3404_de66_dbc9_422f215993ec -->|defined in| d735cdf0_4dd6_b10d_3d65_7f753809639d 0a946d27_710a_f7ff_e9e4_87561079dc25["_convert_to_message_template()"] 0a946d27_710a_f7ff_e9e4_87561079dc25 -->|calls| dee074bd_3404_de66_dbc9_422f215993ec 6966af16_ade2_de3e_4f06_f97eca43d8e0["from_template()"] dee074bd_3404_de66_dbc9_422f215993ec -->|calls| 6966af16_ade2_de3e_4f06_f97eca43d8e0 style dee074bd_3404_de66_dbc9_422f215993ec fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/prompts/chat.py lines 1330–1404
def _create_template_from_message_type(
message_type: str,
template: str | list,
template_format: PromptTemplateFormat = "f-string",
) -> BaseMessagePromptTemplate:
"""Create a message prompt template from a message type and template string.
Args:
message_type: The type of the message template (e.g., `'human'`, `'ai'`, etc.)
template: The template string.
template_format: Format of the template.
Returns:
A message prompt template of the appropriate type.
Raises:
ValueError: If unexpected message type.
"""
if message_type in {"human", "user"}:
message: BaseMessagePromptTemplate = HumanMessagePromptTemplate.from_template(
template, template_format=template_format
)
elif message_type in {"ai", "assistant"}:
message = AIMessagePromptTemplate.from_template(
cast("str", template), template_format=template_format
)
elif message_type == "system":
message = SystemMessagePromptTemplate.from_template(
cast("str", template), template_format=template_format
)
elif message_type == "placeholder":
if isinstance(template, str):
if template[0] != "{" or template[-1] != "}":
msg = (
f"Invalid placeholder template: {template}."
" Expected a variable name surrounded by curly braces."
)
raise ValueError(msg)
var_name = template[1:-1]
message = MessagesPlaceholder(variable_name=var_name, optional=True)
else:
try:
var_name_wrapped, is_optional = template
except ValueError as e:
msg = (
"Unexpected arguments for placeholder message type."
" Expected either a single string variable name"
" or a list of [variable_name: str, is_optional: bool]."
f" Got: {template}"
)
raise ValueError(msg) from e
if not isinstance(is_optional, bool):
msg = f"Expected is_optional to be a boolean. Got: {is_optional}"
raise ValueError(msg) # noqa: TRY004
if not isinstance(var_name_wrapped, str):
msg = f"Expected variable name to be a string. Got: {var_name_wrapped}"
raise ValueError(msg) # noqa: TRY004
if var_name_wrapped[0] != "{" or var_name_wrapped[-1] != "}":
msg = (
f"Invalid placeholder template: {var_name_wrapped}."
" Expected a variable name surrounded by curly braces."
)
raise ValueError(msg)
var_name = var_name_wrapped[1:-1]
message = MessagesPlaceholder(variable_name=var_name, optional=is_optional)
else:
msg = (
f"Unexpected message type: {message_type}. Use one of 'human',"
f" 'user', 'ai', 'assistant', or 'system'."
)
raise ValueError(msg)
return message
Domain
Subdomains
Defined In
Calls
Called By
Source
Frequently Asked Questions
What does _create_template_from_message_type() do?
_create_template_from_message_type() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/chat.py.
Where is _create_template_from_message_type() defined?
_create_template_from_message_type() is defined in libs/core/langchain_core/prompts/chat.py at line 1330.
What does _create_template_from_message_type() call?
_create_template_from_message_type() calls 1 function(s): from_template.
What calls _create_template_from_message_type()?
_create_template_from_message_type() is called by 1 function(s): _convert_to_message_template.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free