79660912

Date: 2025-06-10 17:41:02
Score: 8.5 🚩
Natty:
Report link

I have the same problem when importing HuggingFaceEmmbeddings as well as OllamaEmbeddings:

ImportError: cannot import name 'convert_to_openai_image_block' from 'langchain_core.messages' (E:\U-DEVTOOLS\Python-3.11.9\Lib\site-packages\langchain_core\messages\__init__.py)

Which is rised in:

File E:\U-DEVTOOLS\Python-3.11.9\Lib\site-packages\langchain_core\language_models\chat_models.py:50
---> 50 from langchain_core.messages import (
     51     AIMessage,
     52     AnyMessage,
     53     BaseMessage,
     54     BaseMessageChunk,
     55     HumanMessage,
     56     convert_to_messages,
     57     convert_to_openai_image_block,
     58     is_data_content_block,
     59     message_chunk_to_message,
     60 )

Looking at the source code of this class (https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/language_models/chat_models.py:50), it imports convert_to_openai_image_block from the langchain_core.Messages class:

from langchain_core.messages import (
    [...]
    convert_to_messages,
    convert_to_openai_image_block,
    is_data_content_block,
    message_chunk_to_message,
)

And https://github.com/langchain-ai/langchain/blob/master/libs/core/langchain_core/messages/\__init_\_.py (:35) import convert_to_openai_image_block from content_blocks subclass and export it again :

    from langchain_core.messages.content_blocks import (
        convert_to_openai_data_block,
        convert_to_openai_image_block,
        is_data_content_block,
    )

I also discovered another Messages.py in \Python-3.11.9\Lib\site-packages\langchain\schema, also defining the same class, but exporting only a part of the methods. This looks like an interface.

When I try to load the OllamaEmbeddings interactively in my notebook, I have the same error. And when I remove the convert_to_openai_image_block function from the import statement:

from langchain_core.messages import AIMessage, BaseMessage, convert_to_messages
print(convert_to_messages)
from langchain_core import messages
print(messages)

<function convert_to_messages at 0x00000169A8185620>
<module 'langchain_core.messages' from 'E:\\U-DEVTOOLS\\Python-3.11.9\\Lib\\site-packages\\langchain_core\\messages\\__init__.py'>

Can someone explain what's going on ?

Reasons:
  • Blacklisted phrase (1): I have the same problem
  • RegEx Blacklisted phrase (2.5): Can someone explain what
  • RegEx Blacklisted phrase (1): I have the same error
  • Long answer (-1):
  • Has code block (-0.5):
  • Me too answer (2.5): I have the same problem
  • Me too answer (0): I have the same error
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: pmo78