Skip to content

Add __repr__ to message and content block types#770

Open
kostakost2 wants to merge 1 commit intoanthropics:mainfrom
kostakost2:add-repr-message-types
Open

Add __repr__ to message and content block types#770
kostakost2 wants to merge 1 commit intoanthropics:mainfrom
kostakost2:add-repr-message-types

Conversation

@kostakost2
Copy link
Copy Markdown

Fixes #78.

What this does

Adds human-readable __repr__ methods to all message types and content block dataclasses, so messages display usefully in REPLs, logs and debuggers without needing custom helpers.

Before:

>>> msg
AssistantMessage(content=[TextBlock(text='The capital of France is Paris...'), ToolUseBlock(id='toolu_01', name='bash', input={'command': 'ls'})], model='claude-sonnet-4-6', parent_tool_use_id=None, error=None, usage={...}, message_id='msg_01', stop_reason='end_turn', session_id='sess_01', uuid='uuid_01')

After:

>>> msg
AssistantMessage(model='claude-sonnet-4-6', content='The capital of France is Paris...', stop_reason='end_turn')

>>> TextBlock(text='Hello world')
TextBlock(text='Hello world')

>>> ToolUseBlock(id='toolu_01', name='bash', input={'command': 'ls -la'})
ToolUseBlock(name='bash', input_keys=['command'])

>>> ResultMessage(...)
ResultMessage(status='ok', turns=3, cost=$0.0042)

Changes

  • TextBlock: truncated text preview (60 chars)
  • ThinkingBlock: truncated thinking preview
  • ToolUseBlock: tool name + input key names (values omitted — can be large/sensitive)
  • ToolResultBlock: truncated content preview, error flag shown when true
  • UserMessage: string content preview or block count
  • AssistantMessage: model, first text block preview, stop_reason/error when set
  • SystemMessage: subtype only
  • ResultMessage: status (ok/error), turn count, cost when available

Tests

All 409 existing tests pass. The new reprs are purely additive — no behaviour changes.

Fixes anthropics#78. Adds human-readable __repr__ methods to all message types
and content blocks so they display usefully without custom helpers:

- TextBlock: shows truncated text preview
- ThinkingBlock: shows truncated thinking preview
- ToolUseBlock: shows tool name and input key names
- ToolResultBlock: shows truncated content and error flag
- UserMessage: shows content preview or block count
- AssistantMessage: shows model, text preview, stop_reason
- SystemMessage: shows subtype
- ResultMessage: shows status, turn count and cost

All 409 existing tests pass.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support reprs for message types

1 participant