liter-llm 1.7.2

Universal LLM API client — 142+ providers, streaming, tool calling. Rust-powered, type-safe, compiled.
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
use serde::{Deserialize, Serialize};

// ─── Messages ────────────────────────────────────────────────────────────────

/// A chat message in a conversation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "role")]
pub enum Message {
    #[serde(rename = "system")]
    System(SystemMessage),
    #[serde(rename = "user")]
    User(UserMessage),
    #[serde(rename = "assistant")]
    Assistant(AssistantMessage),
    #[serde(rename = "tool")]
    Tool(ToolMessage),
    #[serde(rename = "developer")]
    Developer(DeveloperMessage),
    /// Deprecated legacy function-role message; retained for API compatibility.
    #[serde(rename = "function")]
    Function(FunctionMessage),
}

#[cfg_attr(alef, alef(skip))]
impl Default for Message {
    fn default() -> Self {
        Self::Assistant(AssistantMessage::default())
    }
}

/// System message guiding model behavior for the entire conversation.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SystemMessage {
    /// Instructions or context that apply throughout the conversation.
    ///
    /// Accepts either a plain text string or an array of content parts,
    /// mirroring [`UserContent`] so that `Message::system_with_parts` works.
    pub content: UserContent,
    /// Optional name for the system message source.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

/// User message in the conversation.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct UserMessage {
    /// Message content as plain text or array of content parts (text, images, documents, audio).
    pub content: UserContent,
    /// Optional name for the user.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

/// User message content as either plain text or a list of multimodal parts.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum UserContent {
    /// Plain text content.
    Text(String),
    /// Array of content parts (text, images, documents, audio).
    Parts(Vec<ContentPart>),
}

#[cfg_attr(alef, alef(skip))]
impl Default for UserContent {
    fn default() -> Self {
        Self::Text(String::new())
    }
}

impl UserContent {
    /// Return the text content as an owned `String`. For `Text` returns the
    /// inner string; for `Parts` concatenates every `ContentPart::Text` in order.
    /// Returns `None` when the content carries no textual data (e.g. an
    /// image-only `Parts` vec).
    pub fn as_text(&self) -> Option<String> {
        match self {
            UserContent::Text(s) => Some(s.clone()),
            UserContent::Parts(parts) => {
                let texts: Vec<&str> = parts
                    .iter()
                    .filter_map(|p| match p {
                        ContentPart::Text { text } => Some(text.as_str()),
                        _ => None,
                    })
                    .collect();
                if texts.is_empty() { None } else { Some(texts.join("")) }
            }
        }
    }
}

impl From<String> for UserContent {
    fn from(s: String) -> Self {
        Self::Text(s)
    }
}

impl From<&str> for UserContent {
    fn from(s: &str) -> Self {
        Self::Text(s.to_owned())
    }
}

/// A single content part in a user message — text, image, document, or audio.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ContentPart {
    /// Plain text.
    #[serde(rename = "text")]
    Text { text: String },
    /// Image identified by URL (with optional detail level).
    #[serde(rename = "image_url")]
    ImageUrl { image_url: ImageUrl },
    /// Document file (PDF, CSV, etc.) as base64 or URL.
    #[serde(rename = "document")]
    Document { document: DocumentContent },
    /// Audio input as base64.
    #[serde(rename = "input_audio")]
    InputAudio { input_audio: AudioContent },
}

#[cfg_attr(alef, alef(skip))]
impl Default for ContentPart {
    fn default() -> Self {
        Self::Text { text: String::new() }
    }
}

/// An image URL reference with optional detail level for processing.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ImageUrl {
    /// URL of the image (data URI or HTTP/HTTPS URL).
    pub url: String,
    /// Detail level: low (512x512), high (2x2 tiles), or auto (model-selected).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub detail: Option<ImageDetail>,
}

/// Image detail level controlling token cost and processing.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ImageDetail {
    /// Low detail: scales image to 512x512, uses fewer tokens.
    Low,
    /// High detail: processes up to 2x2 grid of tiles, higher token cost.
    High,
    /// Auto: model chooses low or high based on image dimensions.
    Auto,
}

/// PDF/document content part for vision-capable models.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DocumentContent {
    /// Base64-encoded document data or URL.
    pub data: String,
    /// MIME type (e.g., "application/pdf", "text/csv").
    pub media_type: String,
}

