Skip to content

fix: preserve dropped fields in AssistantMessage and ResultMessage#563

Open
wenfengwang wants to merge 1 commit intoanthropics:mainfrom
sheet0:fix/preserve-dropped-fields
Open

fix: preserve dropped fields in AssistantMessage and ResultMessage#563
wenfengwang wants to merge 1 commit intoanthropics:mainfrom
sheet0:fix/preserve-dropped-fields

Conversation

@wenfengwang
Copy link

Summary

  • Add missing fields to AssistantMessage: id, usage, stop_reason, stop_sequence, session_id, uuid
  • Add missing fields to ResultMessage: model_usage (from CLI's modelUsage), stop_reason, permission_denials, uuid
  • Update message_parser.py to extract these fields from CLI JSON output
  • All new fields are optional with None defaults — fully backward compatible

Motivation

The CLI outputs rich JSON with many useful fields, but message_parser.py was only extracting a subset. Key losses:

  1. modelUsage — The authoritative per-model usage breakdown, more accurate than aggregate usage for cost tracking (see Python SDK ResultMessage's message.usage['input_tokens'] isn't correct #112 in TypeScript SDK). It's also the only way to identify which model was actually used.
  2. AssistantMessage.usage — Per-message token consumption, essential for per-turn cost tracking.
  3. uuid — Needed for correlating messages with session transcript files.

Full field comparison documented in #562.

Test plan

  • New tests for AssistantMessage with all fields populated (from real CLI output)
  • New tests for AssistantMessage optional fields defaulting to None
  • New tests for ResultMessage with modelUsage, stop_reason, permission_denials, uuid
  • New tests for ResultMessage optional fields defaulting to None
  • New test for ResultMessage with multiple models in modelUsage
  • All 148 existing tests still pass

Fixes #562

Made with Cursor

@wenfengwang wenfengwang force-pushed the fix/preserve-dropped-fields branch from 31ed4cb to 9c668b0 Compare February 11, 2026 04:57
The message parser was dropping significant fields from the raw CLI JSON
output when constructing AssistantMessage and ResultMessage dataclasses.

AssistantMessage now includes:
- id: Anthropic API message ID
- usage: per-message token usage breakdown
- stop_reason: why generation stopped
- stop_sequence: matched stop sequence (if any)
- session_id: session identifier
- uuid: unique message identifier

ResultMessage now includes:
- model_usage: authoritative per-model usage breakdown (from CLI's
  modelUsage field), which is more accurate than the aggregate usage
  field for cost tracking and model identification
- stop_reason: why generation stopped
- permission_denials: list of denied permissions
- uuid: unique message identifier

All new fields are optional with None defaults, ensuring full backward
compatibility.

Fixes anthropics#562

Co-authored-by: Cursor <cursoragent@cursor.com>
@wenfengwang wenfengwang force-pushed the fix/preserve-dropped-fields branch from b4c24ee to 2a2a42e Compare February 11, 2026 06:34
@b657bm7n98-hub
Copy link

+1

2 similar comments
@n0isy
Copy link

n0isy commented Feb 11, 2026

+1

@Augustab
Copy link

+1

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.

AssistantMessage and ResultMessage drop significant fields from CLI JSON output

4 participants