fix: handle Map type in getChatUsage() after deserialization#1118
Conversation
When messages are persisted and reloaded, the _chat_usage metadata value may be deserialized as a Map instead of a ChatUsage object. The getter now detects this case, converts the Map to ChatUsage, and caches the result back in metadata for subsequent calls. Fixes agentscope-ai#1115
|
Hey @mvanhorn, great fix 👍 I noticed the CI build is failing—this might be due to type casting issues when converting values from the Map. Since JSON deserialization can produce Maybe using Happy to help test or contribute improvements if needed 🚀 |
|
Thanks for the review! The The CI failure was a spotless formatting issue with the builder chain indentation. Fixed in 536b3c8. |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Thanks for the merge, @LearningGp. Map handling in getChatUsage was a real corner case. |
…ope-ai#1118) ## Summary `getChatUsage()` returns null when `_chat_usage` metadata is a Map (after session persistence/reload) instead of a ChatUsage object. ## Why this matters When messages are persisted via `agent.loadFrom()` and reloaded, JSON deserialization converts the `_chat_usage` metadata value to a `Map<String, Object>` instead of a `ChatUsage` instance. The existing `instanceof ChatUsage` check fails, returning null even though the token usage data exists in the metadata. ## Changes `agentscope-core/src/main/java/io/agentscope/core/message/Msg.java`: - Added Map handling in `getChatUsage()` (line 413): when the metadata value is a Map, extract `inputTokens`, `outputTokens`, and `time` fields, build a ChatUsage via the builder, and cache it back in metadata for subsequent calls - Added `toInt()` and `toDouble()` helper methods for safe numeric conversion from deserialized values (handles Integer, Long, Double from JSON) ## Testing - When `_chat_usage` is a ChatUsage object: unchanged behavior - When `_chat_usage` is a Map (post-deserialization): converts to ChatUsage and returns it - When `_chat_usage` is null or absent: returns null (unchanged) - The converted ChatUsage is cached back in metadata so the conversion only happens once Fixes agentscope-ai#1115 This contribution was developed with AI assistance (Claude Code). --------- Co-authored-by: Matt Van Horn <[email protected]>
Summary
getChatUsage()returns null when_chat_usagemetadata is a Map (after session persistence/reload) instead of a ChatUsage object.Why this matters
When messages are persisted via
agent.loadFrom()and reloaded, JSON deserialization converts the_chat_usagemetadata value to aMap<String, Object>instead of aChatUsageinstance. The existinginstanceof ChatUsagecheck fails, returning null even though the token usage data exists in the metadata.Changes
agentscope-core/src/main/java/io/agentscope/core/message/Msg.java:getChatUsage()(line 413): when the metadata value is a Map, extractinputTokens,outputTokens, andtimefields, build a ChatUsage via the builder, and cache it back in metadata for subsequent callstoInt()andtoDouble()helper methods for safe numeric conversion from deserialized values (handles Integer, Long, Double from JSON)Testing
_chat_usageis a ChatUsage object: unchanged behavior_chat_usageis a Map (post-deserialization): converts to ChatUsage and returns it_chat_usageis null or absent: returns null (unchanged)Fixes #1115
This contribution was developed with AI assistance (Claude Code).