/// Audio content part for speech-capable models.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AudioContent {
    /// Base64-encoded audio data.
    pub data: String,
    /// Audio format (e.g., "wav", "mp3", "ogg").
    pub format: String,
}

/// Content shape for assistant messages.
///
/// `#[serde(untagged)]` means providers returning a plain scalar string for the
/// `content` field still deserialise correctly into `AssistantContent::Text(_)`.
/// Providers returning an array of typed parts (e.g. after an image-generation
/// or audio-synthesis request) deserialise into `AssistantContent::Parts(_)`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AssistantContent {
    /// Plain text response (the common case for text-only models).
    Text(String),
    /// Structured parts — text, refusals, output images, output audio.
    Parts(Vec<AssistantPart>),
}

#[cfg_attr(alef, alef(skip))]
impl Default for AssistantContent {
    fn default() -> Self {
        Self::Text(String::new())
    }
}

impl AssistantContent {
    /// Return the text content as an owned `String`. For `Text` returns the
    /// inner string; for `Parts` concatenates every `AssistantPart::Text` in order.
    /// Returns `None` when the content carries no textual data (e.g. an
    /// image-only `Parts` vec).
    pub fn as_text(&self) -> Option<String> {
        match self {
            AssistantContent::Text(s) => Some(s.clone()),
            AssistantContent::Parts(parts) => {
                let texts: Vec<&str> = parts
                    .iter()
                    .filter_map(|p| match p {
                        AssistantPart::Text { text } => Some(text.as_str()),
                        _ => None,
                    })
                    .collect();
                if texts.is_empty() { None } else { Some(texts.join("")) }
            }
        }
    }
}

impl std::fmt::Display for AssistantContent {
    /// Writes the textual content (see [`AssistantContent::as_text`]). Image-only
    /// or refusal-only `Parts` carry no text and render as an empty string.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_text().as_deref().unwrap_or(""))
    }
}

impl From<String> for AssistantContent {
    fn from(s: String) -> Self {
        Self::Text(s)
    }
}

impl From<&str> for AssistantContent {
    fn from(s: &str) -> Self {
        Self::Text(s.to_owned())
    }
}

/// One part of a structured assistant response.
///
/// `#[serde(tag = "type", rename_all = "snake_case")]` matches OpenAI's
/// parts-spec discriminator (`"type": "text"`, `"type": "output_image"`, …).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum AssistantPart {
    /// A text segment of the response.
    Text {
        /// The text content of this part.
        text: String,
    },
    /// A refusal — the model declined to respond.
    Refusal {
        /// The refusal reason.
        refusal: String,
    },
    /// An image produced by the model (e.g. `gpt-image-1`, Gemini Imagen).
    OutputImage {
        /// Image URL or data URI referencing the generated image.
        image_url: ImageUrl,
    },
    /// Audio produced by the model (e.g. `gpt-4o-audio-preview`).
    OutputAudio {
        /// Base64-encoded audio data and its format.
        audio: AudioContent,
    },
}

#[cfg_attr(alef, alef(skip))]
impl Default for AssistantPart {
    fn default() -> Self {
        Self::Text { text: String::new() }
    }
}

/// Assistant's response to a user message.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AssistantMessage {
    /// The assistant's response: plain text, structured parts, or absent.
    ///
    /// `None` is valid when the model replies with tool calls only.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content: Option<AssistantContent>,
    /// Optional name for the assistant.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Tool calls the model wants to execute, if any.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCall>>,
    /// Refusal reason, if the model declined to respond per safety policies.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub refusal: Option<String>,
    /// Deprecated legacy function_call field; retained for API compatibility.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub function_call: Option<FunctionCall>,
}

impl AssistantMessage {
    /// Return the assistant's textual response, concatenating all `Text` parts
    /// if the content is structured.
    ///
    /// Returns `None` for `Refusal`-only or `OutputImage`-only responses.
    pub fn text(&self) -> Option<String> {
        match self.content.as_ref()? {
            AssistantContent::Text(s) => Some(s.clone()),
            AssistantContent::Parts(parts) => {
                let texts: Vec<&str> = parts
                    .iter()
                    .filter_map(|p| match p {
                        AssistantPart::Text { text } => Some(text.as_str()),
                        _ => None,
                    })
                    .collect();
                if texts.is_empty() { None } else { Some(texts.join("")) }
            }
        }
    }

