{"id":2622,"date":"2025-08-17T10:40:28","date_gmt":"2025-08-17T01:40:28","guid":{"rendered":"https:\/\/skanto.co.kr\/?p=2622"},"modified":"2025-08-17T10:44:44","modified_gmt":"2025-08-17T01:44:44","slug":"the-role-of-messagesplaceholder-in-langchain-js","status":"publish","type":"post","link":"https:\/\/skanto.co.kr\/?p=2622","title":{"rendered":"The role of MessagesPlaceholder in LangChain.js"},"content":{"rendered":"\n<p>The <code>MessagesPlaceHolder<\/code> in LangChain.js plays a crucial role in enabling dynamic and flexible construction of chat prompts by acting as a placeholder for a list of messages within a <code>ChatPromptTemplate<\/code>. Its primary functions are<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dynamic Message Insertion<\/h3>\n\n\n\n<p>It allows you to inject a list of BaseMessage objects (like <code>HumanMessage<\/code>, <code>AIMessage<\/code>, <code>SystemMessage<\/code>, etc) into a specific position within your chat prompt template <span style=\"text-decoration: underline;\">during runtime<\/span>. This is particularly useful for managing chat history, where the list of past messages needs to be included in the prompt for context.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Facilitating Chat History Management <\/h3>\n\n\n\n<p>When dealing with conversational AI, maintaining and including chat history is essential for the model to understand the ongoing dialogue. <code>MessagesPlaceholder<\/code> provides a clean and structured way to incorporate this history into your prompts without hardcoding individual messages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Enhancing Prompt Template Flexibility<\/h3>\n\n\n\n<p>Instead of defining every single message explicitly in the <code>ChatPromptTempate<\/code>, <code>MessagesPlaceholder<\/code> allows for a more modular approach. <span style=\"text-decoration: underline;\">You can define static system messages or initial prompts, and then use the placeholder to dynamically add the evolving conversation history or other lists of messages as needed<\/span>.<\/p>\n\n\n\n<p><span style=\"text-decoration: underline;\">In essence, MessagesPlaceholder empowers developers to create more adaptable and context-aware chat applications by providing a mechanism to dynamically populate chat prompts with message sequence.<\/span><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const systemPrompt = `\nYou are a supervisor tasked with managing a conversation between the following workers: {members}.\nGiven the following user request, respond with the worker to act next. Each worker will perform a\ntask and respond with their result and status. When finished, respond with FINISH.`;\n\nconst prompt = ChatPromptTemplate.fromMessages(&#91;\n  &#91;'system', systemPrompt],\n  <span style=\"text-decoration: underline;\">new MessagesPlaceholder('messges')<\/span>,\n  &#91;'human', 'Given the conversation above, who should ac next? Or should we FINISH? Select one of: {options}']\n]);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The MessagesPlaceHolder in LangChain.js plays a crucial role in enabling dynamic and flexible construction of chat prompts by acting as a placeholder for a list of messages within a ChatPromptTemplate. Its primary functions are Dynamic Message Insertion It allows you to inject a list of BaseMessage objects (like HumanMessage, AIMessage, SystemMessage, etc) into a specific position within your chat prompt template during runtime. This is particularly useful for managing chat history, where the list of past messages needs to be&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/skanto.co.kr\/?p=2622\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,7],"tags":[48,228,258],"class_list":["post-2622","post","type-post","status-publish","format-standard","hentry","category-sw-development","category-7","tag-ai","tag-langchain","tag-prompt"],"_links":{"self":[{"href":"https:\/\/skanto.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/skanto.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/skanto.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/skanto.co.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/skanto.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2622"}],"version-history":[{"count":2,"href":"https:\/\/skanto.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2622\/revisions"}],"predecessor-version":[{"id":2625,"href":"https:\/\/skanto.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/2622\/revisions\/2625"}],"wp:attachment":[{"href":"https:\/\/skanto.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/skanto.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/skanto.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}