fix: allow null value for used field in UsageUpdate#605
fix: allow null value for used field in UsageUpdate#605sternelee wants to merge 1 commit intoagentclientprotocol:mainfrom
Conversation
The `used` field in UsageUpdate can be null when the agent doesn't provide token usage information. Changed from `u64` to `Option<u64>` to handle this case gracefully. Fixes decode error: "invalid type: null, expected u64" Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR updates the unstable session usage update model to tolerate agents sending null for token usage, preventing deserialization failures when usage data is missing.
Changes:
- Change
UsageUpdate.usedfromu64toOption<u64>to acceptnull/missing values. - Update
UsageUpdate::newto acceptimpl IntoOption<u64>and convert toOption<u64>. - Add
skip_serializing_if = "Option::is_none"onusedto omit it whenNone.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -268,9 +269,9 @@ pub struct UsageUpdate { | |||
| #[cfg(feature = "unstable_session_usage")] | |||
| impl UsageUpdate { | |||
| #[must_use] | |||
| pub fn new(used: u64, size: u64) -> Self { | |||
| pub fn new(used: impl IntoOption<u64>, size: u64) -> Self { | |||
| Self { | |||
| used, | |||
| used: used.into_option(), | |||
| size, | |||
There was a problem hiding this comment.
This change fixes deserialization of used: null, but there’s no test coverage for UsageUpdate’s (de)serialization behavior. Please add unit tests (under #[cfg(feature = "unstable_session_usage")]) that cover at least: {used: null} -> used == None, {} (missing used) behavior if supported, and serialization behavior when used is None vs Some (especially with skip_serializing_if).
| #[serde(skip_serializing_if = "Option::is_none")] | ||
| pub used: Option<u64>, |
There was a problem hiding this comment.
By changing used from u64 to Option<u64>, the generated unstable JSON Schema for UsageUpdate will change (nullable + no longer required). Please regenerate and commit the updated schema/schema.unstable.json (and any generated docs/metadata that depend on it) so the repository’s published schema stays in sync with the Rust types.
| #[serde(skip_serializing_if = "Option::is_none")] | |
| pub used: Option<u64>, | |
| pub used: u64, |
| @@ -250,7 +250,8 @@ impl SessionInfoUpdate { | |||
| #[non_exhaustive] | |||
| pub struct UsageUpdate { | |||
| /// Tokens currently in context. | |||
There was a problem hiding this comment.
used is now optional, but the doc comment still reads like it’s always present. Please update the field docs to clarify that used may be omitted/null when the agent doesn’t provide token usage information (and what None means semantically).
| /// Tokens currently in context. | |
| /// Tokens currently in context. | |
| /// | |
| /// This field is optional and may be omitted/`null` if the agent does not provide | |
| /// token usage information for this update. When `None`, the token usage is | |
| /// unknown, not necessarily zero. |
The
usedfield in UsageUpdate can be null when the agent doesn't provide token usage information. Changed fromu64toOption<u64>to handle this case gracefully.Fixes decode error: "invalid type: null, expected u64"