AI Hints
Home›LangChain Tutorial›Tokens and Providers
LangChain · Lesson 11 of 120

Switching Providers

One interface.

⏱ 2 min read▶ Video at 10:43🎓 Free
Watch this lesson in the LangChain courseWatch on YouTube ↗Full LangChain course

One of LangChain's best features is that every model provider looks the same. Change one string, and your code talks to a different model, with no other changes.

Switching Providers
From the video: Switching Providers
  • Every provider looks the same
  • Change one string
  • No other changes

Provider strings

The model string is provider, colon, model name. Google gen AI for Gemini. Open AI for GPT models. Anthropic for Claude. And Ollama for free models running on your own computer. Each provider needs its own small package, and its own key.

ProviderExample stringPackage
Googlegoogle_genai:gemini-flash-lite-latestlangchain-google-genai
OpenAIopenai:gpt-5-minilangchain-openai
Anthropicanthropic:claude-sonnet-4-5langchain-anthropic
Ollama (local)ollama:llama3.2langchain-ollama

Code that works with any model

Because the interface is shared, we can write a function that takes the model name as a setting. Everything inside, invoke, the messages, and dot text, stays the same. Run it. Here it uses Gemini, but swapping the string is all it would take to use another provider.

any_model.pypython
from langchain.chat_models import init_chat_model

def ask(model_name, question):
    model = init_chat_model(model_name, temperature=0)
    return model.invoke(question).text

MODEL = "google_genai:gemini-flash-lite-latest"   # change me
print(ask(MODEL, "In five words, what is a library?"))
Output
A quiet repository of knowledge.

Summary

Every provider shares the same interface, so switching models is a one line change. Next, a quiz on the basics.

Key takeaways
  • provider:model
  • Same invoke, same text
  • Swap one string
Next up: Basics Quiz — test yourself