in supporting-blog-content/langraph-retrieval-agent-template-demo/src/retrieval_graph/utils.py [0:0]
def get_message_text(msg: AnyMessage) -> str:
"""Get the text content of a message.
This function extracts the text content from various message formats.
Args:
msg (AnyMessage): The message object to extract text from.
Returns:
str: The extracted text content of the message.
Examples:
>>> from langchain_core.messages import HumanMessage
>>> get_message_text(HumanMessage(content="Hello"))
'Hello'
>>> get_message_text(HumanMessage(content={"text": "World"}))
'World'
>>> get_message_text(HumanMessage(content=[{"text": "Hello"}, " ", {"text": "World"}]))
'Hello World'
"""
content = msg.content
if isinstance(content, str):
return content
elif isinstance(content, dict):
return content.get("text", "")
else:
txts = [c if isinstance(c, str) else (c.get("text") or "") for c in content]
return "".join(txts).strip()