ragas icon indicating copy to clipboard operation
ragas copied to clipboard

fix: resolve vertexai import error in langchain-community

Open riit3sh opened this issue 6 months ago • 1 comments

Issue Link / Problem Description

  • Description: Newer versions of langchain-community have sunset and removed the legacy Google Vertex AI modules. Because Ragas attempts a top-level import of these modules in src/ragas/llms/base.py, it causes a hard crash (ModuleNotFoundError) for anyone installing Ragas alongside a modern LangChain setup.

Traceback

>>> import ragas
Traceback (most recent call last):
  File "G:\test\ragas\src\ragas\llms\base.py", line 12, in <module>
    from langchain_community.chat_models.vertexai import ChatVertexAI
ModuleNotFoundError: No module named 'langchain_community.chat_models.vertexai'
Changes Made
Wrapped the ChatVertexAI and VertexAI imports inside src/ragas/llms/base.py in a try-except ImportError block.

Made the addition of these classes to the MULTIPLE_COMPLETION_SUPPORTED list conditional on VERTEXAI_AVAILABLE.

This maintains backward compatibility for legacy langchain-community users while preventing the crash on import for modern setups.

Code Review Checklist
[x] All tests pass (make test / uv run pytest)

[x] Code is formatted (make format / uv run ruff)

[x] Type checking passes (make type)

[ ] Documentation is updated (N/A)

[ ] Appropriate tests are included (N/A - proactive fix)

Screenshots/Examples (if applicable)
Verified that running import ragas in the interactive interpreter now succeeds cleanly without throwing an unhandled ModuleNotFoundError.

riit3sh avatar May 27 '26 06:05 riit3sh

✅ No documentation update needed — Internal bug fix that makes vertexai imports optional to handle ImportError. No user-facing API changes, no new features, and no changes to usage patterns that would require documentation updates.

github-actions[bot] avatar May 27 '26 06:05 github-actions[bot]

Tested this branch against my RAG evaluation pipeline.

Environment: ragas 0.1.dev1148+g18b47dc0d, Python 3.x, Windows

Before this fix, import ragas crashed with the same ModuleNotFoundError on langchain_community.chat_models.vertexai. After installing from this branch, import succeeds cleanly with no workaround needed.

Also ran a full evaluation to confirm nothing else broke:

  • bypass_n=True on LangchainLLMWrapper: works
  • LangchainEmbeddingsWrapper / embed_query for answer_relevancy: works
  • evaluate() with faithfulness + answer_relevancy completed successfully

Note: both LangchainLLMWrapper and LangchainEmbeddingsWrapper print deprecation warnings pointing to llm_factory/embedding_factory and ragas.metrics.collections — not a blocker, just flagging in case it's relevant to this PR's scope.

Thanks for the fix — this resolves the crash for me.

Meenatchisundari avatar Jun 14 '26 10:06 Meenatchisundari

Thank you @Meenatchisundari for testing!

Super happy the fix resolves the import crash for you too.

riit3sh avatar Jun 14 '26 10:06 riit3sh