rstructor 0.6.0

Get structured, validated data out of LLMs as native Rust structs and enums. Derive a type and rstructor generates the JSON Schema, prompts the model, parses the reply, and retries on validation errors — across OpenAI, Anthropic Claude, Google Gemini, and xAI Grok. The Rust answer to Python's Pydantic + Instructor.
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
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
//! Versioned, privacy-aware fixtures for recording and replaying LLM calls.
//!
//! Wrap a live or mock client in [`FixtureRecorder`], make normal non-streaming
//! calls through the wrapper, and persist the resulting [`Fixture`]. Tests can
//! load that file and replay it through [`ReplayClient`] without a network or
//! API key. Replay is ordered and strict: the operation, prompt, schema, and
//! media metadata must match before a recorded response is consumed.

use std::collections::{BTreeSet, VecDeque};
use std::fmt;
use std::path::Path;
use std::sync::{Arc, Mutex};

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use thiserror::Error;

use crate::ResponseMetadata;
use crate::backend::ModelInfo;
use crate::backend::client::{LLMClient, MediaFile};
use crate::backend::usage::{
    AttemptOutcome, ExtractionReport, GenerateResult, MaterializeFailure, MaterializeReport,
    MaterializeResult, RunUsage, TokenUsage,
};
use crate::error::{ApiErrorKind, RStructorError, Result, StreamErrorKind};
use crate::model::Instructor;
use crate::schema::SchemaType;

/// Current on-disk fixture schema version.
pub const FIXTURE_SCHEMA_VERSION: u32 = 1;

type TextSanitizer = dyn Fn(&str) -> String + Send + Sync + 'static;

/// Mandatory privacy boundary used before requests and responses enter a fixture.
///
/// The callback runs on every retained string. Common credential-shaped JSON
/// fields are also replaced structurally, and inline media bytes are never
/// stored. Build the same sanitizer in replay tests when it changes request
/// text that must be matched.
#[derive(Clone)]
pub struct FixtureSanitizer {
    sanitize_text: Arc<TextSanitizer>,
    redacted_json_keys: BTreeSet<String>,
}

impl FixtureSanitizer {
    /// Create a sanitizer with a required string callback and safe JSON-key defaults.
    pub fn new<F>(sanitize_text: F) -> Self
    where
        F: Fn(&str) -> String + Send + Sync + 'static,
    {
        let redacted_json_keys = [
            "api_key",
            "authorization",
            "cookie",
            "password",
            "secret",
            "set_cookie",
            "token",
        ]
        .into_iter()
        .map(str::to_string)
        .collect();

        Self {
            sanitize_text: Arc::new(sanitize_text),
            redacted_json_keys,
        }
    }

    /// Add case-insensitive JSON keys whose values must be replaced entirely.
    #[must_use]
    pub fn redact_json_keys<I, S>(mut self, keys: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.redacted_json_keys
            .extend(keys.into_iter().map(|key| normalize_json_key(key.as_ref())));
        self
    }

    fn text(&self, text: &str) -> String {
        (self.sanitize_text)(text)
    }

    fn json(&self, value: &Value) -> Value {
        match value {
            Value::Object(object) => Value::Object(
                object
                    .iter()
                    .map(|(key, value)| {
                        let value = if self.redacted_json_keys.contains(&normalize_json_key(key)) {
                            Value::String("[REDACTED]".to_string())
                        } else {
                            self.json(value)
                        };
                        (key.clone(), value)
                    })
                    .collect(),
            ),
            Value::Array(items) => Value::Array(items.iter().map(|item| self.json(item)).collect()),
            Value::String(text) => Value::String(self.text(text)),
            primitive => primitive.clone(),
        }
    }

    fn request(&self, mut request: StoredRequest) -> StoredRequest {
        request.prompt = self.text(&request.prompt);
        request.schema_name = request.schema_name.map(|name| self.text(&name));
        request.schema = request.schema.map(|schema| self.json(&schema));
        for media in &mut request.media {
            media.uri = self.text(&media.uri);
            media.mime_type = self.text(&media.mime_type);
        }
        request
    }

    fn report(&self, mut report: ExtractionReport) -> ExtractionReport {
        sanitize_usage(self, report.final_usage.as_mut());
        if let Some(cumulative) = report.cumulative_usage.as_mut() {
            sanitize_run_usage(self, cumulative);
        }
        for attempt in &mut report.attempts {
            sanitize_usage(self, attempt.usage.as_mut());
            if let AttemptOutcome::Failed { message, .. } = &mut attempt.outcome {
                *message = self.text(message);
            }
            if let Some(response) = attempt.response.as_mut() {
                sanitize_response_metadata(self, response);
            }
        }
        report
    }
}

