Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

open_ai: Fix parsing stream usage chunks with null prompt cache tokens (#62983)

Enoch693140abac79
diff --git a/crates/open_ai/src/completion.rs b/crates/open_ai/src/completion.rs
index 893a14ca5ec..94028b976e7 100644
--- a/crates/open_ai/src/completion.rs
+++ b/crates/open_ai/src/completion.rs
@@ -820,11 +820,13 @@ impl OpenAiEventMapper {
let cache_creation_input_tokens = usage
.prompt_tokens_details
.as_ref()
- .map_or(0, |details| details.cache_write_tokens);
+ .and_then(|details| details.cache_write_tokens)
+ .unwrap_or(0);
let cache_read_input_tokens = usage
.prompt_tokens_details
.as_ref()
- .map_or(0, |details| details.cached_tokens);
+ .and_then(|details| details.cached_tokens)
+ .unwrap_or(0);
events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(TokenUsage {
input_tokens: prompt_tokens
.saturating_sub(cache_creation_input_tokens)
diff --git a/crates/open_ai/src/open_ai.rs b/crates/open_ai/src/open_ai.rs
index f9a66db8d38..2b79a5c823d 100644
--- a/crates/open_ai/src/open_ai.rs
+++ b/crates/open_ai/src/open_ai.rs
@@ -515,6 +515,25 @@ mod tests {
expected_efforts.as_slice()
);
}
+
+ #[test]
+ fn stream_usage_with_null_prompt_cache_tokens_is_not_an_error() {
+ let chunk = r#"{"choices":[],"usage":{"prompt_tokens":5,"completion_tokens":3,"total_tokens":8,"prompt_tokens_details":{"cached_tokens":0,"cache_write_tokens":null}}}"#;
+
+ let super::ResponseStreamResult::Ok(event) =
+ serde_json::from_str::<super::ResponseStreamResult>(chunk).unwrap()
+ else {
+ panic!("usage-only chunk with null cache token fields must not fail to parse");
+ };
+
+ let details = event
+ .usage
+ .unwrap()
+ .prompt_tokens_details
+ .expect("prompt_tokens_details should be present");
+ assert_eq!(details.cached_tokens, Some(0));
+ assert_eq!(details.cache_write_tokens, None);
+ }
}
#[derive(Debug, Serialize, Deserialize)]
@@ -754,11 +773,9 @@ pub struct FunctionChunk {
#[derive(Clone, Serialize, Deserialize, Debug, Default)]
pub struct PromptTokensDetails {
/// Tokens read from a prompt cache.
- #[serde(default)]
- pub cached_tokens: u64,
+ pub cached_tokens: Option<u64>,
/// Tokens written to a prompt cache.
- #[serde(default)]
- pub cache_write_tokens: u64,
+ pub cache_write_tokens: Option<u64>,
}
#[derive(Clone, Serialize, Deserialize, Debug)]