anthropic-api 0.0.5

An unofficial Rust library for the Anthropic API.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
//! # Messages API
//!
//! This module provides a Rust interface to Anthropic's Messages API, which allows you to interact
//! with Claude models in a conversational manner.
//!
//! ## Key Features
//!
//! - Send messages to Claude models and receive responses
//! - Support for streaming responses
//! - Tool usage capabilities
//! - Image input support
//!
//! ## Basic Usage
//!
//! ```no_run
//! use anthropic_api::{messages::*, Credentials};
//!
//! #[tokio::main]
//! async fn main() {
//!     let credentials = Credentials::from_env();
//!
//!     let response =MessagesBuilder::builder(
//!         "claude-3-7-sonnet-20250219",
//!         vec![Message {
//!             role: MessageRole::User,
//!             content: MessageContent::Text("Hello, Claude!".to_string()),
//!         }],
//!         1024,
//!     )
//!     .credentials(credentials)
//!     .create()
//!     .await
//!     .unwrap();
//!
//!     println!("Claude says: {:?}", response.content);
//! }
//! ```

use crate::{anthropic_post, anthropic_request_stream, ApiResponseOrError, Credentials, Usage};
use anyhow::Result;
use derive_builder::Builder;
use futures_util::StreamExt;
use reqwest::Method;
use reqwest_eventsource::{CannotCloneRequestError, Event, EventSource};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::mpsc::{channel, Receiver, Sender};

/// Represents a full message response from the Anthropic API.
///
/// This struct contains the complete response from a message request, including
/// the model's generated content and usage statistics.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct MessagesResponse {
    /// Unique identifier for this message
    pub id: String,
    /// The model that generated the response
    pub model: String,
    /// The role of the message sender (always Assistant for responses)
    pub role: MessageRole,
    /// The content blocks in the response (text, tool use, thinking, redacted thinking)
    pub content: Vec<ResponseContentBlock>,
    /// Reason why the model stopped generating, if applicable
    pub stop_reason: Option<String>,
    /// The specific sequence that caused generation to stop, if applicable
    pub stop_sequence: Option<String>,
    /// The type of the response (always "message")
    #[serde(rename = "type")]
    pub typ: String,
    /// Token usage statistics for the request and response
    pub usage: Usage,
}

/// Content block in a response, can be text or tool use.
///
/// Claude's responses can contain different types of content blocks.
/// Currently, this can be either text, a tool use request, a thinking block, or a redacted thinking block.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum ResponseContentBlock {
    /// A text content block containing natural language
    #[serde(rename = "text")]
    Text { text: String },
    /// A tool use request from the model
    #[serde(rename = "tool_use")]
    ToolUse {
        id: String,
        name: String,
        input: Value,
    },
    /// A thinking block from the model
    #[serde(rename = "thinking")]
    Thinking { signature: String, thinking: String },
    /// A redacted thinking block from the model
    #[serde(rename = "redacted_thinking")]
    RedactedThinking { data: String },
}

/// Streaming events from the Anthropic API.
///
/// When using streaming mode, the API returns a series of events that
/// incrementally build up the complete response.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum StreamEvent {
    /// Indicates the start of a message
    #[serde(rename = "message_start")]
    MessageStart { message: MessageStart },
    /// Indicates the start of a content block
    #[serde(rename = "content_block_start")]
    ContentBlockStart {
        index: u32,
        content_block: ContentBlockStart,
    },
    /// Contains a delta (incremental update) to a content block
    #[serde(rename = "content_block_delta")]
    ContentBlockDelta {
        index: u32,
        delta: ContentBlockDelta,
    },
    /// Indicates the end of a content block
    #[serde(rename = "content_block_stop")]
    ContentBlockStop { index: u32 },
    /// Contains final message information like stop reason
    #[serde(rename = "message_delta")]
    MessageDelta { delta: MessageDelta, usage: Usage },
    /// Indicates the end of the message
    #[serde(rename = "message_stop")]
    MessageStop,
    /// A keepalive event that can be ignored
    #[serde(rename = "ping")]
    Ping,
}