impl fmt::Debug for FixtureSanitizer {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FixtureSanitizer")
            .field("sanitize_text", &"<caller-provided>")
            .field("redacted_json_keys", &self.redacted_json_keys)
            .finish()
    }
}

fn normalize_json_key(key: &str) -> String {
    key.trim().to_ascii_lowercase().replace(['-', ' '], "_")
}

fn sanitize_usage(sanitizer: &FixtureSanitizer, usage: Option<&mut TokenUsage>) {
    if let Some(usage) = usage {
        usage.model = sanitizer.text(&usage.model);
    }
}

fn sanitize_run_usage(sanitizer: &FixtureSanitizer, usage: &mut RunUsage) {
    let old = std::mem::take(&mut usage.by_model);
    usage.by_model = old
        .into_iter()
        .map(|(model, mut token_usage)| {
            token_usage.model = sanitizer.text(&token_usage.model);
            (sanitizer.text(&model), token_usage)
        })
        .collect();
}

fn sanitize_response_metadata(sanitizer: &FixtureSanitizer, response: &mut ResponseMetadata) {
    for request_id in response.request_ids.values_mut() {
        *request_id = sanitizer.text(request_id);
    }
    if let Some(body) = response.sanitized_body.as_mut() {
        body.text = sanitize_body(sanitizer, &body.text);
    }
}

fn sanitize_body(sanitizer: &FixtureSanitizer, body: &str) -> String {
    serde_json::from_str(body).map_or_else(
        |_| sanitizer.text(body),
        |value| sanitizer.json(&value).to_string(),
    )
}

/// File-format and replay-completion errors for fixtures.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum FixtureError {
    /// The fixture could not be read or written.
    #[error("fixture I/O error: {0}")]
    Io(#[from] std::io::Error),
    /// The fixture JSON was invalid.
    #[error("invalid fixture JSON: {0}")]
    Json(#[from] serde_json::Error),
    /// The fixture was written with an unsupported schema version.
    #[error("unsupported fixture schema version {found}; this build supports {supported}")]
    UnsupportedVersion {
        /// Version found in the fixture.
        found: u32,
        /// Version supported by this library build.
        supported: u32,
    },
    /// A replay finished while recorded interactions remained unused.
    #[error("fixture replay left {remaining} interaction(s) unused")]
    ReplayIncomplete {
        /// Number of unused interactions.
        remaining: usize,
    },
}

/// A versioned collection of sanitized, ordered request/response interactions.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Fixture {
    schema_version: u32,
    interactions: Vec<StoredInteraction>,
}

impl Default for Fixture {
    fn default() -> Self {
        Self::new()
    }
}

impl Fixture {
    /// Create an empty fixture using the current schema version.
    #[must_use]
    pub fn new() -> Self {
        Self {
            schema_version: FIXTURE_SCHEMA_VERSION,
            interactions: Vec::new(),
        }
    }

    /// Return the on-disk schema version.
    #[must_use]
    pub fn schema_version(&self) -> u32 {
        self.schema_version
    }

    /// Number of recorded interactions.
    #[must_use]
    pub fn len(&self) -> usize {
        self.interactions.len()
    }