    /// Return the refusal message, if the model declined to respond.
    ///
    /// Checks both the top-level `refusal` field and any `Refusal` parts
    /// inside a structured `content`.
    pub fn refusal_text(&self) -> Option<&str> {
        if let Some(r) = self.refusal.as_deref() {
            return Some(r);
        }
        if let Some(AssistantContent::Parts(parts)) = self.content.as_ref() {
            for part in parts {
                if let AssistantPart::Refusal { refusal } = part {
                    return Some(refusal.as_str());
                }
            }
        }
        None
    }

    /// Return all [`AssistantPart::OutputImage`] parts in the response.
    pub fn output_images(&self) -> Vec<ImageUrl> {
        let Some(AssistantContent::Parts(parts)) = self.content.as_ref() else {
            return vec![];
        };
        parts
            .iter()
            .filter_map(|p| match p {
                AssistantPart::OutputImage { image_url } => Some(image_url.clone()),
                _ => None,
            })
            .collect()
    }

    /// Return all [`AssistantPart::OutputAudio`] parts in the response.
    pub fn output_audio(&self) -> Vec<AudioContent> {
        let Some(AssistantContent::Parts(parts)) = self.content.as_ref() else {
            return vec![];
        };
        parts
            .iter()
            .filter_map(|p| match p {
                AssistantPart::OutputAudio { audio } => Some(audio.clone()),
                _ => None,
            })
            .collect()
    }
}

/// Tool execution result returned to the model.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct ToolMessage {
    /// Result of the tool execution.
    pub content: String,
    /// ID of the tool call this result responds to.
    pub tool_call_id: String,
    /// Optional tool/function name.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

/// Developer message (system-like message for Claude models).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DeveloperMessage {
    /// Developer-specific instructions or context.
    pub content: String,
    /// Optional name for the developer message source.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
}

/// Deprecated legacy function-role message body.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct FunctionMessage {
    pub content: String,
    pub name: String,
}

// ─── Ergonomic constructors ───────────────────────────────────────────────────

impl Message {
    /// Build a user message with multimodal content parts.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::{Message, ContentPart};
    ///
    /// let msg = Message::user_with_parts(vec![
    ///     ContentPart::text("Describe this image:"),
    ///     ContentPart::image_png(b"\x89PNG"),
    /// ]);
    /// ```
    pub fn user_with_parts(parts: Vec<ContentPart>) -> Self {
        Self::User(UserMessage {
            content: UserContent::Parts(parts),
            name: None,
        })
    }

    /// Build a system message with multimodal content parts.
    ///
    /// Most providers accept plain-text system messages; this constructor is
    /// for providers (e.g. Anthropic) that support structured system content.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::{Message, ContentPart};
    ///
    /// let msg = Message::system_with_parts(vec![
    ///     ContentPart::text("You are a helpful assistant."),
    /// ]);
    /// ```
    pub fn system_with_parts(parts: Vec<ContentPart>) -> Self {
        Self::System(SystemMessage {
            content: UserContent::Parts(parts),
            name: None,
        })
    }

    /// Build an assistant message with structured output parts.
    ///
    /// Use when constructing synthetic assistant turns that include images,
    /// audio, or refusals alongside text.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::{Message, AssistantPart};
    ///
    /// let msg = Message::assistant_with_parts(vec![
    ///     AssistantPart::Text { text: "Here is the image:".into() },
    /// ]);
    /// ```
    pub fn assistant_with_parts(parts: Vec<AssistantPart>) -> Self {
        Self::Assistant(AssistantMessage {
            content: Some(AssistantContent::Parts(parts)),
            name: None,
            tool_calls: None,
            refusal: None,
            function_call: None,
        })
    }
}

impl ContentPart {
    /// Create a text content part.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    ///
    /// let part = ContentPart::text("Hello, world!");
    /// ```
    pub fn text(s: impl Into<String>) -> Self {
        Self::Text { text: s.into() }
    }

