feat: add custom repr to message and content block types#727
Open
javierdejesusda wants to merge 2 commits intoanthropics:mainfrom
Open
feat: add custom repr to message and content block types#727javierdejesusda wants to merge 2 commits intoanthropics:mainfrom
javierdejesusda wants to merge 2 commits intoanthropics:mainfrom
Conversation
Add concise __repr__ methods to 14 dataclass types so print(message) produces readable output without helper functions. Uses !r formatting for proper escaping and _truncate helper for 80-char limit.
fd694f9 to
802f338
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
__repr__methods to 14 dataclass types soprint(message)produces useful output without helper functions_truncateprivate helper (80-char limit with...suffix)Closes #78
Context
Issue #78 (filed by @blois) noted that the auto-generated dataclass repr
is too verbose for practical debugging. Example helpers like
display_message()inexamples/streaming_mode.pyexist solely becausethe default output is unreadable.
Note: PR #326 by @martinritchie addressed the same issue but has been
pending review since Nov 2025. This PR takes a more focused approach
(repr only, no
__str__, no version bump, no example changes) and uses!rformatting for proper escaping of special characters.Changes
src/claude_agent_sdk/types.py(+106 lines):_truncate(text, max_length=80)helper before content block section@dataclass(repr=False)+ custom__repr__on 14 types:TextBlock,ThinkingBlock,ToolUseBlock,ToolResultBlockUserMessage,AssistantMessage,SystemMessage,ResultMessageTaskStartedMessage,TaskProgressMessage,TaskNotificationMessageStreamEvent,RateLimitEvent,RateLimitInfotests/test_types.py(+237 lines):TestTruncateHelper(5 tests)TestContentBlockRepr(10 tests incl. quotes/newlines/backslash edge cases)TestMessageRepr(4 tests)TestSystemMessageRepr(4 tests)TestResultAndEventRepr(5 tests)Before / After
Test plan
python -m pytest tests/— 355/355 passpython -m ruff check src/ tests/— cleanpython -m ruff format src/ tests/— cleanpython -m mypy src/— only pre-existing errors in__init__.py!r