Repository navigation
fix: make langchain imports compatible with langchain v2 - #11
Merged
Merged
Conversation
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>
Author
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
…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>
… '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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
agentlab-xrayand several LLM modules crash withModuleNotFoundError: No module named 'langchain.schema'because langchain v2 movedBaseMessage/HumanMessagetolangchain_core.messages.This PR updates three files to use a try/except import chain: try
langchain_core.messagesfirst → fall back tolangchain.schema→ gracefully degrade toNone. This matches the conditional import pattern already used elsewhere in the codebase (e.g., theelsebranches already set these toNone).Files changed:
agent_xray.py— import fix +Noneguards onisinstancechecksllm_utils.py— same import fix forLangchainBaseMessageandconvert_message_to_dict, plus aNoneguard onconvert_message_to_dictusage with a role-aware dict fallbacktracking.py— wrappedlangchain_community.callbacksimport in try/exceptKey details
LangchainBaseMessageandconvert_message_to_dictare imported independently (separate try/except blocks), so one can succeed while the other fails.convert_message_to_dictisNonebutLangchainBaseMessageis available,messages_to_dict()falls back to a manual role mapping (human→user,ai→assistant,system→system) viam.typeinstead of callingNone(m).Review & Testing Checklist for Human
role_mapcompleteness: The manual fallback inllm_utils.py:55mapshuman,ai, andsystemtypes. Other langchain message types (e.g.,tool,function) would pass through as-is viarole_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: Ifm.contentis 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).Nonehandling inagent_xray.py: WhenBaseMessage=None,format_chat_messagefalls through tostr(message)andsubmit_actionfalls through to theValueErrorraise. Confirm this is acceptable when langchain is not installed.agentlab-xraywith 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
langchainis not declared inpyproject.tomloruv.lock— it was always an undeclared optional dependency. This PR doesn't change that; it just stops the hard crash.Link to Devin session: https://app.devin.ai/sessions/991a2a652ca44d36a06d4b59397593d0
Requested by: @ShawnAzman