Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(rust): decode Anthropic citation deltas

Yujong Leecde34d2b399c
diff --git a/litellm-rust/crates/core/src/providers/anthropic/messages/streaming.rs b/litellm-rust/crates/core/src/providers/anthropic/messages/streaming.rs
index 3dabf58c7af..ab087e50805 100644
--- a/litellm-rust/crates/core/src/providers/anthropic/messages/streaming.rs
+++ b/litellm-rust/crates/core/src/providers/anthropic/messages/streaming.rs
@@ -43,12 +43,25 @@ pub struct AnthropicStreamMessage {
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AnthropicContentBlockDelta {
- TextDelta { text: String },
- InputJsonDelta { partial_json: String },
- Citations { citation: Value },
- ThinkingDelta { thinking: String },
- SignatureDelta { signature: String },
- CompactionDelta { content: String },
+ TextDelta {
+ text: String,
+ },
+ InputJsonDelta {
+ partial_json: String,
+ },
+ #[serde(rename = "citations_delta")]
+ Citations {
+ citation: Value,
+ },
+ ThinkingDelta {
+ thinking: String,
+ },
+ SignatureDelta {
+ signature: String,
+ },
+ CompactionDelta {
+ content: String,
+ },
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
@@ -218,6 +231,28 @@ mod tests {
);
}
+ #[test]
+ fn decodes_citations_delta_events() {
+ let event = decode_anthropic_sse_frame(SseFrame {
+ event: Some("content_block_delta".into()),
+ data: Some(
+ r#"{"type":"content_block_delta","index":0,"delta":{"type":"citations_delta","citation":{"type":"char_location"}}}"#
+ .into(),
+ ),
+ id: None,
+ retry: None,
+ })
+ .unwrap();
+
+ assert!(matches!(
+ event,
+ AnthropicMessagesStreamEvent::ContentBlockDelta {
+ delta: AnthropicContentBlockDelta::Citations { .. },
+ ..
+ }
+ ));
+ }
+
#[tokio::test]
async fn bedrock_aws_frames_into_the_same_typed_events() {
let payload = serde_json::json!({"bytes": STANDARD.encode(TEXT_DELTA)});