Improve BaseMessage.Pretty_repr For Multimodal Content
BaseMessage.pretty_repr currently outputs raw Python lists/dicts for multimodal content, making debugging difficult. A human-readable formatting for text, image, audio, video, and reasoning blocks is needed.
The pretty_repr method in BaseMessage only handles string content; it does not recursively format list-based multimodal content blocks, causing the default repr of lists and dictionaries to be printed.
Define a HumanMessage with a list of content blocks including text and image_url, then call msg.pretty_print(). Example:
```python
from langchain_core.messages import HumanMessage
msg = HumanMessage(content=[
{"type": "text", "text": "What is in this image?"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.png"}}
])
msg.pretty_print()
```
Observed output: `[{'type': 'text', 'text': 'What is in this image?'}, {'type': 'image_url', 'image_url': {'url': 'https://example.com/image.png'}}]`
The fix adds a conditional branch in pretty_repr to handle list-type content. It iterates through each content block and extracts a readable summary based on its type (text, image_url, audio, video, reasoning). This improves debugging output without altering the underlying message data.
Edge Case Audit
This patch assumes each content block is a dictionary with a 'type' key. If malformed blocks or custom types appear, the .get() calls prevent KeyError but may still produce misleading output. Additional error handling may be needed for production robustness. HTML mode does not escape user-provided text, which could lead to XSS if used insecurely. The fix only affects pretty_repr and should be safe to roll back by reverting to the original method.