Creating a ChatOpenAI instance
Why this matters
Every LangChain application starts with a language model instance: this is how you tell LangChain which LLM backend to use and configure its behavior (temperature, model version, token limits). Without this, you cannot build chains, invoke prompts, or generate any output.
Explanation
What it is: ChatOpenAI is a LangChain wrapper around OpenAI's chat completion API. It abstracts away HTTP calls and response parsing so you interact with the LLM through a consistent Python interface.
How it works mechanically: When you instantiate ChatOpenAI(model="gpt-4o", temperature=0.7), you're creating a Python object that stores your API credentials (read from environment variables by default), your model choice, and hyperparameters like temperature and max_tokens. When you later call .invoke() or .stream() on a chain using this instance, it makes authenticated HTTPS requests to OpenAI's endpoint and returns structured responses as LangChain message objects.
When to use it: Use this whenever you want GPT models (4o, 4-turbo, 3.5-turbo, etc.) in your LangChain application. This is the standard entry point for OpenAI in modern LangChain code (1.0+).
Analogy
Think of ChatOpenAI as a translator at a conference. You hand it your message in Python, it speaks fluent HTTP/OpenAI-API, gets the response back, and translates it into Python objects you can work with. You never touch the raw API: ChatOpenAI does that translation work.
Code
import os
from langchain_openai import ChatOpenAI
os.environ['OPENAI_API_KEY'] = 'sk-your-api-key-here'
model = ChatOpenAI(
model='gpt-4o',
temperature=0.7,
max_tokens=500
)
print(type(model))
print(f"Model name: {model.model_name}")
print(f"Temperature: {model.temperature}") <class 'langchain_openai.chat_models.ChatOpenAI'> Model name: gpt-4o Temperature: 0.7
What just happened?
You imported ChatOpenAI from langchain_openai (the provider-specific package, not core langchain). You created an instance with gpt-4o as the model, set temperature to 0.7 (more creative), and max_tokens to 500. You then printed the object type and verified the stored configuration: no API call happened yet because you haven't invoked the model.
Common gotcha
The most common mistake is forgetting to set OPENAI_API_KEY in your environment, or importing from the wrong package. Old code uses from langchain.chat_models import ChatOpenAI: that's deprecated in langchain 1.2.x. The new way is from langchain_openai import ChatOpenAI. If you use the old import, you'll get a ModuleNotFoundError because the class was moved to the separate langchain-openai package.
Error recovery
ModuleNotFoundError: No module named 'langchain.chat_models'AuthenticationError: Incorrect API key providedValueError: model parameter not recognizedExperienced dev note
Temperature and max_tokens feel like LLM tweaks, but they're actually critical for production reliability. Temperature=0 makes the model deterministic (good for structured tasks like parsing); temperature=1+ makes it creative (good for brainstorming). Set max_tokens to prevent runaway API costs: a single call can cost $0.30+ if you accidentally request 2M tokens. Also: never hardcode your API key in source code; always use environment variables or a secrets manager. In production, rotate your keys regularly and monitor usage with OpenAI's dashboard.
Check your understanding
Why does importing ChatOpenAI from langchain.chat_models fail in langchain 1.2.x, and what does that tell you about how LangChain is organized as a library?
Show answer hint
A correct answer explains that langchain 1.0+ split provider-specific code (OpenAI, Anthropic, etc.) into separate packages to reduce bloat and dependencies, so ChatOpenAI lives in langchain-openai now. This also implies that different LLM providers each get their own package.