Skip to content

The agent engineering platform.

docs(core): add examples for `pretty_repr`, `pretty_print` (#34968)

Mason Daugherty3aca3fbebe64
diff --git a/libs/core/langchain_core/messages/ai.py b/libs/core/langchain_core/messages/ai.py
index 4b64b4b212..92bac634d6 100644
--- a/libs/core/langchain_core/messages/ai.py
+++ b/libs/core/langchain_core/messages/ai.py
@@ -355,7 +355,31 @@ class AIMessage(BaseMessage):
Returns:
A pretty representation of the message.
- """
+ Example:
+ ```python
+ from langchain_core.messages import AIMessage
+
+ msg = AIMessage(
+ content="Let me check the weather.",
+ tool_calls=[
+ {"name": "get_weather", "args": {"city": "Paris"}, "id": "1"}
+ ],
+ )
+ ```
+
+ Results in:
+ ```python
+ >>> print(msg.pretty_repr())
+ ================================== Ai Message ==================================
+
+ Let me check the weather.
+ Tool Calls:
+ get_weather (1)
+ Call ID: 1
+ Args:
+ city: Paris
+ ```
+ """ # noqa: E501
base = super().pretty_repr(html=html)
lines = []
diff --git a/libs/core/langchain_core/messages/base.py b/libs/core/langchain_core/messages/base.py
index 1c52af9732..2b0e998c70 100644
--- a/libs/core/langchain_core/messages/base.py
+++ b/libs/core/langchain_core/messages/base.py
@@ -319,7 +319,22 @@ class BaseMessage(Serializable):
Returns:
A pretty representation of the message.
- """
+ Example:
+ ```python
+ from langchain_core.messages import HumanMessage
+
+ msg = HumanMessage(content="What is the capital of France?")
+ print(msg.pretty_repr())
+ ```
+
+ Results in:
+
+ ```txt
+ ================================ Human Message =================================
+
+ What is the capital of France?
+ ```
+ """ # noqa: E501
title = get_msg_title_repr(self.type.title() + " Message", bold=html)
# TODO: handle non-string content.
if self.name is not None:
@@ -327,7 +342,24 @@ class BaseMessage(Serializable):
return f"{title}\n\n{self.content}"
def pretty_print(self) -> None:
- """Print a pretty representation of the message."""
+ """Print a pretty representation of the message.
+
+ Example:
+ ```python
+ from langchain_core.messages import AIMessage
+
+ msg = AIMessage(content="The capital of France is Paris.")
+ msg.pretty_print()
+ ```
+
+ Results in:
+
+ ```txt
+ ================================== Ai Message ==================================
+
+ The capital of France is Paris.
+ ```
+ """ # noqa: E501
print(self.pretty_repr(html=is_interactive_env())) # noqa: T201