deepseek-sdk 0.3.0

DeepSeek API client for Rust.
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
//! Responses API streaming and request implementation for `/responses`.
use crate::DeepSeekRequest;
use crate::error::DeepSeekError;
use crate::{api_post, api_request_stream, consume_sse, spawn_blocking_stream};

use super::request::*;
use super::response::*;
use reqwest::Method;
use serde::{Deserialize, Serialize};
use tokio::sync::mpsc;

/// Stream item produced by Responses API streaming.
pub type ResponsesStreamItem = Result<ResponsesStreamEvent, DeepSeekError>;

/// Blocking iterator over streamed Responses API events.
pub struct ResponsesStreamBlocking {
    pub rx: std::sync::mpsc::Receiver<ResponsesStreamItem>,
}

impl Iterator for ResponsesStreamBlocking {
    type Item = ResponsesStreamItem;

    fn next(&mut self) -> Option<Self::Item> {
        self.rx.recv().ok()
    }
}

/// A semantic server-sent event emitted by the Responses API stream.
///
/// Each event carries a `sequence_number`. The final event is `response.completed` /
/// `response.incomplete` / `response.failed`; there is no `data: [DONE]` message.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(tag = "type")]
pub enum ResponsesStreamEvent {
    /// The first event; the response has been created with status `in_progress`.
    #[serde(rename = "response.created")]
    ResponseCreated {
        sequence_number: u64,
        response: Response,
    },
    /// The response is being generated.
    #[serde(rename = "response.in_progress")]
    ResponseInProgress {
        sequence_number: u64,
        response: Response,
    },
    /// An output item starts.
    #[serde(rename = "response.output_item.added")]
    ResponseOutputItemAdded {
        sequence_number: u64,
        output_index: u64,
        item: OutputItem,
    },
    /// An output item completes.
    #[serde(rename = "response.output_item.done")]
    ResponseOutputItemDone {
        sequence_number: u64,
        output_index: u64,
        item: OutputItem,
    },
    /// A content part within an output item starts.
    #[serde(rename = "response.content_part.added")]
    ResponseContentPartAdded {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        content_index: u64,
        part: ContentPart,
    },
    /// A content part within an output item completes.
    #[serde(rename = "response.content_part.done")]
    ResponseContentPartDone {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        content_index: u64,
        part: ContentPart,
    },
    /// Incremental chain-of-thought text.
    #[serde(rename = "response.reasoning_text.delta")]
    ResponseReasoningTextDelta {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        content_index: u64,
        delta: String,
    },
    /// The full chain-of-thought text.
    #[serde(rename = "response.reasoning_text.done")]
    ResponseReasoningTextDone {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        content_index: u64,
        text: String,
    },
    /// Incremental output text.
    #[serde(rename = "response.output_text.delta")]
    ResponseOutputTextDelta {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        content_index: u64,
        delta: String,
    },
    /// The full output text.
    #[serde(rename = "response.output_text.done")]
    ResponseOutputTextDone {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        content_index: u64,
        text: String,
    },
    /// Incremental function call arguments.
    #[serde(rename = "response.function_call_arguments.delta")]
    ResponseFunctionCallArgumentsDelta {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        delta: String,
    },
    /// The full function call arguments.
    #[serde(rename = "response.function_call_arguments.done")]
    ResponseFunctionCallArgumentsDone {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        arguments: String,
    },
    /// Incremental custom tool call (`apply_patch`) input.
    #[serde(rename = "response.custom_tool_call_input.delta")]
    ResponseCustomToolCallInputDelta {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        delta: String,
    },
    /// The full custom tool call (`apply_patch`) input.
    #[serde(rename = "response.custom_tool_call_input.done")]
    ResponseCustomToolCallInputDone {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
        input: String,
    },
    /// A server-side web search tool call is starting.
    #[serde(rename = "response.web_search_call.in_progress")]
    ResponseWebSearchCallInProgress {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
    },
    /// A server-side web search tool call is searching.
    #[serde(rename = "response.web_search_call.searching")]
    ResponseWebSearchCallSearching {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
    },
    /// A server-side web search tool call completed.
    #[serde(rename = "response.web_search_call.completed")]
    ResponseWebSearchCallCompleted {
        sequence_number: u64,
        item_id: String,
        output_index: u64,
    },
    /// The final event when the response completes normally, carrying the full response object.
    #[serde(rename = "response.completed")]
    ResponseCompleted {
        sequence_number: u64,
        response: Response,
    },
    /// The final event when the response is truncated, carrying the full response object.
    #[serde(rename = "response.incomplete")]
    ResponseIncomplete {
        sequence_number: u64,
        response: Response,
    },
    /// The final event when the response fails, carrying the full response object with error details.
    #[serde(rename = "response.failed")]
    ResponseFailed {
        sequence_number: u64,
        response: Response,
    },
    /// An unrecognized event type, tolerated during deserialization.
    #[serde(other)]
    Unknown,
}

