Skip to content

fix: make langchain imports compatible with langchain v2 - #11

Merged
ShawnAzman merged 3 commits into
mainfrom
devin/1775233809-fix-langchain-import
Apr 3, 2026
Merged

ShawnAzman merged 3 commits into
mainfrom
devin/1775233809-fix-langchain-import

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

Summary

agentlab-xray and several LLM modules crash with ModuleNotFoundError: No module named 'langchain.schema' because langchain v2 moved BaseMessage/HumanMessage to langchain_core.messages.

This PR updates three files to use a try/except import chain: try langchain_core.messages first → fall back to langchain.schema → gracefully degrade to None. This matches the conditional import pattern already used elsewhere in the codebase (e.g., the else branches already set these to None).

Files changed:

  • agent_xray.py — import fix + None guards on isinstance checks
  • llm_utils.py — same import fix for LangchainBaseMessage and convert_message_to_dict, plus a None guard on convert_message_to_dict usage with a role-aware dict fallback
  • tracking.py — wrapped langchain_community.callbacks import in try/except

Key details

  • LangchainBaseMessage and convert_message_to_dict are imported independently (separate try/except blocks), so one can succeed while the other fails.
  • When convert_message_to_dict is None but LangchainBaseMessage is available, messages_to_dict() falls back to a manual role mapping (human→user, ai→assistant, system→system) via m.type instead of calling None(m).

Review & Testing Checklist for Human

  • Fallback role_map completeness: The manual fallback in llm_utils.py:55 maps human, ai, and system types. Other langchain message types (e.g., tool, function) would pass through as-is via role_map.get(m.type, m.type). Verify this is acceptable or if additional mappings are needed.
  • str(m.content) in fallback may lose structured content: If m.content is a list (e.g., multimodal content blocks), str() will serialize it as a Python repr. This only applies in the rare fallback path (langchain_core installed without langchain_community).
  • Downstream None handling in agent_xray.py: When BaseMessage=None, format_chat_message falls through to str(message) and submit_action falls through to the ValueError raise. Confirm this is acceptable when langchain is not installed.
  • Test plan: Run agentlab-xray with and without langchain installed. With langchain: legacy message formats should still render correctly. Without: the app should launch and function for dict-based messages.

Notes

  • langchain is not declared in pyproject.toml or uv.lock — it was always an undeclared optional dependency. This PR doesn't change that; it just stops the hard crash.
  • All 68 tests pass locally (15 skipped, matching CI expectations).
  • No new tests cover the fallback paths specifically since the fallback is only exercised in unusual dependency configurations (langchain_core without langchain_community).

Link to Devin session: https://app.devin.ai/sessions/991a2a652ca44d36a06d4b59397593d0
Requested by: @ShawnAzman


Open with Devin

The langchain v2 package moved BaseMessage/HumanMessage from
langchain.schema to langchain_core.messages. This caused
agentlab-xray and other modules to crash with ModuleNotFoundError.

Updated all three affected files to try langchain_core.messages first,
fall back to langchain.schema, and gracefully handle the case where
neither is available - matching the existing conditional import pattern
used elsewhere in the codebase.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

devin-ai-integration[bot]

This comment was marked as resolved.

…ependently

Address Devin Review feedback: when langchain_core.messages is available
but langchain_community.adapters.openai is not, convert_message_to_dict
would be None. Added a None check with a fallback to manual dict
construction.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
devin-ai-integration[bot]

This comment was marked as resolved.

… 'user'

Maps langchain message types (human→user, ai→assistant, system→system)
to preserve correct roles when convert_message_to_dict is unavailable.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@ShawnAzman
ShawnAzman merged commit 4ce47b9 into main Apr 3, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant