ChatOpenAI is a LangChain class for connecting an application to OpenAI chat models. It provides an interface similar to an OpenAI API client and can be used as the language model in a LangGraph chatbot. section 10.2.3
Import ChatOpenAI from langchain_openai and initialize it with a model name and an OpenAI API key. In a graph node, call the model’s invoke method with the conversation messages; the returned AI message can then be added to the graph state. section 10.2.3
Listing 10.1 An example of a graph
from langgraph.graph import START, END, StateGraph
from langchain_core.messages import AnyMessage, HumanMessage
from langchain_openai import ChatOpenAI
from typing import TypedDict
llm = ChatOpenAI(model_name="gpt-5.1", openai_api_key="sk-proj-...")
class MyGraphState(TypedDict):
messages: list[AnyMessage]
builder = StateGraph(MyGraphState)
def assistant_node(state):
messages = state["messages"]
ai_response_message = llm.invoke(messages)
return {"messages": messages + [ai_response_message]}
builder.add_node("assistant", assistant_node)
builder.add_edge(START, "assistant")
builder.add_edge("assistant", END)
graph = builder.compile()
input_message = input("Talk to the bot: ")
initial_state = {"messages": [HumanMessage(content=input_message)]}
final_state = graph.invoke(initial_state)
print("Bot:\n" + final_state["messages"][-1].content)
This Python example connects ChatOpenAI to a one-node graph, sends a user message, and prints the response. Replace the placeholder with an actual API key. section 10.2.3
A chatbot can also configure max_tokens when creating ChatOpenAI and retrieve its API key from application secrets rather than placing it directly in the model initialization. section 10.2.3