impl ResponsesStreamEvent {
    /// Whether this event is one of the terminal events (`response.completed` /
    /// `response.incomplete` / `response.failed`).
    #[allow(clippy::match_like_matches_macro)] // non_exhaustive enum requires a catch-all arm
    pub fn is_terminal(&self) -> bool {
        match self {
            ResponsesStreamEvent::ResponseCompleted { .. }
            | ResponsesStreamEvent::ResponseIncomplete { .. }
            | ResponsesStreamEvent::ResponseFailed { .. } => true,
            _ => false,
        }
    }

    /// The full `response` object carried by created / in-progress / terminal events.
    pub fn response(&self) -> Option<&Response> {
        match self {
            ResponsesStreamEvent::ResponseCreated { response, .. }
            | ResponsesStreamEvent::ResponseInProgress { response, .. }
            | ResponsesStreamEvent::ResponseCompleted { response, .. }
            | ResponsesStreamEvent::ResponseIncomplete { response, .. }
            | ResponsesStreamEvent::ResponseFailed { response, .. } => Some(response),
            _ => None,
        }
    }

    /// The incremental delta text carried by `*_text.delta` / `*_arguments.delta` events.
    pub fn delta(&self) -> Option<&str> {
        match self {
            ResponsesStreamEvent::ResponseOutputTextDelta { delta, .. }
            | ResponsesStreamEvent::ResponseReasoningTextDelta { delta, .. }
            | ResponsesStreamEvent::ResponseFunctionCallArgumentsDelta { delta, .. }
            | ResponsesStreamEvent::ResponseCustomToolCallInputDelta { delta, .. } => Some(delta),
            _ => None,
        }
    }
}

impl DeepSeekRequest for ResponsesRequest {
    type Response = Response;
    type StreamItem = ResponsesStreamItem;
    type BlockingStream = ResponsesStreamBlocking;

    async fn send(self) -> Result<Response, DeepSeekError> {
        let client = self.client.clone();
        api_post("/responses", &self, client).await
    }

    async fn stream(self) -> Result<mpsc::Receiver<ResponsesStreamItem>, DeepSeekError> {
        let mut request = self;
        request.stream = Some(true);

        let client = request.client.clone();
        let event_source = api_request_stream(
            Method::POST,
            "/responses",
            |builder| builder.json(&request),
            client,
        )
        .await?;

        Ok(consume_sse(event_source, |data| {
            serde_json::from_str::<ResponsesStreamEvent>(&data)
                .map(Some)
                .map_err(|err| DeepSeekError::decode(err.to_string(), data))
        }))
    }

    fn stream_blocking(self) -> Result<ResponsesStreamBlocking, DeepSeekError> {
        let rx = spawn_blocking_stream(self.stream())?;
        Ok(ResponsesStreamBlocking { rx })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{DEFAULT_BASE_URL, DeepSeekClient};

    #[test]
    fn deserialize_stream_event_dotted_names() {
        let created = serde_json::from_str::<ResponsesStreamEvent>(
            r#"{"type":"response.created","sequence_number":0,"response":{"id":"r_1","object":"response","created_at":1753000000,"status":"in_progress","model":"deepseek-v4-flash","output":[],"usage":null}}"#,
        )
        .unwrap();
        assert!(matches!(
            created,
            ResponsesStreamEvent::ResponseCreated { response, .. }
                if response.usage.is_none() && response.status == ResponseStatus::InProgress
        ));

        let delta = serde_json::from_str::<ResponsesStreamEvent>(
            r#"{"type":"response.output_text.delta","sequence_number":11,"item_id":"msg_1","output_index":1,"content_index":0,"delta":"Hello"}"#,
        )
        .unwrap();
        assert!(matches!(
            delta,
            ResponsesStreamEvent::ResponseOutputTextDelta { delta, .. } if delta == "Hello"
        ));

        let done = serde_json::from_str::<ResponsesStreamEvent>(
            r#"{"type":"response.output_item.done","sequence_number":20,"output_index":1,"item":{"type":"message","id":"msg_1","status":"completed","role":"assistant","content":[{"type":"output_text","text":"Hello"}]}}"#,
        )
        .unwrap();
        assert!(matches!(
            done,
            ResponsesStreamEvent::ResponseOutputItemDone { .. }
        ));

        let unknown = serde_json::from_str::<ResponsesStreamEvent>(
            r#"{"type":"something.new","sequence_number":99}"#,
        )
        .unwrap();
        assert!(matches!(unknown, ResponsesStreamEvent::Unknown));
    }

    #[test]
    fn stream_event_serializes_dotted_names() {
        let evt = ResponsesStreamEvent::ResponseCompleted {
            sequence_number: 20,
            response: Response {
                id: "r_1".to_string(),
                object: "response".to_string(),
                created_at: 1753000000,
                status: ResponseStatus::Completed,
                error: None,
                incomplete_details: None,
                model: "deepseek-v4-flash".to_string(),
                output: vec![],
                usage: None,
                store: None,
                parallel_tool_calls: None,
                previous_response_id: None,
            },
        };
        let value = serde_json::to_value(evt).unwrap();
        assert_eq!(
            value.get("type"),
            Some(&serde_json::json!("response.completed"))
        );
    }

    fn get_client() -> DeepSeekClient {
        DeepSeekClient::new(
            std::env::var("DEEPSEEK_API_KEY").expect("DEEPSEEK_API_KEY is not set"),
            DEFAULT_BASE_URL.clone(),
        )
    }

    fn get_builder() -> ResponsesRequestBuilder {
        ResponsesRequestBuilder::default()
            .client(get_client())
            .model("deepseek-v4-flash")
            .instructions("You are a helpful assistant.")
            .reasoning(Reasoning::new(ReasoningEffort::None))
    }

    #[tokio::test]
    async fn responses_basic() {
        let req = get_builder()
            .input("Reply with exactly: OK")
            .max_output_tokens(64_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        assert_eq!(response.object, "response");
        assert_eq!(response.status, ResponseStatus::Completed);
        assert!(!response.output_text().is_empty());
        assert!(response.usage.is_some());
    }

    #[tokio::test]
    async fn responses_thinking_mode() {
        let req = get_builder()
            .reasoning(Reasoning::new(ReasoningEffort::Low))
            .input("What is 2+2? Reply briefly.")
            .max_output_tokens(256_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        let has_reasoning = response
            .output
            .iter()
            .any(|item| matches!(item, OutputItem::Reasoning { .. }));
        assert!(
            has_reasoning,
            "expected a reasoning output item in thinking mode"
        );
        assert!(!response.output_text().is_empty());
    }

    #[tokio::test]
    async fn responses_json_object() {
        let req = get_builder()
            .text(Text::new(TextFormat::json_object()))
            .input("Return a JSON object with a `city` field set to Hangzhou.")
            .max_output_tokens(128_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        let parsed: serde_json::Value = serde_json::from_str(&response.output_text())
            .expect("output text should be valid JSON");
        assert_eq!(
            parsed.get("city").and_then(|c| c.as_str()),
            Some("Hangzhou")
        );
    }

    #[tokio::test]
    async fn responses_json_schema() {
        let req = get_builder()
            .text(Text::new(TextFormat::json_schema(
                "city_response",
                serde_json::json!({
                    "type": "object",
                    "properties": {
                        "city": {"type": "string"},
                        "temperature": {"type": "number"}
                    },
                    "required": ["city", "temperature"]
                }),
            )))
            .input("What is the weather in Hangzhou? Say 24 degrees.")
            .max_output_tokens(128_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        let parsed: serde_json::Value = serde_json::from_str(&response.output_text())
            .expect("output text should be valid JSON");
        assert_eq!(
            parsed.get("city").and_then(|c| c.as_str()),
            Some("Hangzhou")
        );
        assert!(parsed.get("temperature").and_then(|t| t.as_f64()).is_some());
    }

    #[tokio::test]
    async fn responses_input_item_list() {
        let req = get_builder()
            .input(vec![
                InputItem::user("Remember my name is Alice."),
                InputItem::assistant("Got it, Alice!"),
                InputItem::user("What is my name?"),
            ])
            .max_output_tokens(64_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        assert!(response.output_text().contains("Alice"));
    }

    #[tokio::test]
    async fn responses_tool_call() {
        let tool = Tool::function(
            "get_weather",
            "Get the weather of a location.",
            Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            })),
        );
        let req = get_builder()
            .input("What is the weather in Hangzhou?")
            .tool(tool)
            .tool_choice(ToolChoice::named("get_weather"))
            .max_output_tokens(128_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        let function_call = response
            .output
            .iter()
            .find(|item| matches!(item, OutputItem::FunctionCall { .. }))
            .expect("expected a function_call output item");
        let OutputItem::FunctionCall {
            name, arguments, ..
        } = function_call
        else {
            unreachable!()
        };
        assert_eq!(name, "get_weather");
        let parsed: serde_json::Value =
            serde_json::from_str(arguments).expect("arguments should be valid JSON");
        assert_eq!(
            parsed.get("location").and_then(|l| l.as_str()),
            Some("Hangzhou")
        );
    }

    #[tokio::test]
    async fn responses_multi_turn_tool_call() {
        let tool = Tool::function(
            "get_weather",
            "Get the weather of a location.",
            Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "location": {"type": "string"}
                },
                "required": ["location"]
            })),
        );

        let first = get_builder()
            .input("What is the weather in Hangzhou?")
            .tool(tool.clone())
            .tool_choice(ToolChoice::named("get_weather"))
            .max_output_tokens(128_u32)
            .build()
            .unwrap();
        let first_resp = first.send().await.unwrap();
        let function_call = first_resp
            .output
            .iter()
            .find(|item| matches!(item, OutputItem::FunctionCall { .. }))
            .expect("expected a function_call output item");
        let OutputItem::FunctionCall {
            call_id,
            name,
            arguments,
            ..
        } = function_call
        else {
            unreachable!()
        };
        let call_id = call_id.clone().expect("function_call carries a call_id");
        let name = name.clone();
        let arguments = arguments.clone();

        let second = get_builder()
            .input(vec![
                InputItem::user("What is the weather in Hangzhou?"),
                InputItem::function_call(call_id.clone(), name, arguments),
                InputItem::function_call_output(call_id, "24°C, clear sky"),
            ])
            .tool(tool)
            .tool_choice(ToolChoice::auto())
            .max_output_tokens(128_u32)
            .build()
            .unwrap();
        let second_resp = second.send().await.unwrap();
        println!("{:#?}", second_resp);
        assert!(second_resp.output_text().contains("24°C"));
    }

    #[tokio::test]
    async fn responses_web_search() {
        let req = get_builder()
            .tool(Tool::web_search())
            .tool_choice(ToolChoice::web_search())
            .input("Search the web and report the weather in Hangzhou.")
            .max_output_tokens(256_u32)
            .build()
            .unwrap();
        let response = req.send().await.unwrap();
        println!("{:#?}", response);
        assert_eq!(response.status, ResponseStatus::Completed);
        // Forcing web_search executes the search on the server and returns the
        // `web_search_call` items; a final assistant message is not guaranteed.
        assert!(
            response
                .output
                .iter()
                .any(|item| matches!(item, OutputItem::WebSearchCall { .. })),
            "expected web_search_call output items"
        );
    }

    #[tokio::test]
    async fn responses_stream_async() {
        let req = get_builder()
            .input("Count from 1 to 5.")
            .max_output_tokens(128_u32)
            .build()
            .unwrap();

        let mut rx = req.stream().await.unwrap();
        let mut text = String::new();
        let mut saw_terminal = false;
        while let Some(item) = rx.recv().await {
            match item {
                Ok(evt) => {
                    if let Some(delta) = evt.delta() {
                        text.push_str(delta);
                    }
                    if evt.is_terminal() {
                        saw_terminal = true;
                        let response = evt.response().expect("terminal event carries the response");
                        assert!(response.usage.is_some());
                    }
                }
                Err(err) => eprintln!("Error>\t {err:?}"),
            }
        }
        println!("Model>\t {text}");
        assert!(!text.is_empty());
        assert!(saw_terminal, "expected a terminal stream event");
    }

    #[test]
    fn responses_stream_blocking() {
        let req = get_builder()
            .input("Count from 1 to 5.")
            .max_output_tokens(128_u32)
            .build()
            .unwrap();

        let stream = req.stream_blocking().unwrap();
        let mut text = String::new();
        for item in stream.take(500) {
            match item {
                Ok(evt) => {
                    if let Some(delta) = evt.delta() {
                        text.push_str(delta);
                    }
                }
                Err(err) => eprintln!("Error>\t {err:?}"),
            }
        }
        println!("Model>\t {text}");
        assert!(!text.is_empty());
    }
}