    /// Create an image content part from a data URL or HTTP/HTTPS URL.
    ///
    /// Both `image_data_url` and `image_url` produce identical output —
    /// `ImageUrl { url, detail: None }`. The two names exist for caller
    /// clarity: use `image_data_url` when passing a `data:` URI and
    /// `image_url` when passing an HTTPS URL.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    /// use liter_llm::image::{encode_data_url, IMAGE_PNG};
    ///
    /// let url = encode_data_url(b"\x89PNG", Some(IMAGE_PNG));
    /// let part = ContentPart::image_data_url(url);
    /// ```
    pub fn image_data_url(url: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: ImageUrl {
                url: url.into(),
                detail: None,
            },
        }
    }

    /// Create an image content part from an HTTP/HTTPS URL.
    ///
    /// Both `image_url` and `image_data_url` produce identical output —
    /// `ImageUrl { url, detail: None }`. The two names exist for caller
    /// clarity: use `image_url` when passing an HTTPS URL and
    /// `image_data_url` when passing a `data:` URI.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    ///
    /// let part = ContentPart::image_url("https://example.com/photo.jpg");
    /// ```
    pub fn image_url(url: impl Into<String>) -> Self {
        Self::ImageUrl {
            image_url: ImageUrl {
                url: url.into(),
                detail: None,
            },
        }
    }

    /// Create an image content part with an explicit [`ImageDetail`] level.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::{ContentPart, ImageDetail};
    ///
    /// let part = ContentPart::image_with_detail(
    ///     "https://example.com/photo.jpg",
    ///     ImageDetail::High,
    /// );
    /// ```
    pub fn image_with_detail(url: impl Into<String>, detail: ImageDetail) -> Self {
        Self::ImageUrl {
            image_url: ImageUrl {
                url: url.into(),
                detail: Some(detail),
            },
        }
    }

    /// Create an image content part from raw PNG bytes, encoding as a data URL.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    ///
    /// let part = ContentPart::image_png(b"\x89PNG\r\n\x1a\n");
    /// ```
    pub fn image_png(bytes: &[u8]) -> Self {
        Self::image_data_url(crate::image::encode_data_url(bytes, Some(crate::image::IMAGE_PNG)))
    }

    /// Create an image content part from raw JPEG bytes, encoding as a data URL.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    ///
    /// let part = ContentPart::image_jpeg(b"\xff\xd8\xff");
    /// ```
    pub fn image_jpeg(bytes: &[u8]) -> Self {
        Self::image_data_url(crate::image::encode_data_url(bytes, Some(crate::image::IMAGE_JPEG)))
    }

    /// Create an image content part from raw WebP bytes, encoding as a data URL.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    ///
    /// let part = ContentPart::image_webp(b"RIFF");
    /// ```
    pub fn image_webp(bytes: &[u8]) -> Self {
        Self::image_data_url(crate::image::encode_data_url(bytes, Some(crate::image::IMAGE_WEBP)))
    }

    /// Create an image content part from raw TIFF bytes, encoding as a data URL.
    ///
    /// # Example
    ///
    /// ```
    /// use liter_llm::types::ContentPart;
    ///
    /// let part = ContentPart::image_tiff(b"II*\0");
    /// ```
    pub fn image_tiff(bytes: &[u8]) -> Self {
        Self::image_data_url(crate::image::encode_data_url(bytes, Some(crate::image::IMAGE_TIFF)))
    }
}

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

    #[test]
    fn content_part_text_constructor() {
        let part = ContentPart::text("hi");
        let json = serde_json::to_string(&part).expect("serialization should not fail");
        assert_eq!(json, r#"{"type":"text","text":"hi"}"#);
    }

    #[test]
    fn content_part_image_data_url_constructor() {
        let part = ContentPart::image_data_url("data:image/png;base64,aGk=");
        let json = serde_json::to_string(&part).expect("serialization should not fail");
        assert_eq!(
            json,
            r#"{"type":"image_url","image_url":{"url":"data:image/png;base64,aGk="}}"#
        );
    }

    #[test]
    fn content_part_image_with_detail() {
        let part = ContentPart::image_with_detail("https://example.com/img.png", ImageDetail::High);
        let json = serde_json::to_string(&part).expect("serialization should not fail");
        assert_eq!(
            json,
            r#"{"type":"image_url","image_url":{"url":"https://example.com/img.png","detail":"high"}}"#
        );
    }

    #[test]
    fn content_part_image_png_round_trip() {
        let part = ContentPart::image_png(b"hi");
        match &part {
            ContentPart::ImageUrl { image_url } => {
                assert!(
                    image_url.url.starts_with("data:image/png;base64,"),
                    "expected png data URL, got: {}",
                    image_url.url
                );
            }
            other => panic!("expected ImageUrl variant, got: {other:?}"),
        }
    }

    #[test]
    fn message_user_with_parts() {
        let msg = Message::user_with_parts(vec![
            ContentPart::text("hello"),
            ContentPart::image_data_url("data:image/png;base64,aGk="),
        ]);
        let json = serde_json::to_string(&msg).expect("serialization should not fail");
        assert_eq!(
            json,
            r#"{"role":"user","content":[{"type":"text","text":"hello"},{"type":"image_url","image_url":{"url":"data:image/png;base64,aGk="}}]}"#
        );
    }

    // ── ResponseFormat / JsonSchemaFormat constructors ──────────────────────

    #[test]
    fn json_schema_new_defaults_strict_true() {
        let fmt = JsonSchemaFormat::new("S", serde_json::json!({}));
        assert_eq!(fmt.strict, Some(true));
        assert_eq!(fmt.description, None);
        assert_eq!(fmt.name, "S");
    }

    #[test]
    fn json_schema_strict_toggle() {
        let fmt = JsonSchemaFormat::new("S", serde_json::json!({})).strict(false);
        assert_eq!(fmt.strict, Some(false));
    }

    #[test]
    fn json_schema_description_attaches() {
        let fmt = JsonSchemaFormat::new("S", serde_json::json!({})).description("d");
        assert_eq!(fmt.description.as_deref(), Some("d"));
    }

    #[test]
    fn response_format_json_schema_serializes() {
        let fmt = ResponseFormat::json_schema("S", serde_json::json!({"type": "object"}));
        let value = serde_json::to_value(&fmt).expect("serialization must succeed");
        assert_eq!(
            value,
            serde_json::json!({
                "type": "json_schema",
                "json_schema": {
                    "name": "S",
                    "schema": {"type": "object"},
                    "strict": true
                }
            })
        );
        // description must be absent (skip_serializing_if = "Option::is_none")
        assert!(value["json_schema"].get("description").is_none());
    }

    #[test]
    fn response_format_json_object_serializes() {
        let value = serde_json::to_value(ResponseFormat::json_object()).expect("serialization must succeed");
        assert_eq!(value, serde_json::json!({"type": "json_object"}));
    }

    #[test]
    fn response_format_text_serializes() {
        let value = serde_json::to_value(ResponseFormat::text()).expect("serialization must succeed");
        assert_eq!(value, serde_json::json!({"type": "text"}));
    }

    #[test]
    fn chat_request_serializes_response_format() {
        use crate::types::chat::ChatCompletionRequest;
        let request = ChatCompletionRequest {
            model: "gpt-4o".into(),
            messages: vec![],
            response_format: Some(ResponseFormat::json_schema(
                "PersonSchema",
                serde_json::json!({"type": "object", "properties": {"name": {"type": "string"}}}),
            )),
            ..Default::default()
        };
        let value = serde_json::to_value(&request).expect("serialization must succeed");
        let rf = &value["response_format"];
        assert_eq!(rf["type"], "json_schema");
        assert_eq!(rf["json_schema"]["name"], "PersonSchema");
        assert_eq!(rf["json_schema"]["strict"], true);
    }

    // ── AssistantContent / AssistantPart ─────────────────────────────────────

    #[test]
    fn assistant_content_text_deserializes_from_scalar_string() {
        let json = r#"{"role":"assistant","content":"hi"}"#;
        let msg: Message = serde_json::from_str(json).expect("must deserialise");
        let Message::Assistant(a) = msg else {
            panic!("expected assistant")
        };
        assert_eq!(a.content, Some(AssistantContent::Text("hi".into())));
    }

    #[test]
    fn assistant_content_parts_deserializes_from_array() {
        let json = r#"{"role":"assistant","content":[{"type":"text","text":"hi"}]}"#;
        let msg: Message = serde_json::from_str(json).expect("must deserialise");
        let Message::Assistant(a) = msg else {
            panic!("expected assistant")
        };
        assert_eq!(
            a.content,
            Some(AssistantContent::Parts(vec![AssistantPart::Text { text: "hi".into() }]))
        );
    }

    #[test]
    fn assistant_content_display_renders_text() {
        assert_eq!(AssistantContent::Text("hi there".into()).to_string(), "hi there");
        let parts = AssistantContent::Parts(vec![
            AssistantPart::Text { text: "a".into() },
            AssistantPart::OutputImage {
                image_url: ImageUrl::default(),
            },
            AssistantPart::Text { text: "b".into() },
        ]);
        assert_eq!(parts.to_string(), "ab");
        // Image-only / non-text content renders as an empty string.
        let image_only = AssistantContent::Parts(vec![AssistantPart::OutputImage {
            image_url: ImageUrl::default(),
        }]);
        assert_eq!(image_only.to_string(), "");
    }

    #[test]
    fn assistant_message_text_helper_with_text() {
        let a = AssistantMessage {
            content: Some(AssistantContent::Text("hello".into())),
            ..Default::default()
        };
        assert_eq!(a.text(), Some("hello".into()));
    }

    #[test]
    fn assistant_message_text_helper_with_parts() {
        let a = AssistantMessage {
            content: Some(AssistantContent::Parts(vec![
                AssistantPart::Text { text: "foo".into() },
                AssistantPart::Text { text: "bar".into() },
            ])),
            ..Default::default()
        };
        assert_eq!(a.text(), Some("foobar".into()));
    }

    #[test]
    fn assistant_message_text_helper_with_refusal_only_is_none() {
        let a = AssistantMessage {
            content: Some(AssistantContent::Parts(vec![AssistantPart::Refusal {
                refusal: "I cannot do that.".into(),
            }])),
            ..Default::default()
        };
        assert_eq!(a.text(), None);
    }

    #[test]
    fn assistant_part_output_image_serializes() {
        let part = AssistantPart::OutputImage {
            image_url: ImageUrl {
                url: "data:image/png;base64,aGk=".into(),
                detail: None,
            },
        };
        let json = serde_json::to_string(&part).expect("must serialise");
        assert_eq!(
            json,
            r#"{"type":"output_image","image_url":{"url":"data:image/png;base64,aGk="}}"#
        );
    }

    #[test]
    fn assistant_part_output_audio_serializes() {
        let part = AssistantPart::OutputAudio {
            audio: AudioContent {
                data: "aGk=".into(),
                format: "wav".into(),
            },
        };
        let json = serde_json::to_string(&part).expect("must serialise");
        assert_eq!(
            json,
            r#"{"type":"output_audio","audio":{"data":"aGk=","format":"wav"}}"#
        );
    }

    #[test]
    fn message_system_with_parts_serializes() {
        let msg = Message::system_with_parts(vec![ContentPart::text("You are helpful.")]);
        let json = serde_json::to_string(&msg).expect("must serialise");
        assert_eq!(
            json,
            r#"{"role":"system","content":[{"type":"text","text":"You are helpful."}]}"#
        );
    }

    #[test]
    fn message_assistant_with_parts_round_trips() {
        let msg = Message::assistant_with_parts(vec![AssistantPart::Text { text: "ok".into() }]);
        let json = serde_json::to_string(&msg).expect("must serialise");
        assert_eq!(json, r#"{"role":"assistant","content":[{"type":"text","text":"ok"}]}"#);
    }

    #[test]
    fn assistant_output_images_and_audio_helpers() {
        let url = ImageUrl {
            url: "data:image/png;base64,aGk=".into(),
            detail: None,
        };
        let audio = AudioContent {
            data: "aGk=".into(),
            format: "wav".into(),
        };
        let a = AssistantMessage {
            content: Some(AssistantContent::Parts(vec![
                AssistantPart::OutputImage { image_url: url.clone() },
                AssistantPart::OutputAudio { audio: audio.clone() },
            ])),
            ..Default::default()
        };
        assert_eq!(a.output_images(), vec![url.clone()]);
        assert_eq!(a.output_audio(), vec![audio.clone()]);
    }

    #[test]
    fn system_message_content_from_string_back_compat() {
        // Providers that return `"content": "plain text"` for system messages
        // must still round-trip via the untagged UserContent.
        let json = r#"{"role":"system","content":"You are a helpful assistant."}"#;
        let msg: Message = serde_json::from_str(json).expect("must deserialise");
        let Message::System(s) = msg else {
            panic!("expected system")
        };
        assert_eq!(s.content, UserContent::Text("You are a helpful assistant.".into()));
    }
}

// ─── Tools ───────────────────────────────────────────────────────────────────

/// The type discriminator for tool/tool-call objects.
///
/// Per the OpenAI spec this is always `"function"`. Using an enum enforces
/// that constraint at the type level and rejects any other value on
/// deserialization.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum ToolType {
    #[default]
    #[serde(rename = "function")]
    Function,
}

/// A tool the model can invoke (currently, all tools are functions).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ChatCompletionTool {
    /// Tool type (always "function" in OpenAI spec).
    #[serde(rename = "type")]
    pub tool_type: ToolType,
    /// Function definition with name, description, and JSON schema parameters.
    pub function: FunctionDefinition,
}

/// Function definition exposed to the model.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FunctionDefinition {
    /// Name of the function. Required and must be alphanumeric + underscores.
    pub name: String,
    /// Human-readable description explaining what the function does.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// JSON Schema defining the function's parameters.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub parameters: Option<serde_json::Value>,
    /// If true, enforce strict JSON schema validation for arguments.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
}

/// A tool call the model wants to execute.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ToolCall {
    /// Unique ID for this call, used to reference in tool result messages.
    pub id: String,
    /// Tool type (always "function").
    #[serde(rename = "type")]
    pub call_type: ToolType,
    /// Function name and arguments.
    pub function: FunctionCall,
}

/// Function call details.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FunctionCall {
    /// Function name.
    pub name: String,
    /// Arguments as a JSON string (parse with serde_json::from_str).
    pub arguments: String,
}

// ─── Tool Choice ─────────────────────────────────────────────────────────────

/// Tool usage mode or a specific tool to call.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ToolChoice {
    /// Predefined mode: auto, required, or none.
    Mode(ToolChoiceMode),
    /// Force a specific tool to be called.
    Specific(SpecificToolChoice),
}

