AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

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.

mediumConfidence 85%LangchainChainsRetrievers

Origin Analysis

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'}}]`

Fixing Code Block

def pretty_repr(self, html: bool = False) -> str: from langchain_core.messages import message_to_dict content = self.content if isinstance(content, str): content_str = f'"{content}"' if not html else f"<pre>{content}</pre>" elif isinstance(content, list): items = [] for block in content: block_type = block.get("type", "") if block_type == "text": items.append(f"Text: {block.get('text', '')}") elif block_type == "image_url": image_url = block.get("image_url", {}) url = image_url.get("url", "") if isinstance(image_url, dict) else str(image_url) items.append(f"Image URL: {url}") elif block_type == "audio": audio = block.get("audio", {}) url = audio.get("url", "") if isinstance(audio, dict) else str(audio) items.append(f"Audio URL: {url}") elif block_type == "video": video = block.get("video", {}) url = video.get("url", "") if isinstance(video, dict) else str(video) items.append(f"Video URL: {url}") elif block_type == "reasoning": items.append(f"Reasoning: {block.get('reasoning', '')}") else: items.append(str(block)) content_str = ", ".join(items) if html: content_str = f"<pre>[{content_str}]</pre>" else: content_str = f"[{content_str}]" else: content_str = repr(content) return f"{self.__class__.__name__}(content={content_str})"
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.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state