_convert_to_message() — langchain Function Reference
Architecture documentation for the _convert_to_message() function in utils.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD fc0b8e91_c9e6_1868_e169_5885ffb3f1fb["_convert_to_message()"] 0b528c80_0ce7_1c74_8932_bc433bcb03c6["utils.py"] fc0b8e91_c9e6_1868_e169_5885ffb3f1fb -->|defined in| 0b528c80_0ce7_1c74_8932_bc433bcb03c6 7d8fdbaf_a57f_bad7_f47e_85c3fa1f78fe["convert_to_messages()"] 7d8fdbaf_a57f_bad7_f47e_85c3fa1f78fe -->|calls| fc0b8e91_c9e6_1868_e169_5885ffb3f1fb 8bf4a738_aa0e_7f54_cf71_2ba709475974["_create_message_from_message_type()"] fc0b8e91_c9e6_1868_e169_5885ffb3f1fb -->|calls| 8bf4a738_aa0e_7f54_cf71_2ba709475974 style fc0b8e91_c9e6_1868_e169_5885ffb3f1fb fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/messages/utils.py lines 675–732
def _convert_to_message(message: MessageLikeRepresentation) -> BaseMessage:
"""Instantiate a `Message` from a variety of message formats.
The message format can be one of the following:
- `BaseMessagePromptTemplate`
- `BaseMessage`
- 2-tuple of (role string, template); e.g., (`'human'`, `'{user_input}'`)
- dict: a message dict with role and content keys
- string: shorthand for (`'human'`, template); e.g., `'{user_input}'`
Args:
message: a representation of a message in one of the supported formats.
Returns:
An instance of a message or a message template.
Raises:
NotImplementedError: if the message type is not supported.
ValueError: if the message dict does not contain the required keys.
"""
if isinstance(message, BaseMessage):
message_ = message
elif isinstance(message, Sequence):
if isinstance(message, str):
message_ = _create_message_from_message_type("human", message)
else:
try:
message_type_str, template = message
except ValueError as e:
msg = "Message as a sequence must be (role string, template)"
raise NotImplementedError(msg) from e
message_ = _create_message_from_message_type(message_type_str, template)
elif isinstance(message, dict):
msg_kwargs = message.copy()
try:
try:
msg_type = msg_kwargs.pop("role")
except KeyError:
msg_type = msg_kwargs.pop("type")
# None msg content is not allowed
msg_content = msg_kwargs.pop("content") or ""
except KeyError as e:
msg = f"Message dict must contain 'role' and 'content' keys, got {message}"
msg = create_message(
message=msg, error_code=ErrorCode.MESSAGE_COERCION_FAILURE
)
raise ValueError(msg) from e
message_ = _create_message_from_message_type(
msg_type, msg_content, **msg_kwargs
)
else:
msg = f"Unsupported message type: {type(message)}"
msg = create_message(message=msg, error_code=ErrorCode.MESSAGE_COERCION_FAILURE)
raise NotImplementedError(msg)
return message_
Domain
Subdomains
Defined In
Called By
Source
Frequently Asked Questions
What does _convert_to_message() do?
_convert_to_message() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/utils.py.
Where is _convert_to_message() defined?
_convert_to_message() is defined in libs/core/langchain_core/messages/utils.py at line 675.
What does _convert_to_message() call?
_convert_to_message() calls 1 function(s): _create_message_from_message_type.
What calls _convert_to_message()?
_convert_to_message() is called by 1 function(s): convert_to_messages.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free