#[cfg_attr(alef, alef(skip))]
impl Default for ToolChoice {
    fn default() -> Self {
        Self::Mode(ToolChoiceMode::default())
    }
}

/// Tool choice mode.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ToolChoiceMode {
    /// Model may or may not call tools; default behavior.
    #[default]
    Auto,
    /// Model must call at least one tool.
    Required,
    /// Model must not call any tools.
    None,
}

/// Directive to call a specific tool.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SpecificToolChoice {
    /// Tool type (always "function").
    #[serde(rename = "type")]
    pub choice_type: ToolType,
    /// The specific function to invoke.
    pub function: SpecificFunction,
}

/// Name of the specific function to invoke.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SpecificFunction {
    /// Function name.
    pub name: String,
}

// ─── Response Format ─────────────────────────────────────────────────────────

/// Wire format for the chat completions `response_format` field.
///
/// # Provider mapping
///
/// - **OpenAI** (and OpenAI-compatible providers): emitted verbatim as
///   `{"type": "json_schema", "json_schema": {...}}` per the
///   chat-completions spec.
/// - **Gemini / Vertex AI**: translated to
///   `generationConfig.responseMimeType = "application/json"` and
///   `generationConfig.responseSchema = <schema>`. The `name`,
///   `description`, and `strict` fields are dropped — Gemini's
///   structured-output API does not consume them.
/// - **Anthropic**: no native JSON mode. A system instruction is
///   prepended asking the model to respond with valid JSON.
///   `strict` is advisory only; callers should still validate the
///   returned JSON if the schema is load-bearing.
///
/// # Example
///
/// ```no_run
/// # use liter_llm::types::{ResponseFormat, ChatCompletionRequest};
/// # use serde_json::json;
/// let request = ChatCompletionRequest {
///     model: "gpt-4o".into(),
///     messages: vec![],
///     response_format: Some(ResponseFormat::json_schema(
///         "PersonSchema",
///         json!({ "type": "object", "properties": { "name": { "type": "string" } } }),
///     )),
///     ..Default::default()
/// };
/// # let _ = request;
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ResponseFormat {
    /// Plain text output (default).
    #[default]
    #[serde(rename = "text")]
    Text,
    /// Output must be valid JSON object (no schema validation).
    #[serde(rename = "json_object")]
    JsonObject,
    /// Output must conform to the specified JSON schema.
    #[serde(rename = "json_schema")]
    JsonSchema { json_schema: JsonSchemaFormat },
}

