Add __str__ methods to content blocks and message types#833
Open
yuribodo wants to merge 1 commit intoanthropics:mainfrom
Open
Add __str__ methods to content blocks and message types#833yuribodo wants to merge 1 commit intoanthropics:mainfrom
yuribodo wants to merge 1 commit intoanthropics:mainfrom
Conversation
Makes messages pretty-printable by default, so users can call print(msg) without writing custom display helpers like the ones in examples/. - TextBlock prints text verbatim; ThinkingBlock, ToolUseBlock, and ToolResultBlock print with a labeled prefix. - ToolUseBlock.input and ToolResultBlock.content are truncated to 200 chars so a large Write/Edit payload doesn't blow up terminal output. - UserMessage / AssistantMessage join their content blocks; empty content yields "User:" / "Claude:" without a trailing space. - SystemMessage shows its subtype. TaskStartedMessage, TaskProgressMessage, and TaskNotificationMessage override it to surface their description/status/summary instead of inheriting the lossy parent format. - StreamEvent shows the event type; RateLimitEvent shows status, window, and utilization. - ResultMessage surfaces duration and cost when available. __repr__ remains the dataclass default so debugging output (REPL, logging, pytest assertion reprs) is unchanged — this PR adds __str__ only, per Python's str-for-users / repr-for-developers convention. Closes anthropics#78
86cf611 to
edc4522
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #78. Adds
__str__methods to user-facing content-block and message dataclasses soprint(msg)produces readable output out of the box, removing the need for customdisplay_messagehelpers like the one inexamples/streaming_mode.py.Before
Users had to write their own
isinstanceladder:After
Covered types
TextBlock,ThinkingBlock,ToolUseBlock,ToolResultBlockUserMessage,AssistantMessage,SystemMessage,ResultMessageTaskStartedMessage,TaskProgressMessage,TaskNotificationMessage(override the parent__str__so their description / status / summary isn't lost)StreamEvent,RateLimitEventHook-related types and MCP status types are
TypedDict, not dataclasses, so they're out of scope here.Design notes
__str__only — the dataclass-generated__repr__is left alone. Python convention isstr()for end users,repr()for developers, and keeping the default repr preserves useful debugging output in the REPL,logging, and pytest assertion messages. The issue title says "reprs" but the body clearly asks for print-friendly display, so__str__felt like the conservative call. Happy to switch if maintainers prefer overriding__repr__instead.ToolUseBlock.inputandToolResultBlock.contentare truncated to 200 chars (with...) so a large Write/Edit payload doesn't blow up terminal output. The dataclass__repr__still renders the full payload for debugging.UserMessage/AssistantMessagewith no content render as"User:"/"Claude:"— no trailing space.Nonecontent onToolResultBlock: renders as"[Tool result]", not"[Tool result] None".Test plan
python -m pytest tests/— 510 passed (26 new inTestMessageStr, covering truncation, empty/None content, mixed-block ordering, task subclasses, stream events, rate-limit events)python -m ruff check src/ tests/— cleanpython -m ruff format --check src/ tests/— cleanpython -m mypy src/— no issues