All jobs

Fix "ImportError: cannot import name 'ChatOpenAI' from 'langchain.chat_models'" in LangChain 1.0

Yes. An agent maps every langchain import to the package that holds it now, adds those packages, and proves the app imports and splits its documents.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads the imports and the dependency file together, because this error is the first of ten. In AIAnytime/Question-Answer-Generation-App, a FastAPI app that turns a PDF into a question-and-answer CSV, requirements.txt lists langchain, openai, pypdf, tiktoken, aiofiles, fastapi, uvicorn, jinja2, python-multipart and PyPDF2, none pinned, and a clean install today resolves langchain 1.4.9 with langchain-core 1.7.6, openai 3.46.0 and fastapi 0.162.0. Lines 6 to 15 of app.py import ChatOpenAI from langchain.chat_models, QAGenerationChain and RetrievalQA from langchain.chains, TokenTextSplitter, Document, PyPDFLoader, PromptTemplate, OpenAIEmbeddings, FAISS and load_summarize_chain from the old top-level modules. python -c 'import app' fails on line 6; script.py has the same ten lines.

Then it maps each name to where it lives in the split packages, by installing them and importing. langchain.chat_models still exists in 1.x, holding init_chat_model and no providers; langchain.chains, langchain.text_splitter, langchain.docstore, langchain.document_loaders, langchain.prompts, langchain.vectorstores, langchain.memory, langchain.schema and langchain.llms are gone, each a ModuleNotFoundError. The homes that import cleanly in the sandbox: ChatOpenAI and OpenAIEmbeddings from langchain_openai 1.7.4; QAGenerationChain, RetrievalQA and langchain_classic.chains.summarize.load_summarize_chain from langchain-classic 1.0.8; TokenTextSplitter from langchain_text_splitters 1.1.3; Document and PromptTemplate from langchain_core.documents and langchain_core.prompts; PyPDFLoader and FAISS from langchain_community 0.4.2, which no longer exports ChatOpenAI either; faiss-cpu 1.15.1 for the store.

The agent rewrites the ten imports in both files, adds the five packages to requirements.txt, and runs what it can without a model key, since app.py sets OPENAI_API_KEY to an empty string on line 31 and the pipeline needs a real one. On the branch the module imports, the FastAPI app lists its /, /upload and /analyze routes, and file_processing('SDG.pdf') on the bundled PDF returns one question chunk from the splitter set to 10000 tokens and four answer chunks from the one set to 1000. QAGenerationChain.from_llm and RetrievalQA.from_chain_type, the two constructors llm_pipeline calls, exist on the classic package.

It also reads what the classic package says about the next break. Chain.run, which llm_pipeline and get_csv call, is marked deprecated since 0.1.0 with invoke as the alternative and removal planned for 2.0.0. The agent leaves those calls alone, lists them in the pull request with the line numbers, and keeps the change to what makes the app import again.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "This app fails with \"ImportError: cannot import name 'ChatOpenAI' from 'langchain.chat_models'\". List every import from langchain in the project with file and line. Read the dependency files and say which langchain and langchain-core a clean install resolves. For each imported name, find by installing and importing which package exports it now (langchain-openai, langchain-classic, langchain-community, langchain-text-splitters, langchain-core) and record any module that no longer exists. Rewrite the imports to those packages, add each package to the dependency file, and change nothing else. Prove it without a model key: import the entry module in a fresh environment, list the app's routes, and run the document loading and splitting on a file in the repository, reporting the chunk counts. Note every call the new packages mark as deprecated, with the alternative they name, but do not change them. Open a pull request with the import map, the new dependencies and the outputs.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
  1. Get an API key. In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
  2. Send the request. POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
  3. Read what came back. Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

What comes back

A pull request that changes ten import lines in app.py and ten in script.py, and adds langchain-openai, langchain-classic, langchain-community, langchain-text-splitters and faiss-cpu to requirements.txt. The description holds a table with one row per imported name: the old module, whether it still exists in langchain 1.x, the package and module it comes from now, and the version that was installed. Below that are the route list, the chunk counts from the bundled PDF, and the list of run calls with the deprecation text.

A reviewer checks that every name in the table was imported rather than guessed, which the table's installed versions show, and that nothing in llm_pipeline changed. If the team has a key, the reviewer runs /analyze once on a small PDF after merging; the sandbox had none, and the pull request says the model calls were not exercised.

The pull request does not pin langchain, but says that langchain>=1 is the floor the new import paths assume.

Run it on every failed deploy or container build

LangChain 1.0 reaches an unpinned project at the next image rebuild, so the failing build or the first request after deploy is the alert. A build job that can run a step on failure posts the log tail to a Rush webhook trigger's URL with the trigger's secret in Authorization: Bearer, and the automation's prompt tells the agent to start from the first ImportError or ModuleNotFoundError naming langchain.

A host that only sends its own signed notification needs a handler on your server that receives it and forwards the log. Have the handler send an Idempotency-Key header to the trigger's URL built from the build id, so a retried delivery starts one run per failed build.

The use case this job follows, with its automation and trigger: Fix a bug from an alert.

Why run it on Rush?

Ten names moved to five packages, and the only reliable way to know where each went is to install the packages and import. A Rush sandbox does that in minutes, from a clean environment that resolves the same releases a rebuild would, and the pull request carries the versions that were actually imported, so nobody has to trust a migration table from memory.

The run also draws the line between what it proved and what it could not. The imports, the routes and the splitting are shown from real commands; the model calls needed a key the sandbox did not have, and the pull request says so instead of implying an end-to-end run.

Frequently asked questions

Where did ChatOpenAI go?

To the langchain-openai package, as from langchain_openai import ChatOpenAI, which also exports OpenAIEmbeddings. The langchain.chat_models module still exists in 1.x but holds only init_chat_model, and langchain_community.chat_models no longer exports it either.

What about the chains, RetrievalQA and load_summarize_chain?

They moved to the langchain-classic package: langchain_classic.chains for QAGenerationChain and RetrievalQA, langchain_classic.chains.summarize for load_summarize_chain. Their run method is marked deprecated in favour of invoke, and the pull request lists each call without changing it.

Can I pin langchain below 1 instead?

Yes, as a stopgap, and the pull request names the pin. It also pins langchain-core and the provider packages to the old line, so a project that wants a current openai client ends up fighting the resolver. The import rewrite is twenty lines and leaves the project on supported releases.

Does the agent need an OpenAI key?

No. It proves the imports, the routes and the PDF loading and splitting, which need no key, and says that the question generation and retrieval were not run. If you give the run a key as a Project secret, it can run one small PDF through /analyze as well.

More LangChain jobs

More jobs like this: Fix a bug from an alert

Keep reading