impl ResponseFormat {
    /// Construct a `json_schema` response format with `strict = true`.
    ///
    /// # Example
    ///
    /// ```
    /// # use liter_llm::types::ResponseFormat;
    /// # use serde_json::json;
    /// let fmt = ResponseFormat::json_schema("MySchema", json!({"type": "object"}));
    /// ```
    pub fn json_schema(name: impl Into<String>, schema: serde_json::Value) -> Self {
        Self::JsonSchema {
            json_schema: JsonSchemaFormat::new(name, schema),
        }
    }

    /// Construct a `json_object` response format (unvalidated JSON).
    ///
    /// # Example
    ///
    /// ```
    /// # use liter_llm::types::ResponseFormat;
    /// let fmt = ResponseFormat::json_object();
    /// ```
    pub fn json_object() -> Self {
        Self::JsonObject
    }

    /// Construct a plain-text response format (the default).
    ///
    /// # Example
    ///
    /// ```
    /// # use liter_llm::types::ResponseFormat;
    /// let fmt = ResponseFormat::text();
    /// ```
    pub fn text() -> Self {
        Self::Text
    }
}

/// JSON Schema specification for constrained output.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct JsonSchemaFormat {
    /// Name of the schema (must be unique in the request).
    pub name: String,
    /// Description of what the schema represents.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// JSON Schema object defining the output structure.
    pub schema: serde_json::Value,
    /// If true, enforce strict schema validation.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub strict: Option<bool>,
}

