Why should we use SystemMessage and HumanMessage class in Langchain

Ajay Gupta · Intermediate ·🧠 Large Language Models ·2y ago

About this lesson

Explore the benefits of using SystemMessage and HumanMessage classes in Langchain for enhanced AI interactions Understand how to set up ChatOpenAI with key configurations: model selection, temperature control, and API key management Step-by-step guide on leveraging dotenv for secure API key handling Learn the importance of SystemMessage and HumanMessage classes over plain tuples for message handling Discover how these classes enforce string validation and type safety, eliminating the need for tuple structures Ensure consistency and improved readability in large codebases with structured messaging Enhance interoperability by maintaining a consistent message structure regardless of the underlying language model Practical example: Define and invoke a chat model with pre-defined system and human messages Perfect for developers aiming to optimize their Langchain workflows and AI application development Follow along to improve the robustness and clarity of your Langchain projects Connect with me on LinkedIn Post your questions and suggestions in the comments section below. #genai #openai #openaichat #openaiapi #openaichatgpt #langchain #python #pythonprogramming #llm #llms #chatgpt #chatbot

Full Transcript

welcome to the channel so this is in continuation in our of our last video where we understood uh a little bit of standardization where we use EnV file instead of directly hardcoding the opena API key so in this particular video we'll try and understand what our system message and human message class provided by Lang chain and why we should be using them instead of directly uh using the tles system and human that we were doing till now so uh we'll first see the example of how we can use system message in human message so up until now I was only using Lang chain _ open but now I've uh installed Lang chain core package as well which provides me human message and system message class after importing them what I did was earlier we were creating tles right um here if you see messages I've cre tles for system and human but now what we are doing is we using the classes that we imported from lch which are system message and we passed in the content of that in the human message we passed in the human message so the messages remain the same only uh the thing that changes is how we are passing them into our llm right and then uh like before we pass in the messages to the llm and we get similar response right right you can run multiple uh instances of it and you'll get the you'll get response as per your question now why should you be using system message and human message instead of plain tles right I mean this there's nothing wrong in this and it looks very similar to what we are doing now using U System message in human message class so there are few uh uh few things that uh these classes provide one is validation type safety so these classes ensure uh that you are using a string and not any other data type which is not supported by openai or any other llm and also it provides type safety uh you don't need to use toles you might uh miss something in the syntax which which could lead to errors so this provides validation and type safety next is uh when you are working with a larger code base where you need to use multiple instance is of system message and human message this provides you a consistent way of doing that right but uh earlier it was just tle somebody could uh just write human somebody could could uh make some mistake so this these classes provide you a consistent way of doing this again and again right obviously this improves readability because um uh now we know what message we need to pass and last but not the least is interoperability this is the way how you are um passing in the messages in open a right you can pass in system you can pass in human but what if tomorrow you decide to use a different uh llm all together and it has a different syntax of passing these messages instead of worrying about all of those details you can just use these system message and human message class they will take care of um supporting other llms but for you you will still be using um these two classes so you don't need to change your code so this is why we should be using this classes this is another way of uh standardizing our code this is how you would do in a larger code base uh in an Enterprise so that's it for this video uh please subscribe to the channel if you're new thank you for watching have a good day

Original Description

Explore the benefits of using SystemMessage and HumanMessage classes in Langchain for enhanced AI interactions Understand how to set up ChatOpenAI with key configurations: model selection, temperature control, and API key management Step-by-step guide on leveraging dotenv for secure API key handling Learn the importance of SystemMessage and HumanMessage classes over plain tuples for message handling Discover how these classes enforce string validation and type safety, eliminating the need for tuple structures Ensure consistency and improved readability in large codebases with structured messaging Enhance interoperability by maintaining a consistent message structure regardless of the underlying language model Practical example: Define and invoke a chat model with pre-defined system and human messages Perfect for developers aiming to optimize their Langchain workflows and AI application development Follow along to improve the robustness and clarity of your Langchain projects Connect with me on LinkedIn Post your questions and suggestions in the comments section below. #genai #openai #openaichat #openaiapi #openaichatgpt #langchain #python #pythonprogramming #llm #llms #chatgpt #chatbot
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

📰
Intelligence is not designed, but evolved through feedback
Evolve AI intelligence through environmental feedback, not design, for more adaptive cognitive structures
Medium · LLM
📰
Le paradoxe de l'IA open-source en 2026 : Meta se ferme, Moonshot s'ouvre, et la frontière se déplace de la Californie à Pékin
Meta shifts to proprietary AI models while Chinese labs like Moonshot and DeepSeek open-source their best work, changing the AI landscape
Dev.to AI
📰
The Open-Source AI Paradox of 2026: Meta Closes, Moonshot Opens, and the Frontier Shifts From California to Beijing
Meta shifts to proprietary AI models while Chinese labs open-source their best work, changing the AI landscape
Dev.to AI
📰
'World Models' Will Be the Next Buzzword. The Man Saying That Just Raised $1B to Build One.
Learn about 'world models' and their predicted impact on the tech industry, with a $1B seed round invested in AMI Labs to build one
Dev.to AI
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →