-
Notifications
You must be signed in to change notification settings - Fork 2.9k
feat(core): Add BaseMessage.toFormattedString()
#9228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hntrl
merged 6 commits into
langchain-ai:main
from
pokey:pokey/featcore-add-prettyprint-method-to-basemessage
Oct 22, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c5ffc79
feat(core): Add `prettyPrint` method to `BaseMessage`
pokey 3e3534e
prettyPrint => toFormattedString
pokey da40172
Create metal-hats-camp.md
hntrl e24f3fb
chore: hoist pretty print logic
hntrl 6e1d2d0
cr
hntrl 5688d88
cr
hntrl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@langchain/core": patch | ||
| --- | ||
|
|
||
| Add `BaseMessage.toFormattedString()` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { type BaseMessage } from "./base.js"; | ||
| import { type AIMessage } from "./ai.js"; | ||
| import { type ToolMessage } from "./tool.js"; | ||
|
|
||
| export type MessageStringFormat = "pretty"; | ||
|
|
||
| export function convertToFormattedString( | ||
| message: BaseMessage, | ||
| format: MessageStringFormat = "pretty" | ||
| ): string { | ||
| if (format === "pretty") return convertToPrettyString(message); | ||
| return JSON.stringify(message); | ||
| } | ||
|
|
||
| function convertToPrettyString(message: BaseMessage): string { | ||
| const lines: string[] = []; | ||
| const title = ` ${ | ||
| message.type.charAt(0).toUpperCase() + message.type.slice(1) | ||
| } Message `; | ||
| const sepLen = Math.floor((80 - title.length) / 2); | ||
| const sep = "=".repeat(sepLen); | ||
| const secondSep = title.length % 2 === 0 ? sep : `${sep}=`; | ||
| lines.push(`${sep}${title}${secondSep}`); | ||
|
|
||
| // Add message type specific details | ||
| if (message.type === "ai") { | ||
| const aiMessage = message as AIMessage; | ||
| if (aiMessage.tool_calls && aiMessage.tool_calls.length > 0) { | ||
| lines.push("Tool Calls:"); | ||
| for (const tc of aiMessage.tool_calls) { | ||
| lines.push(` ${tc.name} (${tc.id})`); | ||
| lines.push(` Call ID: ${tc.id}`); | ||
| lines.push(" Args:"); | ||
| for (const [key, value] of Object.entries(tc.args)) { | ||
| lines.push(` ${key}: ${value}`); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (message.type === "tool") { | ||
| const toolMessage = message as ToolMessage; | ||
| if (toolMessage.name) { | ||
| lines.push(`Name: ${toolMessage.name}`); | ||
| } | ||
| } | ||
|
|
||
| // Add content if it's a string and not empty | ||
| if (typeof message.content === "string" && message.content.trim()) { | ||
| if (lines.length > 1) { | ||
| lines.push(""); // blank line before content | ||
| } | ||
| lines.push(message.content); | ||
| } | ||
|
|
||
| return lines.join("\n"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think according to semver this would be
minor, because it is adding something to public api? Admittedly it is a small thing we're adding; not sure how strictly we're adhering to semverThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think you're the first of us to ask that Q post v1! Will confer and let you know