impl JsonSchemaFormat {
    /// Create a strict `json_schema` response format with the given name and schema.
    ///
    /// Defaults: `strict = Some(true)`, `description = None`.
    ///
    /// # Example
    ///
    /// ```
    /// # use liter_llm::types::JsonSchemaFormat;
    /// # use serde_json::json;
    /// let fmt = JsonSchemaFormat::new("PersonSchema", json!({"type": "object"}));
    /// assert_eq!(fmt.strict, Some(true));
    /// ```
    pub fn new(name: impl Into<String>, schema: serde_json::Value) -> Self {
        Self {
            name: name.into(),
            description: None,
            schema,
            strict: Some(true),
        }
    }

    /// Override the strict-mode flag.
    ///
    /// # Example
    ///
    /// ```
    /// # use liter_llm::types::JsonSchemaFormat;
    /// # use serde_json::json;
    /// let fmt = JsonSchemaFormat::new("S", json!({})).strict(false);
    /// assert_eq!(fmt.strict, Some(false));
    /// ```
    // Builder-pattern method: takes owned `self` (not expressible across FFI boundaries).
    #[cfg_attr(alef, alef(skip))]
    #[must_use]
    pub fn strict(mut self, on: bool) -> Self {
        self.strict = Some(on);
        self
    }

    /// Attach a description shown to the model.
    ///
    /// # Example
    ///
    /// ```
    /// # use liter_llm::types::JsonSchemaFormat;
    /// # use serde_json::json;
    /// let fmt = JsonSchemaFormat::new("S", json!({})).description("A person object");
    /// assert_eq!(fmt.description.as_deref(), Some("A person object"));
    /// ```
    // Builder-pattern method: takes owned `self` (not expressible across FFI boundaries).
    // Also, the method name `description` collides with the struct field `description`.
    #[cfg_attr(alef, alef(skip))]
    #[must_use]
    pub fn description(mut self, d: impl Into<String>) -> Self {
        self.description = Some(d.into());
        self
    }
}

// ─── Usage ───────────────────────────────────────────────────────────────────

/// Token-usage accounting returned by the provider on each completion / embedding call.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Usage {
    /// Prompt tokens used. Defaults to 0 when absent (some providers omit this).
    #[serde(default)]
    pub prompt_tokens: u64,
    /// Completion tokens used. Defaults to 0 when absent (e.g. embedding responses).
    #[serde(default)]
    pub completion_tokens: u64,
    /// Total tokens used. Defaults to 0 when absent (some providers omit this).
    #[serde(default)]
    pub total_tokens: u64,
    /// Breakdown of tokens used in the prompt, including cached tokens served
    /// at the provider's discounted cache-read rate. Absent when the provider
    /// does not return prompt-token details.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub prompt_tokens_details: Option<PromptTokensDetails>,
}

/// Breakdown of tokens used in the prompt portion of a request.
///
/// `cached_tokens` is included in `Usage::prompt_tokens` — it is *not* an
/// additional charge on top of the prompt token count. When pricing supports
/// a `cache_read_input_token_cost`, the cached portion is billed at the
/// discounted rate and the remainder at the regular input rate.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct PromptTokensDetails {
    /// Cached tokens present in the prompt. Defaults to 0 when absent.
    #[serde(default)]
    pub cached_tokens: u64,
    /// Audio input tokens present in the prompt. Defaults to 0 when absent.
    #[serde(default)]
    pub audio_tokens: u64,
}

// ─── Stop Sequence ───────────────────────────────────────────────────────────

/// Stop sequence(s) that cause the model to stop generating.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StopSequence {
    /// Single stop sequence.
    Single(String),
    /// Multiple stop sequences.
    Multiple(Vec<String>),
}

#[cfg_attr(alef, alef(skip))]
impl Default for StopSequence {
    fn default() -> Self {
        Self::Single(String::new())
    }
}