pub fn nullable_count<'de, D, T>(
deserializer: D,
) -> Result<T, <D as Deserializer<'de>>::Error>Expand description
Core traits and types.
Always available regardless of feature flags. Includes:
Agent- The fundamental trait for all agentsTool/Toolset- For extending agents with capabilitiesSession/State- For managing conversation contextEvent- For streaming agent responsesAdkError/Result- Unified error handling Deserializes a token count that a provider may report asnull, readingnullas zero.
Pair it with #[serde(default)] so that a missing count is zero too. Compatible endpoints
report a count they did not compute as null or leave it out, and that must not fail the
model turn the usage belongs to. Fields typed Option<_> need no helper: serde already
reads null there as None.
§Example
use serde::Deserialize;
#[derive(Deserialize)]
struct Usage {
#[serde(default, deserialize_with = "adk_core::nullable_count")]
input_tokens: u32,
}
let usage: Usage = serde_json::from_str(r#"{"input_tokens": null}"#).unwrap();
assert_eq!(usage.input_tokens, 0);
let usage: Usage = serde_json::from_str("{}").unwrap();
assert_eq!(usage.input_tokens, 0);
let usage: Usage = serde_json::from_str(r#"{"input_tokens": 12}"#).unwrap();
assert_eq!(usage.input_tokens, 12);§Errors
Returns the deserializer’s error when the value is neither null nor a T.