Skip to content

fix: include input_tokens in message_delta events for Anthropic compatibility#21

Open
dracpet wants to merge 1 commit intoAMAP-ML:mainfrom
dracpet:fix/anthropic-message-delta-input-tokens
Open

fix: include input_tokens in message_delta events for Anthropic compatibility#21
dracpet wants to merge 1 commit intoAMAP-ML:mainfrom
dracpet:fix/anthropic-message-delta-input-tokens

Conversation

@dracpet
Copy link
Copy Markdown

@dracpet dracpet commented Apr 27, 2026

Problem

Some Anthropic-compatible clients (e.g., AgentScope/QwenPaw) expect input_tokens to be present in the message_delta event's usage object. Currently, stream_from_openai_result only includes output_tokens, which causes TypeError: '>' not supported between instances of 'NoneType' and 'int' in frameworks that parse usage.input_tokens from the final delta event.

Solution

Add input_tokens to the usage dict in the message_delta event, matching the value from message_start.

Changed

# Before
"usage": {"output_tokens": usage.get("completion_tokens", 0)},

# After  
"usage": {
    "input_tokens": usage.get("prompt_tokens", 0),
    "output_tokens": usage.get("completion_tokens", 0),
},

Verification

Tested with:

  • ✅ Simple streaming & non-streaming
  • ✅ Tool definitions & tool results
  • ✅ Multi-turn conversations (3 turns)
  • ✅ Large system prompts (skill injection simulation)
  • ✅ Minimal responses (edge case)
  • ✅ Stop sequences
  • ✅ Concurrent requests (3 simultaneous)
  • ✅ AgentScope/QwenPaw integration

All 10 test scenarios pass without regression.

…tibility

Some Anthropic-compatible clients (e.g., AgentScope/QwenPaw) expect input_tokens to be present in the message_delta event's usage object. Currently, stream_from_openai_result only includes output_tokens, which causes TypeError: '>' not supported between instances of 'NoneType' and 'int' in frameworks that parse usage.input_tokens from the final delta event.

Solution: Add input_tokens to the usage dict in the message_delta event.
@Upper9527 Upper9527 requested a review from StoneHanaMori April 27, 2026 12:11
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.

1 participant