1. Two Ways to Call a Model
Remember "What's the Issue?" This is the fix. Pick your company in each way and you get the full code. The last three lines are the same in both.
Way 1a class for each company
install:
pip install langchain-google-genailangchain-groqlangchain-mistralailangchain-openaifrom dotenv import load_dotenv from langchain_google_genai import ChatGoogleGenerativeAIfrom langchain_groq import ChatGroqfrom langchain_mistralai import ChatMistralAIfrom langchain_openai import ChatOpenAI load_dotenv() model = ChatGoogleGenerativeAI(model="gemini-2.5-flash")model = ChatGroq(model="openai/gpt-oss-20b")model = ChatMistralAI(model="open-mistral-nemo")model = ChatOpenAI(model="gpt-4o-mini") response = model.invoke("What is the capital of India?") print(response.content)
- Import the model class from the company's own package:
langchain_google_genailangchain_groqlangchain_mistralailangchain_openai. - Create the model with the class ChatGoogleGenerativeAIChatGroqChatMistralAIChatOpenAI. The model name goes inside the brackets.
- Extra settings, like
temperature=0, go inside the same brackets. For a Gemini 3 model, read the answer withresponse.text. See What Comes Back.
Way 2one function for every company
install:
pip install langchain langchain-google-genailangchain-groqlangchain-mistralailangchain-openaifrom dotenv import load_dotenv from langchain.chat_models import init_chat_model load_dotenv() model = init_chat_model("google_genai:gemini-2.5-flash")model = init_chat_model("groq:openai/gpt-oss-20b")model = init_chat_model("mistralai:open-mistral-nemo")model = init_chat_model("openai:gpt-4o-mini") response = model.invoke("What is the capital of India?") print(response.content)
- Install two things:
langchain, which hasinit_chat_model, and the package of your company. - One import,
init_chat_model, is the same for every company. - Write the company and the model as one string:
"provider:model". For this company the provider isgoogle_genaigroqmistralaiopenai. - To switch company, change only this string. For a Gemini 3 model, read the answer with
response.text. See What Comes Back.
2. What Comes Back
When you call model.invoke(), LangChain gives back one standard object called an AIMessage. The object is the same for every company. What is inside content can be different.
response is the whole AIMessage.
| Model | What response.content is | Get the text |
|---|---|---|
gemini-2.5-flash | A string: 'The capital of India is **New Delhi**.' | response.content or response.text |
gemini-3.5-flash | A list of blocks: [{'type': 'text', 'text': 'The capital of India is **New Delhi**.', 'extras': {...}}] | response.text |
response.text gives only the text, whatever the shape of
content is.extras holds a signature, an encoded note about the model's thinking. You can ignore it.
3. Which Way Should You Use?
- Way 1: use it when you need settings that only one company has.
- Way 2: use it when you want to switch companies easily. This is our default in the course.
Code
Calling with LangChaincallingbylangchain.py · 30 linesShow code
First install the package:
pip install langchain langchain-mistralai langchain-google-genai python-dotenv1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | from dotenv import load_dotenv from langchain_mistralai import ChatMistralAI from langchain_google_genai import ChatGoogleGenerativeAI from langchain.chat_models import init_chat_model load_dotenv() # temperature = 0 -> same answer every time # temperature = 1 -> new answer every time # Way 1: a separate class for each company model1 = ChatMistralAI(model="open-mistral-nemo", temperature=0) model2 = ChatGoogleGenerativeAI(model="gemini-2.5-flash", temperature=0) # Way 2: one function for every company, "provider:model" model3 = init_chat_model("mistralai:open-mistral-nemo", temperature=1) model4 = init_chat_model("google_genai:gemini-2.5-flash", temperature=1) question = "Give me one name for a coffee shop. Only the name." res1 = model1.invoke(question) res2 = model2.invoke(question) res3 = model3.invoke(question) res4 = model4.invoke(question) print(res1.content) print(res2.content) print(res3.content) print(res4.content) |