/// Initial message information in a streaming response.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct MessageStart {
    /// Unique identifier for this message
    pub id: String,
    /// The model generating the response
    pub model: String,
    /// The role of the message sender (always Assistant for responses)
    pub role: MessageRole,
    /// Initial content blocks in the response
    pub content: Vec<ContentBlockStart>,
}

/// Initial content block in a streaming response.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(untagged)]
pub enum ContentBlockStart {
    /// A text content block
    Text { text: String },
    /// A tool use request
    ToolUse {
        id: String,
        name: String,
        input: Value,
    },
}

/// Incremental update to a content block in a streaming response.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
#[serde(untagged)]
pub enum ContentBlockDelta {
    /// Text delta for a text content block
    Text { text: String },
    /// JSON delta for a tool use input
    InputJsonDelta { partial_json: String },
}

/// Final message information in a streaming response.
#[derive(Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct MessageDelta {
    /// Reason why the model stopped generating, if applicable
    pub stop_reason: Option<String>,
    /// The specific sequence that caused generation to stop, if applicable
    pub stop_sequence: Option<String>,
}

/// Request to the Anthropic Messages API.
///
/// This struct represents a complete request to the Messages API,
/// including all parameters that control generation behavior.
#[derive(Serialize, Builder, Debug, Clone)]
#[builder(derive(Clone, Debug, PartialEq))]
#[builder(pattern = "owned")]
#[builder(name = "MessagesBuilder")]
#[builder(setter(strip_option, into))]
pub struct MessagesRequest {
    /// The model to use (e.g., "claude-3-7-sonnet-20250219").
    pub model: String,
    /// The conversation messages.
    pub messages: Vec<Message>,
    /// Maximum number of tokens to generate.
    pub max_tokens: u64,
    /// Optional metadata.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<Metadata>,
    /// Sequences where generation should stop.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop_sequences: Option<Vec<String>>,
    /// Whether to stream the response.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    /// System prompt to guide the assistant's behavior.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub system: Option<String>,
    /// Sampling temperature (0.0 to 1.0).
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f64>,
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub thinking: Option<Thinking>,
    /// Tool choice specification.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_choice: Option<ToolChoice>,
    /// Tools the assistant can use.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tools: Option<Vec<Tool>>,
    /// Top-k sampling parameter.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_k: Option<u32>,
    /// Top-p (nucleus) sampling parameter.
    #[builder(default)]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f64>,
    /// Credentials for authentication (not serialized).
    #[serde(skip_serializing)]
    #[builder(default)]
    pub credentials: Option<Credentials>,
}

/// Message in the conversation.
///
/// Represents a single message in the conversation history,
/// with a role (user or assistant) and content.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub struct Message {
    /// The role of the message sender (user or assistant)
    pub role: MessageRole,
    /// The content of the message (text or content blocks)
    pub content: MessageContent,
}

/// Role of the message sender.
///
/// In the Messages API, messages can be from either the user or the assistant.
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum MessageRole {
    /// Message from the user
    User,
    /// Message from the assistant (Claude)
    Assistant,
}

/// Content of a message, either text or content blocks.
///
/// Messages can contain either simple text or structured content blocks
/// that can include text and images.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
#[serde(untagged)]
pub enum MessageContent {
    /// Simple text content
    Text(String),
    /// Structured content blocks (text and images)
    ContentBlocks(Vec<RequestContentBlock>),
}

/// Content block in a request.
///
/// Request content blocks can be either text or images.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum RequestContentBlock {
    /// A text content block
    #[serde(rename = "text")]
    Text { text: String },
    /// An image content block
    #[serde(rename = "image")]
    Image { source: ImageSource },
}

/// Source of an image content block.
///
/// Currently, images must be provided as base64-encoded data.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub struct ImageSource {
    /// The type of image source (currently only "base64" is supported)
    #[serde(rename = "type")]
    pub source_type: String,
    /// The MIME type of the image (e.g., "image/png", "image/jpeg")
    pub media_type: String,
    /// The base64-encoded image data
    pub data: String,
}

#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub enum ThinkingType {
    /// Whether Claude is to use thinking
    #[serde(rename = "enabled")]
    Enabled,
    /// Whether Claude is not to use thinking
    #[serde(rename = "disabled")]
    Disabled,
}