    /// Whether no interactions have been recorded.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.interactions.is_empty()
    }

    /// Parse and version-check a fixture from JSON.
    pub fn from_json(json: &str) -> std::result::Result<Self, FixtureError> {
        let fixture: Self = serde_json::from_str(json)?;
        fixture.validate_version()?;
        Ok(fixture)
    }

    /// Serialize the fixture as stable, pretty JSON with a trailing newline.
    pub fn to_json(&self) -> std::result::Result<String, FixtureError> {
        self.validate_version()?;
        let mut json = serde_json::to_string_pretty(self)?;
        json.push('\n');
        Ok(json)
    }

    /// Load and version-check a fixture file.
    pub fn load(path: impl AsRef<Path>) -> std::result::Result<Self, FixtureError> {
        Self::from_json(&std::fs::read_to_string(path)?)
    }

    /// Write a fixture as stable, pretty JSON.
    pub fn save(&self, path: impl AsRef<Path>) -> std::result::Result<(), FixtureError> {
        std::fs::write(path, self.to_json()?)?;
        Ok(())
    }

    /// Create a strict offline replay client using identity string sanitization.
    #[must_use]
    pub fn replay(&self) -> ReplayClient {
        self.replay_with_sanitizer(FixtureSanitizer::new(str::to_owned))
    }

    /// Create a replay client that sanitizes incoming requests before matching.
    ///
    /// Use the same sanitizer construction as the recording run when request
    /// prompts or schema string values were transformed.
    #[must_use]
    pub fn replay_with_sanitizer(&self, sanitizer: FixtureSanitizer) -> ReplayClient {
        ReplayClient::new(self.clone(), sanitizer)
    }

    fn validate_version(&self) -> std::result::Result<(), FixtureError> {
        if self.schema_version == FIXTURE_SCHEMA_VERSION {
            Ok(())
        } else {
            Err(FixtureError::UnsupportedVersion {
                found: self.schema_version,
                supported: FIXTURE_SCHEMA_VERSION,
            })
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum StoredOperation {
    Materialize,
    MaterializeWithMedia,
    MaterializeWithMetadata,
    MaterializeWithAttempts,
    MaterializeWithMediaAndAttempts,
    Generate,
    GenerateWithMedia,
    GenerateWithMetadata,
    ListModels,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
struct StoredRequest {
    operation: StoredOperation,
    prompt: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    schema_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    schema: Option<Value>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    media: Vec<StoredMedia>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct StoredMedia {
    uri: String,
    mime_type: String,
    inline_data: bool,
}

impl StoredMedia {
    fn from_media(media: &MediaFile) -> Self {
        Self {
            uri: media.uri.clone(),
            mime_type: media.mime_type.clone(),
            inline_data: media.data.is_some(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct StoredInteraction {
    request: StoredRequest,
    response: StoredResponse,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum StoredResponse {
    Success {
        body: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        usage: Option<TokenUsage>,
        #[serde(skip_serializing_if = "Option::is_none")]
        report: Option<ExtractionReport>,
    },
    Error {
        error: StoredError,
        #[serde(skip_serializing_if = "Option::is_none")]
        report: Option<ExtractionReport>,
    },
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum StoredError {
    Api {
        provider: String,
        api_kind: ApiErrorKind,
        response: Option<Box<ResponseMetadata>>,
    },
    Validation {
        message: String,
    },
    Schema {
        message: String,
    },
    SchemaCompatibility {
        provider: String,
        context: String,
        path: String,
        message: String,
    },
    Serialization {
        message: String,
    },
    OutputDecode {
        path: String,
        message: String,
    },
    ToolArgumentDecode {
        path: String,
        message: String,
    },
    Streaming {
        stream_kind: StreamErrorKind,
        message: String,
    },
    Timeout,
    Unsupported {
        message: String,
    },
}

impl StoredError {
    fn from_error(error: &RStructorError, sanitizer: &FixtureSanitizer) -> Self {
        match error {
            RStructorError::ApiError {
                provider,
                kind,
                response,
            } => {
                let mut kind = kind.clone();
                sanitize_api_error_kind(sanitizer, &mut kind);
                let mut response = response.clone();
                if let Some(response) = response.as_mut() {
                    sanitize_response_metadata(sanitizer, response);
                }
                Self::Api {
                    provider: sanitizer.text(provider),
                    api_kind: kind,
                    response,
                }
            }
            RStructorError::ValidationError(message) => Self::Validation {
                message: sanitizer.text(message),
            },
            RStructorError::SchemaError(message) => Self::Schema {
                message: sanitizer.text(message),
            },
            RStructorError::SchemaCompatibilityError {
                provider,
                context,
                path,
                message,
            } => Self::SchemaCompatibility {
                provider: sanitizer.text(provider),
                context: sanitizer.text(context),
                path: sanitizer.text(path),
                message: sanitizer.text(message),
            },
            RStructorError::SerializationError(message) => Self::Serialization {
                message: sanitizer.text(message),
            },
            RStructorError::OutputDecodeError { path, message } => Self::OutputDecode {
                path: sanitizer.text(path),
                message: sanitizer.text(message),
            },
            RStructorError::ToolArgumentDecodeError { path, message } => Self::ToolArgumentDecode {
                path: sanitizer.text(path),
                message: sanitizer.text(message),
            },
            RStructorError::StreamingError { kind, message } => Self::Streaming {
                stream_kind: *kind,
                message: sanitizer.text(message),
            },
            RStructorError::Timeout => Self::Timeout,
            RStructorError::Unsupported(message) => Self::Unsupported {
                message: sanitizer.text(message),
            },
            #[cfg(feature = "_client")]
            RStructorError::HttpError(error) => Self::Unsupported {
                message: sanitizer.text(&format!("recorded HTTP transport error: {error}")),
            },
            RStructorError::JsonError(error) => Self::Serialization {
                message: sanitizer.text(&format!("recorded JSON error: {error}")),
            },
        }
    }

    fn into_error(self) -> RStructorError {
        match self {
            Self::Api {
                provider,
                api_kind,
                response,
            } => RStructorError::ApiError {
                provider,
                kind: api_kind,
                response,
            },
            Self::Validation { message } => RStructorError::ValidationError(message),
            Self::Schema { message } => RStructorError::SchemaError(message),
            Self::SchemaCompatibility {
                provider,
                context,
                path,
                message,
            } => RStructorError::SchemaCompatibilityError {
                provider: provider.into(),
                context: context.into(),
                path: path.into(),
                message: message.into(),
            },
            Self::Serialization { message } => RStructorError::SerializationError(message),
            Self::OutputDecode { path, message } => {
                RStructorError::OutputDecodeError { path, message }
            }
            Self::ToolArgumentDecode { path, message } => {
                RStructorError::ToolArgumentDecodeError { path, message }
            }
            Self::Streaming {
                stream_kind,
                message,
            } => RStructorError::StreamingError {
                kind: stream_kind,
                message: message.into(),
            },
            Self::Timeout => RStructorError::Timeout,
            Self::Unsupported { message } => RStructorError::Unsupported(message),
        }
    }
}

fn sanitize_api_error_kind(sanitizer: &FixtureSanitizer, kind: &mut ApiErrorKind) {
    match kind {
        ApiErrorKind::InvalidModel { model, suggestion } => {
            *model = sanitizer.text(model);
            *suggestion = suggestion.take().map(|value| sanitizer.text(&value));
        }
        ApiErrorKind::BadRequest { details } | ApiErrorKind::UnexpectedResponse { details } => {
            *details = sanitizer.text(details);
        }
        ApiErrorKind::Other { message, .. } => {
            *message = sanitizer.text(message);
        }
        _ => {}
    }
}

fn structured_request<T>(
    operation: StoredOperation,
    prompt: &str,
    media: &[MediaFile],
) -> Result<StoredRequest>
where
    T: Instructor,
{
    Ok(StoredRequest {
        operation,
        prompt: prompt.to_string(),
        schema_name: <T as SchemaType>::schema_name(),
        schema: Some(<T as SchemaType>::try_schema()?.to_json()),
        media: media.iter().map(StoredMedia::from_media).collect(),
    })
}

fn text_request(operation: StoredOperation, prompt: &str, media: &[MediaFile]) -> StoredRequest {
    StoredRequest {
        operation,
        prompt: prompt.to_string(),
        schema_name: None,
        schema: None,
        media: media.iter().map(StoredMedia::from_media).collect(),
    }
}

fn sanitized_structured_body<T>(value: &T, sanitizer: &FixtureSanitizer) -> Result<String>
where
    T: Serialize,
{
    let value = serde_json::to_value(value)
        .map_err(|error| RStructorError::SerializationError(error.to_string()))?;
    serde_json::to_string(&sanitizer.json(&value))
        .map_err(|error| RStructorError::SerializationError(error.to_string()))
}

fn success_report<T>(report: &MaterializeReport<T>) -> ExtractionReport {
    ExtractionReport {
        final_usage: report.final_usage.clone(),
        cumulative_usage: report.cumulative_usage.clone(),
        attempts: report.attempts.clone(),
        attempts_complete: report.attempts_complete,
    }
}

fn failure_report(failure: &MaterializeFailure) -> ExtractionReport {
    ExtractionReport {
        final_usage: failure
            .attempts
            .last()
            .and_then(|attempt| attempt.usage.clone()),
        cumulative_usage: failure.cumulative_usage.clone(),
        attempts: failure.attempts.clone(),
        attempts_complete: failure.attempts_complete,
    }
}

/// An [`LLMClient`] wrapper that records sanitized non-streaming interactions.
///
/// Recording is in memory until [`fixture`](Self::fixture) or [`save`](Self::save)
/// is called. The sanitizer is applied synchronously before an interaction is
/// retained; the recorder never keeps the original prompt or output.
pub struct FixtureRecorder<C> {
    inner: C,
    sanitizer: FixtureSanitizer,
    fixture: Arc<Mutex<Fixture>>,
}

impl<C> FixtureRecorder<C> {
    /// Wrap a client with a mandatory fixture sanitizer.
    #[must_use]
    pub fn new(inner: C, sanitizer: FixtureSanitizer) -> Self {
        Self {
            inner,
            sanitizer,
            fixture: Arc::new(Mutex::new(Fixture::new())),
        }
    }

    /// Borrow the wrapped client.
    #[must_use]
    pub fn inner(&self) -> &C {
        &self.inner
    }

    /// Snapshot the interactions recorded so far.
    #[must_use]
    pub fn fixture(&self) -> Fixture {
        self.fixture.lock().unwrap().clone()
    }

    /// Number of interactions recorded so far.
    #[must_use]
    pub fn len(&self) -> usize {
        self.fixture.lock().unwrap().len()
    }

    /// Whether no interactions have been recorded.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.fixture.lock().unwrap().is_empty()
    }

    /// Persist a snapshot of the interactions recorded so far.
    pub fn save(&self, path: impl AsRef<Path>) -> std::result::Result<(), FixtureError> {
        self.fixture().save(path)
    }

    fn record(&self, request: StoredRequest, response: StoredResponse) {
        self.fixture
            .lock()
            .unwrap()
            .interactions
            .push(StoredInteraction {
                request: self.sanitizer.request(request),
                response,
            });
    }

    fn success(
        &self,
        request: StoredRequest,
        body: String,
        mut usage: Option<TokenUsage>,
        report: Option<ExtractionReport>,
    ) {
        sanitize_usage(&self.sanitizer, usage.as_mut());
        let report = report.map(|report| self.sanitizer.report(report));
        self.record(
            request,
            StoredResponse::Success {
                body,
                usage,
                report,
            },
        );
    }

    fn error(
        &self,
        request: StoredRequest,
        error: &RStructorError,
        report: Option<ExtractionReport>,
    ) {
        let report = report.map(|report| self.sanitizer.report(report));
        self.record(
            request,
            StoredResponse::Error {
                error: StoredError::from_error(error, &self.sanitizer),
                report,
            },
        );
    }
}

impl<C> fmt::Debug for FixtureRecorder<C> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("FixtureRecorder")
            .field("inner", &std::any::type_name::<C>())
            .field("recorded_interactions", &self.len())
            .field("sanitizer", &self.sanitizer)
            .finish()
    }
}

#[async_trait]
impl<C> LLMClient for FixtureRecorder<C>
where
    C: LLMClient + Sync,
{
    async fn materialize<T>(&self, prompt: &str) -> Result<T>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request = structured_request::<T>(StoredOperation::Materialize, prompt, &[])?;
        match self.inner.materialize(prompt).await {
            Ok(data) => {
                let body = sanitized_structured_body(&data, &self.sanitizer)?;
                self.success(request, body, None, None);
                Ok(data)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }

    async fn materialize_with_media<T>(&self, prompt: &str, media: &[MediaFile]) -> Result<T>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request =
            structured_request::<T>(StoredOperation::MaterializeWithMedia, prompt, media)?;
        match self.inner.materialize_with_media(prompt, media).await {
            Ok(data) => {
                let body = sanitized_structured_body(&data, &self.sanitizer)?;
                self.success(request, body, None, None);
                Ok(data)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }

    async fn materialize_with_metadata<T>(&self, prompt: &str) -> Result<MaterializeResult<T>>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request =
            structured_request::<T>(StoredOperation::MaterializeWithMetadata, prompt, &[])?;
        match self.inner.materialize_with_metadata(prompt).await {
            Ok(result) => {
                let body = sanitized_structured_body(&result.data, &self.sanitizer)?;
                self.success(request, body, result.usage.clone(), None);
                Ok(result)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }

    async fn materialize_with_attempts<T>(
        &self,
        prompt: &str,
    ) -> std::result::Result<MaterializeReport<T>, MaterializeFailure>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request =
            structured_request::<T>(StoredOperation::MaterializeWithAttempts, prompt, &[])
                .map_err(MaterializeFailure::from_error)?;
        match self.inner.materialize_with_attempts(prompt).await {
            Ok(report) => {
                let body = sanitized_structured_body(&report.data, &self.sanitizer)
                    .map_err(MaterializeFailure::from_error)?;
                self.success(
                    request,
                    body,
                    report.final_usage.clone(),
                    Some(success_report(&report)),
                );
                Ok(report)
            }
            Err(failure) => {
                self.error(request, failure.error(), Some(failure_report(&failure)));
                Err(failure)
            }
        }
    }

    async fn materialize_with_media_and_attempts<T>(
        &self,
        prompt: &str,
        media: &[MediaFile],
    ) -> std::result::Result<MaterializeReport<T>, MaterializeFailure>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request = structured_request::<T>(
            StoredOperation::MaterializeWithMediaAndAttempts,
            prompt,
            media,
        )
        .map_err(MaterializeFailure::from_error)?;
        match self
            .inner
            .materialize_with_media_and_attempts(prompt, media)
            .await
        {
            Ok(report) => {
                let body = sanitized_structured_body(&report.data, &self.sanitizer)
                    .map_err(MaterializeFailure::from_error)?;
                self.success(
                    request,
                    body,
                    report.final_usage.clone(),
                    Some(success_report(&report)),
                );
                Ok(report)
            }
            Err(failure) => {
                self.error(request, failure.error(), Some(failure_report(&failure)));
                Err(failure)
            }
        }
    }

    async fn generate(&self, prompt: &str) -> Result<String> {
        let request = text_request(StoredOperation::Generate, prompt, &[]);
        match self.inner.generate(prompt).await {
            Ok(text) => {
                self.success(request, self.sanitizer.text(&text), None, None);
                Ok(text)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }

    async fn generate_with_media(&self, prompt: &str, media: &[MediaFile]) -> Result<String> {
        let request = text_request(StoredOperation::GenerateWithMedia, prompt, media);
        match self.inner.generate_with_media(prompt, media).await {
            Ok(text) => {
                self.success(request, self.sanitizer.text(&text), None, None);
                Ok(text)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }

    async fn generate_with_metadata(&self, prompt: &str) -> Result<GenerateResult> {
        let request = text_request(StoredOperation::GenerateWithMetadata, prompt, &[]);
        match self.inner.generate_with_metadata(prompt).await {
            Ok(result) => {
                self.success(
                    request,
                    self.sanitizer.text(&result.text),
                    result.usage.clone(),
                    None,
                );
                Ok(result)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }

    fn from_env() -> Result<Self>
    where
        Self: Sized,
    {
        Err(RStructorError::Unsupported(
            "FixtureRecorder requires an explicit inner client and sanitizer".to_string(),
        ))
    }

    async fn list_models(&self) -> Result<Vec<ModelInfo>> {
        let request = text_request(StoredOperation::ListModels, "", &[]);
        match self.inner.list_models().await {
            Ok(models) => {
                let body = sanitized_structured_body(&models, &self.sanitizer)?;
                self.success(request, body, None, None);
                Ok(models)
            }
            Err(error) => {
                self.error(request, &error, None);
                Err(error)
            }
        }
    }
}

struct ReplayState {
    remaining: Mutex<VecDeque<StoredInteraction>>,
    total: usize,
}

/// Strict, ordered, offline replay of a [`Fixture`].
///
/// Clones share replay position. Call [`assert_finished`](Self::assert_finished)
/// at the end of a test to catch interactions that were silently skipped.
#[derive(Clone)]
pub struct ReplayClient {
    state: Arc<ReplayState>,
    sanitizer: FixtureSanitizer,
}

impl ReplayClient {
    fn new(fixture: Fixture, sanitizer: FixtureSanitizer) -> Self {
        let total = fixture.interactions.len();
        Self {
            state: Arc::new(ReplayState {
                remaining: Mutex::new(fixture.interactions.into()),
                total,
            }),
            sanitizer,
        }
    }

    /// Number of recorded interactions not yet consumed.
    #[must_use]
    pub fn remaining(&self) -> usize {
        self.state.remaining.lock().unwrap().len()
    }

    /// Fail if replay did not consume every recorded interaction.
    pub fn assert_finished(&self) -> std::result::Result<(), FixtureError> {
        let remaining = self.remaining();
        if remaining == 0 {
            Ok(())
        } else {
            Err(FixtureError::ReplayIncomplete { remaining })
        }
    }

    fn take(&self, request: StoredRequest) -> Result<StoredResponse> {
        let request = self.sanitizer.request(request);
        let mut remaining = self.state.remaining.lock().unwrap();
        let interaction_number = self.state.total - remaining.len() + 1;
        let expected = remaining.front().ok_or_else(|| {
            RStructorError::Unsupported(format!(
                "fixture replay received unexpected interaction {interaction_number}: fixture exhausted"
            ))
        })?;
        if let Some(field) = request_mismatch(&expected.request, &request) {
            return Err(RStructorError::Unsupported(format!(
                "fixture request mismatch at interaction {interaction_number}: {field} differs"
            )));
        }
        Ok(remaining.pop_front().unwrap().response)
    }

    fn structured<T>(&self, request: StoredRequest) -> Result<T>
    where
        T: Instructor + serde::de::DeserializeOwned,
    {
        match self.take(request)? {
            StoredResponse::Success { body, .. } => parse_and_validate(&body),
            StoredResponse::Error { error, .. } => Err(error.into_error()),
        }
    }

    fn structured_with_attempts<T>(
        &self,
        request: StoredRequest,
    ) -> std::result::Result<MaterializeReport<T>, MaterializeFailure>
    where
        T: Instructor + serde::de::DeserializeOwned,
    {
        let response = self.take(request).map_err(MaterializeFailure::from_error)?;
        match response {
            StoredResponse::Success {
                body,
                usage,
                report,
            } => {
                let data = parse_and_validate(&body).map_err(MaterializeFailure::from_error)?;
                Ok(match report {
                    Some(report) => MaterializeReport::from_fixture_parts(
                        data,
                        report.final_usage,
                        report.cumulative_usage,
                        report.attempts,
                        report.attempts_complete,
                    ),
                    None => MaterializeReport::from_result(MaterializeResult::new(data, usage)),
                })
            }
            StoredResponse::Error { error, report } => {
                let error = error.into_error();
                Err(match report {
                    Some(report) => MaterializeFailure::from_fixture_parts(
                        error,
                        report.cumulative_usage,
                        report.attempts,
                        report.attempts_complete,
                    ),
                    None => MaterializeFailure::from_error(error),
                })
            }
        }
    }
}

impl fmt::Debug for ReplayClient {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("ReplayClient")
            .field("remaining", &self.remaining())
            .field("total", &self.state.total)
            .finish()
    }
}

fn request_mismatch(expected: &StoredRequest, actual: &StoredRequest) -> Option<&'static str> {
    if expected.operation != actual.operation {
        Some("operation")
    } else if expected.prompt != actual.prompt {
        Some("prompt")
    } else if expected.schema_name != actual.schema_name {
        Some("schema name")
    } else if expected.schema != actual.schema {
        Some("schema")
    } else if expected.media != actual.media {
        Some("media metadata")
    } else {
        None
    }
}

fn parse_and_validate<T>(body: &str) -> Result<T>
where
    T: Instructor + serde::de::DeserializeOwned,
{
    let value: T = crate::decode::output_from_str(body)?;
    value.validate()?;
    Ok(value)
}

#[async_trait]
impl LLMClient for ReplayClient {
    async fn materialize<T>(&self, prompt: &str) -> Result<T>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        self.structured(structured_request::<T>(
            StoredOperation::Materialize,
            prompt,
            &[],
        )?)
    }

    async fn materialize_with_media<T>(&self, prompt: &str, media: &[MediaFile]) -> Result<T>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        self.structured(structured_request::<T>(
            StoredOperation::MaterializeWithMedia,
            prompt,
            media,
        )?)
    }

    async fn materialize_with_metadata<T>(&self, prompt: &str) -> Result<MaterializeResult<T>>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request =
            structured_request::<T>(StoredOperation::MaterializeWithMetadata, prompt, &[])?;
        match self.take(request)? {
            StoredResponse::Success { body, usage, .. } => {
                Ok(MaterializeResult::new(parse_and_validate(&body)?, usage))
            }
            StoredResponse::Error { error, .. } => Err(error.into_error()),
        }
    }

    async fn materialize_with_attempts<T>(
        &self,
        prompt: &str,
    ) -> std::result::Result<MaterializeReport<T>, MaterializeFailure>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request =
            structured_request::<T>(StoredOperation::MaterializeWithAttempts, prompt, &[])
                .map_err(MaterializeFailure::from_error)?;
        self.structured_with_attempts(request)
    }

    async fn materialize_with_media_and_attempts<T>(
        &self,
        prompt: &str,
        media: &[MediaFile],
    ) -> std::result::Result<MaterializeReport<T>, MaterializeFailure>
    where
        T: Instructor + serde::de::DeserializeOwned + Send + 'static,
    {
        let request = structured_request::<T>(
            StoredOperation::MaterializeWithMediaAndAttempts,
            prompt,
            media,
        )
        .map_err(MaterializeFailure::from_error)?;
        self.structured_with_attempts(request)
    }

    async fn generate(&self, prompt: &str) -> Result<String> {
        match self.take(text_request(StoredOperation::Generate, prompt, &[]))? {
            StoredResponse::Success { body, .. } => Ok(body),
            StoredResponse::Error { error, .. } => Err(error.into_error()),
        }
    }

    async fn generate_with_media(&self, prompt: &str, media: &[MediaFile]) -> Result<String> {
        match self.take(text_request(
            StoredOperation::GenerateWithMedia,
            prompt,
            media,
        ))? {
            StoredResponse::Success { body, .. } => Ok(body),
            StoredResponse::Error { error, .. } => Err(error.into_error()),
        }
    }

    async fn generate_with_metadata(&self, prompt: &str) -> Result<GenerateResult> {
        match self.take(text_request(
            StoredOperation::GenerateWithMetadata,
            prompt,
            &[],
        ))? {
            StoredResponse::Success { body, usage, .. } => Ok(GenerateResult::new(body, usage)),
            StoredResponse::Error { error, .. } => Err(error.into_error()),
        }
    }

    fn from_env() -> Result<Self>
    where
        Self: Sized,
    {
        Err(RStructorError::Unsupported(
            "ReplayClient must be created from a Fixture".to_string(),
        ))
    }

    async fn list_models(&self) -> Result<Vec<ModelInfo>> {
        match self.take(text_request(StoredOperation::ListModels, "", &[]))? {
            StoredResponse::Success { body, .. } => serde_json::from_str(&body)
                .map_err(|error| RStructorError::SerializationError(error.to_string())),
            StoredResponse::Error { error, .. } => Err(error.into_error()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Instructor, LLMClient, MockClient};
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Clone, PartialEq, Instructor, Serialize, Deserialize)]
    struct Position {
        account_id: String,
        symbol: String,
        quantity: i64,
    }

    fn sanitizer() -> FixtureSanitizer {
        FixtureSanitizer::new(|text| text.replace("HF-ALPHA-001", "[ACCOUNT]"))
    }

    #[tokio::test]
    async fn records_sanitized_report_and_replays_strictly() {
        let usage = TokenUsage::new("gpt-5.6-2026-07-15", 82, 19);
        let inner = MockClient::new()
            .with_response_and_usage(
                r#"{"account_id":"HF-ALPHA-001","symbol":"AAPL","quantity":125000}"#,
                usage.clone(),
            )
            .with_retries(0);
        let recorder = FixtureRecorder::new(inner, sanitizer());

        let extraction = recorder
            .extract_with_report::<Position>("HF-ALPHA-001 owns 125,000 AAPL shares")
            .await
            .unwrap();
        assert_eq!(extraction.report.final_usage, Some(usage));

        let fixture = recorder.fixture();
        let json = fixture.to_json().unwrap();
        assert!(!json.contains("HF-ALPHA-001"));
        assert!(json.contains("[ACCOUNT]"));

        let replay = fixture.replay_with_sanitizer(sanitizer());
        let replayed = replay
            .extract_with_report::<Position>("HF-ALPHA-001 owns 125,000 AAPL shares")
            .await
            .unwrap();
        assert_eq!(replayed.data.account_id, "[ACCOUNT]");
        assert_eq!(replayed.report, extraction.report);
        replay.assert_finished().unwrap();
    }

    #[tokio::test]
    async fn request_mismatch_does_not_consume_or_echo_prompts() {
        let inner = MockClient::new().with_response("approved");
        let recorder = FixtureRecorder::new(inner, sanitizer());
        recorder.generate("approve HF-ALPHA-001").await.unwrap();
        let replay = recorder.fixture().replay_with_sanitizer(sanitizer());

        let error = replay.generate("reject HF-ALPHA-001").await.unwrap_err();
        let message = error.to_string();
        assert!(message.contains("prompt differs"));
        assert!(!message.contains("approve"));
        assert!(!message.contains("reject"));
        assert_eq!(replay.remaining(), 1);
    }

    #[test]
    fn common_sensitive_json_keys_and_inline_media_are_not_retained() {
        let sanitizer = FixtureSanitizer::new(str::to_owned);
        let value = serde_json::json!({
            "authorization": "Bearer live-secret",
            "nested": {"api-key": "live-key", "safe": "kept"}
        });
        let sanitized = sanitizer.json(&value);
        assert_eq!(sanitized["authorization"], "[REDACTED]");
        assert_eq!(sanitized["nested"]["api-key"], "[REDACTED]");
        assert_eq!(sanitized["nested"]["safe"], "kept");

        let media = MediaFile {
            uri: String::new(),
            mime_type: "image/png".to_string(),
            data: Some("base64-secret".to_string()),
        };
        let stored = StoredMedia::from_media(&media);
        let json = serde_json::to_string(&stored).unwrap();
        assert!(stored.inline_data);
        assert!(!json.contains("base64-secret"));
    }

    #[test]
    fn rejects_unknown_schema_version_and_malformed_json() {
        let unsupported = r#"{"schema_version":99,"interactions":[]}"#;
        assert!(matches!(
            Fixture::from_json(unsupported),
            Err(FixtureError::UnsupportedVersion {
                found: 99,
                supported: FIXTURE_SCHEMA_VERSION
            })
        ));
        assert!(matches!(
            Fixture::from_json("{"),
            Err(FixtureError::Json(_))
        ));
    }

    #[tokio::test]
    async fn typed_api_error_round_trips_with_sanitized_diagnostics() {
        let mut metadata = ResponseMetadata::new(429);
        metadata.request_ids.insert(
            "x-request-id".to_string(),
            "HF-ALPHA-001-request".to_string(),
        );
        let error = RStructorError::api_error_with_response(
            "OpenAI",
            ApiErrorKind::RateLimited { retry_after: None },
            metadata,
        );
        let inner = MockClient::new().with_error(error);
        let recorder = FixtureRecorder::new(inner, sanitizer());
        let original = recorder.generate("HF-ALPHA-001").await.unwrap_err();
        assert_eq!(original.status_code(), Some(429));

        let replay = recorder.fixture().replay_with_sanitizer(sanitizer());
        let replayed = replay.generate("HF-ALPHA-001").await.unwrap_err();
        assert_eq!(replayed.status_code(), Some(429));
        assert_eq!(replayed.request_id(), Some("[ACCOUNT]-request"));
        replay.assert_finished().unwrap();
    }

    #[test]
    fn incomplete_replay_reports_remaining_interactions() {
        let fixture = Fixture {
            schema_version: FIXTURE_SCHEMA_VERSION,
            interactions: vec![StoredInteraction {
                request: text_request(StoredOperation::Generate, "hello", &[]),
                response: StoredResponse::Success {
                    body: "world".to_string(),
                    usage: None,
                    report: None,
                },
            }],
        };
        assert!(matches!(
            fixture.replay().assert_finished(),
            Err(FixtureError::ReplayIncomplete { remaining: 1 })
        ));
    }
}