Skip to main content

nullable_count

Function nullable_count 

Source
pub fn nullable_count<'de, D, T>(
    deserializer: D,
) -> Result<T, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>, T: Deserialize<'de> + Default,
Expand description

Core traits and types.

Always available regardless of feature flags. Includes:

  • Agent - The fundamental trait for all agents
  • Tool / Toolset - For extending agents with capabilities
  • Session / State - For managing conversation context
  • Event - For streaming agent responses
  • AdkError / Result - Unified error handling Deserializes a token count that a provider may report as null, reading null as 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.