#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub struct Thinking {
    #[serde(rename = "type")]
    pub thinking_type: ThinkingType,
    /// The budget for the thinking in tokens must
    /// be at least 1024 and less than max_tokens
    #[serde(rename = "budget_tokens")]
    pub budget_tokens: u64,
}

/// Tool definition.
///
/// Tools allow Claude to perform actions outside its context,
/// such as calculations or API calls.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub struct Tool {
    /// The name of the tool
    pub name: String,
    /// A description of what the tool does
    pub description: String,
    /// JSON Schema defining the input format for the tool
    pub input_schema: Value,
}

/// Tool choice specification.
///
/// Controls how Claude decides whether to use tools.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
#[serde(tag = "type")]
pub enum ToolChoice {
    /// Claude decides whether to use tools
    #[serde(rename = "auto")]
    Auto,
    /// Claude can use any available tool
    #[serde(rename = "any")]
    Any,
    /// Claude must use the specified tool
    #[serde(rename = "tool")]
    Tool { name: String },
    /// Claude must not use any tools
    #[serde(rename = "none")]
    None,
}

/// Metadata for the request.
///
/// Additional information about the request that isn't
/// directly related to generation behavior.
#[derive(Serialize, Debug, Clone, Eq, PartialEq)]
pub struct Metadata {
    /// Optional user identifier for tracking purposes
    pub user_id: Option<String>,
}

// Implementation for non-streaming response
impl MessagesResponse {
    /// Creates a new message request and returns the response.
    ///
    /// This method sends a request to the Messages API and returns
    /// the complete response.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{messages::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let request = MessagesRequest {
    ///     model: "claude-3-7-sonnet-20250219".to_string(),
    ///     messages: vec![Message {
    ///         role: MessageRole::User,
    ///         content: MessageContent::Text("Hello!".to_string()),
    ///     }],
    ///     max_tokens: 100,
    ///     credentials: Some(credentials),
    ///     metadata: None,
    ///     stop_sequences: None,
    ///     stream: None,
    ///     system: None,
    ///     temperature: None,
    ///     thinking: None,
    ///     tool_choice: None,
    ///     tools: None,
    ///     top_k: None,
    ///     top_p: None,
    /// };
    ///
    /// let response = MessagesResponse::create(request).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(request: MessagesRequest) -> ApiResponseOrError<Self> {
        let credentials_opt = request.credentials.clone();
        anthropic_post("messages", &request, credentials_opt).await
    }
}

// Implementation for streaming response
impl StreamEvent {
    /// Creates a new streaming message request and returns a channel of events.
    ///
    /// This method sends a request to the Messages API in streaming mode
    /// and returns a channel that will receive the streaming events.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{messages::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    /// let mut request = MessagesRequest {
    ///     model: "claude-3-7-sonnet-20250219".to_string(),
    ///     messages: vec![Message {
    ///         role: MessageRole::User,
    ///         content: MessageContent::Text("Hello!".to_string()),
    ///     }],
    ///     max_tokens: 100,
    ///     credentials: Some(credentials),
    ///     metadata: None,
    ///     stop_sequences: None,
    ///     stream: Some(true),
    ///     system: None,
    ///     temperature: None,
    ///     thinking: None,
    ///     tool_choice: None,
    ///     tools: None,
    ///     top_k: None,
    ///     top_p: None,
    /// };
    ///
    /// let mut stream = StreamEvent::create_stream(request).await?;
    ///
    /// while let Some(event) = stream.recv().await {
    ///     // Process streaming events
    ///     println!("{:?}", event);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create_stream(
        request: MessagesRequest,
    ) -> Result<Receiver<Self>, CannotCloneRequestError> {
        let credentials_opt = request.credentials.clone();
        let stream = anthropic_request_stream(
            Method::POST,
            "messages",
            |r| r.json(&request),
            credentials_opt,
        )
        .await?;
        let (tx, rx) = channel::<Self>(32);
        tokio::spawn(forward_deserialized_anthropic_stream(stream, tx));
        Ok(rx)
    }
}

/// Processes the event stream and forwards events to the channel.
///
/// This internal function handles the raw event stream from the API
/// and deserializes events into the `StreamEvent` enum.
async fn forward_deserialized_anthropic_stream(
    mut stream: EventSource,
    tx: Sender<StreamEvent>,
) -> anyhow::Result<()> {
    while let Some(event) = stream.next().await {
        let event = event?;
        if let Event::Message(event) = event {
            let stream_event = serde_json::from_str::<StreamEvent>(&event.data)?;
            if matches!(stream_event, StreamEvent::Ping) {
                continue; // Ignore ping events
            }
            tx.send(stream_event).await?;
        }
    }
    Ok(())
}

// Builder convenience methods
impl MessagesBuilder {
    pub fn builder(model: &str, messages: impl Into<Vec<Message>>, max_tokens: u64) -> Self {
        Self::create_empty()
            .model(model)
            .messages(messages)
            .max_tokens(max_tokens)
    }

    /// Creates a new message request and returns the response.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the Messages API.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{messages::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let response =MessagesBuilder::builder("claude-3-7-sonnet-20250219",[], 1024)
    ///     .credentials(credentials.clone())
    ///     .create()
    ///     .await
    ///     .unwrap();
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create(self) -> ApiResponseOrError<MessagesResponse> {
        let request = self.build().unwrap();
        MessagesResponse::create(request).await
    }

    /// Creates a new streaming message request and returns a channel of events.
    ///
    /// This is a convenience method that builds the request from the builder
    /// and sends it to the Messages API in streaming mode.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{messages::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let mut stream =MessagesBuilder::builder("claude-3-7-sonnet-20250219", [], 1024)
    ///     .credentials(credentials)
    ///     .create_stream()
    ///     .await?;
    ///
    /// while let Some(event) = stream.recv().await {
    ///     // Process streaming events
    ///     println!("{:?}", event);
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn create_stream(self) -> Result<Receiver<StreamEvent>, CannotCloneRequestError> {
        let mut request = self.build().expect("Failed to build MessagesRequest");
        request.stream = Some(true);
        StreamEvent::create_stream(request).await
    }
}

// Helper to create a builder with required fields
impl MessagesResponse {
    /// Creates a new builder with the required fields.
    ///
    /// This is a convenience method to create a builder with the
    /// minimum required fields for a message request.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use anthropic_api::{messages::*, Credentials};
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let credentials = Credentials::from_env();
    ///
    /// let response =MessagesBuilder::builder(
    ///     "claude-3-7-sonnet-20250219",
    ///     vec![Message {
    ///         role: MessageRole::User,
    ///         content: MessageContent::Text("Hello!".to_string()),
    ///     }],
    ///     100,
    /// )
    /// .credentials(credentials)
    /// .create()
    /// .await?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn builder(
        model: &str,
        messages: impl Into<Vec<Message>>,
        max_tokens: u64,
    ) -> MessagesBuilder {
        MessagesBuilder::create_empty()
            .model(model)
            .messages(messages)
            .max_tokens(max_tokens)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_simple_message() {
        let credentials = Credentials::from_env();

        let response = MessagesResponse::builder(
            "claude-3-7-sonnet-20250219",
            vec![Message {
                role: MessageRole::User,
                content: MessageContent::Text("Hello!".to_string()),
            }],
            100,
        )
        .credentials(credentials)
        .create()
        .await
        .unwrap();

        assert!(!response.content.is_empty());
    }

    #[tokio::test]
    async fn test_streaming_message() {
        let credentials = Credentials::from_env();

        let mut stream = MessagesResponse::builder(
            "claude-3-7-sonnet-20250219",
            vec![Message {
                role: MessageRole::User,
                content: MessageContent::Text("Hello!".to_string()),
            }],
            100,
        )
        .credentials(credentials)
        .create_stream()
        .await
        .unwrap();

        while let Some(event) = stream.recv().await {
            match event {
                StreamEvent::ContentBlockDelta { delta, .. } => {
                    if let ContentBlockDelta::Text { text } = delta {
                        print!("{}", text);
                    }
                }
                _ => {}
            }
        }
    }
}