llmposter 0.5.0

Drop-in mock server for OpenAI, Anthropic & Gemini APIs — library or standalone CLI. SSE streaming, tool calling, OAuth2, failure injection, streaming chaos, stateful scenarios, request capture, hot-reload, response templating, record/replay (VCR). Test LLM apps without burning tokens.
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
//! Per-provider response extraction: turn a real upstream JSON response
//! into a [`RecordedFixture`].
//!
//! Value-path based on purpose — llmposter's typed response structs
//! reject real-API variants (e.g. Anthropic `thinking` blocks, extra
//! Gemini usage fields), so extraction walks the raw `serde_json::Value`
//! and takes only what the fixture schema can replay.

use crate::format::Provider;

use super::{
    RecordedFixture, RecordedMatch, RecordedResponse, RecordedToolCall, RECORDED_PRIORITY,
};

/// Disambiguates OpenAI-provider endpoints that share `Provider::OpenAI`
/// but have different response shapes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum OpenAiEndpoint {
    Chat,
    Completions,
    Embeddings,
}

impl OpenAiEndpoint {
    /// Derive the endpoint from the forwarded request path. Anything
    /// other than the two special OpenAI paths maps to `Chat` — non-OpenAI
    /// providers never consult the value. A new OpenAI-provider route
    /// must add an arm here or it silently falls through to the Chat
    /// extractor and never records.
    pub(crate) fn from_path(path: &str) -> Self {
        match path {
            "/v1/completions" => Self::Completions,
            "/v1/embeddings" => Self::Embeddings,
            _ => Self::Chat,
        }
    }
}

/// Dispatch to the right extractor. `endpoint` discriminates the OpenAI
/// endpoints that share `Provider::OpenAI` (chat completions, legacy
/// completions, embeddings); other providers ignore it.
pub(crate) fn extract_for(
    provider: Provider,
    endpoint: OpenAiEndpoint,
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    match provider {
        Provider::OpenAI => match endpoint {
            OpenAiEndpoint::Chat => extract_openai(body, model, user_message),
            OpenAiEndpoint::Completions => extract_completions(body, model, user_message),
            OpenAiEndpoint::Embeddings => extract_embeddings(body, model, user_message),
        },
        Provider::Anthropic => extract_anthropic(body, model, user_message),
        Provider::Gemini => extract_gemini(body, model, user_message),
        Provider::Responses => extract_responses(body, model, user_message),
    }
}

/// OpenAI Chat Completions: `choices[0].message` carries either text
/// `content` or `tool_calls[*].function.{name,arguments}` (arguments as a
/// JSON-encoded string).
pub(crate) fn extract_openai(
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    let choice = body.get("choices")?.get(0)?;
    let message = choice.get("message")?;
    let mut tool_calls = Vec::new();
    if let Some(calls) = message.get("tool_calls").and_then(|v| v.as_array()) {
        for call in calls {
            let function = call.get("function");
            let Some(name) = function
                .and_then(|f| f.get("name"))
                .and_then(|n| n.as_str())
            else {
                continue;
            };
            tool_calls.push(RecordedToolCall {
                name: name.to_string(),
                arguments: string_args_or_warn(name, function.and_then(|f| f.get("arguments"))),
            });
        }
    }
    let content = message
        .get("content")
        .and_then(|c| c.as_str())
        .filter(|s| !s.is_empty())
        .map(str::to_string);
    let finish_reason = choice
        .get("finish_reason")
        .and_then(|r| r.as_str())
        .map(str::to_string);
    finish(
        Provider::OpenAI,
        model,
        user_message,
        content,
        tool_calls,
        None,
        finish_reason,
    )
}

/// Anthropic Messages: `content` is a block list — `text` blocks are
/// concatenated, `tool_use` blocks become tool calls, and everything
/// else (`thinking`, `redacted_thinking`, ...) is skipped.
pub(crate) fn extract_anthropic(
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    let blocks = body.get("content")?.as_array()?;
    let mut text = String::new();
    let mut tool_calls = Vec::new();
    for block in blocks {
        match block.get("type").and_then(|t| t.as_str()) {
            Some("text") => {
                if let Some(t) = block.get("text").and_then(|t| t.as_str()) {
                    text.push_str(t);
                }
            }
            Some("tool_use") => {
                let Some(name) = block.get("name").and_then(|n| n.as_str()) else {
                    continue;
                };
                tool_calls.push(RecordedToolCall {
                    name: name.to_string(),
                    arguments: block
                        .get("input")
                        .and_then(parse_args)
                        .unwrap_or_else(|| serde_json::json!({})),
                });
            }
            _ => {} // thinking / redacted_thinking / unknown — skip
        }
    }
    let stop_reason = body
        .get("stop_reason")
        .and_then(|r| r.as_str())
        .map(str::to_string);
    finish(
        Provider::Anthropic,
        model,
        user_message,
        Some(text).filter(|t| !t.is_empty()),
        tool_calls,
        stop_reason,
        None,
    )
}

/// Gemini generateContent: `candidates[0].content.parts` — `text` parts
/// are concatenated, `functionCall` parts become tool calls;
/// `candidates[0].finishReason` records like every other provider's
/// terminal reason.
pub(crate) fn extract_gemini(
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    let candidate = body.get("candidates")?.get(0)?;
    let parts = candidate.get("content")?.get("parts")?.as_array()?;
    let mut text = String::new();
    let mut tool_calls = Vec::new();
    accumulate_gemini_parts(parts, &mut text, &mut tool_calls);
    let finish_reason = candidate
        .get("finishReason")
        .and_then(|r| r.as_str())
        .map(str::to_string);
    finish(
        Provider::Gemini,
        model,
        user_message,
        Some(text).filter(|t| !t.is_empty()),
        tool_calls,
        None,
        finish_reason,
    )
}

/// OpenAI Responses API: `output` is an item list — `message` items
/// carry `content[*].output_text` text, `function_call` items carry
/// `{name, arguments}` (arguments as a JSON-encoded string). Other item
/// types (`reasoning`, `web_search_call`, ...) are skipped.
pub(crate) fn extract_responses(
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    // A 2xx body can still be a non-terminal result (`status:
    // "incomplete"` with `incomplete_details`, `"failed"`, ...). Replay
    // rebuilds `status: "completed"`, so recording one would hand
    // clients the wrong terminal state — pass it through unrecorded
    // instead. An ABSENT status still records: some OpenAI-compatible
    // gateways omit the field. (The streaming reassembler is already
    // safe — it only records from a `response.completed` event.)
    match body.get("status") {
        None => {}
        Some(s) if s.as_str() == Some("completed") => {}
        Some(_) => return None,
    }
    let output = body.get("output")?.as_array()?;
    let mut text = String::new();
    let mut tool_calls = Vec::new();
    for item in output {
        match item.get("type").and_then(|t| t.as_str()) {
            Some("message") => {
                let Some(content) = item.get("content").and_then(|c| c.as_array()) else {
                    continue;
                };
                for part in content {
                    if part.get("type").and_then(|t| t.as_str()) == Some("output_text") {
                        if let Some(t) = part.get("text").and_then(|t| t.as_str()) {
                            text.push_str(t);
                        }
                    }
                }
            }
            Some("function_call") => {
                let Some(name) = item.get("name").and_then(|n| n.as_str()) else {
                    continue;
                };
                tool_calls.push(RecordedToolCall {
                    name: name.to_string(),
                    arguments: string_args_or_warn(name, item.get("arguments")),
                });
            }
            _ => {} // reasoning / tool outputs / unknown — skip
        }
    }
    finish(
        Provider::Responses,
        model,
        user_message,
        Some(text).filter(|t| !t.is_empty()),
        tool_calls,
        None,
        None,
    )
}

/// Legacy text completions: `choices[0].text`, preserved verbatim
/// (leading whitespace is significant for completion continuations).
pub(crate) fn extract_completions(
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    let choice = body.get("choices")?.get(0)?;
    let content = choice
        .get("text")
        .and_then(|t| t.as_str())
        .filter(|s| !s.is_empty())
        .map(str::to_string);
    let finish_reason = choice
        .get("finish_reason")
        .and_then(|r| r.as_str())
        .map(str::to_string);
    finish(
        Provider::OpenAI,
        model,
        user_message,
        content,
        Vec::new(),
        None,
        finish_reason,
    )
}

/// OpenAI Embeddings: `data` must hold EXACTLY one entry — the fixture
/// schema stores a single vector, so multi-input responses (one entry
/// per input) pass through unrecorded. Every vector value must be a JSON
/// number; anything else (e.g. base64-encoded floats from
/// `encoding_format: "base64"`) yields `None`.
pub(crate) fn extract_embeddings(
    body: &serde_json::Value,
    model: &str,
    user_message: &str,
) -> Option<RecordedFixture> {
    let data = body.get("data")?.as_array()?;
    if data.len() != 1 {
        return None;
    }
    let values = data[0].get("embedding")?.as_array()?;
    let embedding = values
        .iter()
        .map(serde_json::Value::as_f64)
        .collect::<Option<Vec<f64>>>()?;
    let mut rec = base(Provider::OpenAI, model, user_message);
    rec.response.embedding = Some(embedding);
    Some(rec)
}

/// Walk Gemini `content.parts`: `text` parts append to `text`,
/// `functionCall` parts (with a name — nameless calls are dropped)
/// append to `tool_calls`; missing `args` records `{}` (zero-arg calls
/// are legitimate). Shared with `reassemble.rs`.
pub(super) fn accumulate_gemini_parts(
    parts: &[serde_json::Value],
    text: &mut String,
    tool_calls: &mut Vec<RecordedToolCall>,
) {
    for part in parts {
        if let Some(t) = part.get("text").and_then(|t| t.as_str()) {
            text.push_str(t);
        }
        if let Some(call) = part.get("functionCall") {
            let Some(name) = call.get("name").and_then(|n| n.as_str()) else {
                continue;
            };
            tool_calls.push(RecordedToolCall {
                name: name.to_string(),
                arguments: call
                    .get("args")
                    .and_then(parse_args)
                    .unwrap_or_else(|| serde_json::json!({})),
            });
        }
    }
}

/// Resolve OpenAI-family tool-call arguments, which arrive as a
/// JSON-encoded STRING on the wire. A string that fails to parse as a
/// JSON object records `{}` and warns on stderr — the tool-call NAME and
/// the fact only, never the argument content (it could contain secrets).
/// Absent arguments and already-an-object arguments stay silent (absent
/// args are legitimate, e.g. Gemini zero-arg function calls).
/// Shared with `reassemble.rs`.
pub(super) fn string_args_or_warn(
    name: &str,
    args: Option<&serde_json::Value>,
) -> serde_json::Value {
    let Some(v) = args else {
        return serde_json::json!({});
    };
    match parse_args(v) {
        Some(parsed) => parsed,
        None => {
            if v.is_string() {
                eprintln!(
                    "[llmposter] record mode: tool call '{}' had unparseable arguments — \
                     recorded as {{}}",
                    name
                );
            }
            serde_json::json!({})
        }
    }
}

/// Assemble the final fixture. Tool calls WIN over text when both are
/// present — the fixture schema is content XOR tool_calls, and a
/// response that called tools replays most faithfully as a tool call.
/// Returns `None` when neither is present (nothing replayable).
/// Shared with `reassemble.rs`.
pub(super) fn finish(
    provider: Provider,
    model: &str,
    user_message: &str,
    content: Option<String>,
    tool_calls: Vec<RecordedToolCall>,
    stop_reason: Option<String>,
    finish_reason: Option<String>,
) -> Option<RecordedFixture> {
    let mut rec = base(provider, model, user_message);
    if !tool_calls.is_empty() {
        rec.response.tool_calls = Some(tool_calls);
    } else if content.is_some() {
        rec.response.content = content;
    } else {
        return None;
    }
    rec.response.stop_reason = stop_reason;
    rec.response.finish_reason = finish_reason;
    Some(rec)
}

/// Shared fixture skeleton. Takes the [`Provider`] ENUM so a wrong
/// provider string is unrepresentable — `RecordedFixture.provider`
/// stays `&'static str` fed only from `Provider::as_str()`.
fn base(provider: Provider, model: &str, user_message: &str) -> RecordedFixture {
    RecordedFixture {
        match_rule: RecordedMatch {
            user_message: user_message.to_string(),
            model: model.to_string(),
        },
        provider: provider.as_str(),
        priority: RECORDED_PRIORITY,
        response: RecordedResponse::default(),
    }
}

/// Normalize tool-call arguments to a JSON object: OpenAI-family APIs
/// send them as a JSON-encoded STRING, Anthropic/Gemini as an object.
/// Strings that don't parse to an object (and non-string/non-object
/// values) yield `None`; callers fall back to an empty object so a
/// tool call with mangled arguments still records its name.
/// Shared with `reassemble.rs`.
pub(super) fn parse_args(v: &serde_json::Value) -> Option<serde_json::Value> {
    match v {
        serde_json::Value::String(s) => serde_json::from_str::<serde_json::Value>(s)
            .ok()
            .filter(|parsed| parsed.is_object()),
        serde_json::Value::Object(_) => Some(v.clone()),
        _ => None,
    }
}

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

    #[test]
    fn should_extract_openai_text_response() {
        let body = json!({
            "id": "chatcmpl-AIdRnXqrjJXgTom1yzM6ZUX4A9CqB",
            "object": "chat.completion",
            "created": 1728933352,
            "model": "gpt-4o-2024-08-06",
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": "hello!",
                    "refusal": null
                },
                "logprobs": null,
                "finish_reason": "stop"
            }],
            "usage": {
                "prompt_tokens": 19,
                "completion_tokens": 10,
                "total_tokens": 29,
                "completion_tokens_details": { "reasoning_tokens": 0 }
            },
            "system_fingerprint": "fp_6b68a8204b"
        });
        let rec = extract_openai(&body, "gpt-4o", "say hello").unwrap();
        assert_eq!(rec.provider, "openai");
        assert_eq!(rec.match_rule.model, "gpt-4o");
        assert_eq!(rec.match_rule.user_message, "say hello");
        assert_eq!(rec.response.content.as_deref(), Some("hello!"));
        assert_eq!(rec.response.finish_reason.as_deref(), Some("stop"));
        assert!(rec.response.tool_calls.is_none());
    }

    #[test]
    fn should_extract_openai_tool_calls_with_string_arguments() {
        let body = json!({
            "id": "chatcmpl-abc123",
            "object": "chat.completion",
            "created": 1699896916,
            "model": "gpt-4o-2024-08-06",
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "call_abc123",
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "arguments": "{\"city\":\"SF\"}"
                        }
                    }]
                },
                "logprobs": null,
                "finish_reason": "tool_calls"
            }],
            "usage": { "prompt_tokens": 82, "completion_tokens": 17, "total_tokens": 99 }
        });
        let rec = extract_openai(&body, "gpt-4o", "weather?").unwrap();
        let calls = rec.response.tool_calls.as_ref().unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "get_weather");
        assert_eq!(calls[0].arguments["city"], "SF");
        assert!(
            rec.response.content.is_none(),
            "tool calls win; content stays None"
        );
    }

    #[test]
    fn should_extract_anthropic_skipping_thinking_blocks_and_preferring_tools() {
        let body = json!({
            "id": "msg_01XFDUDYJgAACzvnptvVoYEL",
            "type": "message",
            "role": "assistant",
            "model": "claude-sonnet-4-6",
            "content": [
                {
                    "type": "thinking",
                    "thinking": "The user wants a lookup; I should call the tool.",
                    "signature": "EqQBCgIYAhIM1gbcDa9GJwZA2b3hGgxBdjrkzLoky3dl1pk"
                },
                { "type": "text", "text": "Let me check." },
                {
                    "type": "tool_use",
                    "id": "toolu_01A09q90qw90lq917835lq9",
                    "name": "lookup",
                    "input": { "q": "x" }
                }
            ],
            "stop_reason": "tool_use",
            "stop_sequence": null,
            "usage": { "input_tokens": 599, "output_tokens": 152 }
        });
        let rec = extract_anthropic(&body, "claude-sonnet-4-6", "look up x").unwrap();
        assert_eq!(rec.provider, "anthropic");
        let calls = rec.response.tool_calls.as_ref().unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "lookup");
        assert_eq!(calls[0].arguments["q"], "x");
        assert!(rec.response.content.is_none(), "tools win over text");
        assert_eq!(rec.response.stop_reason.as_deref(), Some("tool_use"));
    }

    #[test]
    fn should_extract_anthropic_text_only() {
        let body = json!({
            "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
            "type": "message",
            "role": "assistant",
            "model": "claude-sonnet-4-6",
            "content": [
                { "type": "text", "text": "part one " },
                { "type": "text", "text": "part two" }
            ],
            "stop_reason": "end_turn",
            "stop_sequence": null,
            "usage": { "input_tokens": 10, "output_tokens": 25 }
        });
        let rec = extract_anthropic(&body, "claude-sonnet-4-6", "two parts").unwrap();
        assert_eq!(rec.response.content.as_deref(), Some("part one part two"));
        assert!(rec.response.tool_calls.is_none());
        assert_eq!(rec.response.stop_reason.as_deref(), Some("end_turn"));
    }

    #[test]
    fn should_extract_gemini_with_extra_real_fields() {
        let body = json!({
            "candidates": [{
                "content": {
                    "parts": [{ "text": "answer" }],
                    "role": "model"
                },
                "finishReason": "STOP",
                "avgLogprobs": -0.003405,
                "index": 0
            }],
            "usageMetadata": {
                "promptTokenCount": 4,
                "candidatesTokenCount": 5,
                "totalTokenCount": 9,
                "promptTokensDetails": [{ "modality": "TEXT", "tokenCount": 4 }]
            },
            "modelVersion": "gemini-2.5-flash",
            "responseId": "wp5rZ_KNGpyO2PgPn5uD8Ac"
        });
        let rec = extract_gemini(&body, "gemini-2.5-flash", "ask").unwrap();
        assert_eq!(rec.provider, "gemini");
        assert_eq!(rec.response.content.as_deref(), Some("answer"));
        assert!(rec.response.tool_calls.is_none());
        assert_eq!(
            rec.response.finish_reason.as_deref(),
            Some("STOP"),
            "candidates[0].finishReason is recorded like every other provider's terminal reason"
        );
    }

    #[test]
    fn should_extract_gemini_function_call() {
        let body = json!({
            "candidates": [{
                "content": {
                    "parts": [{
                        "functionCall": {
                            "name": "get_weather",
                            "args": { "city": "SF" }
                        }
                    }],
                    "role": "model"
                },
                "finishReason": "STOP",
                "index": 0
            }],
            "usageMetadata": { "promptTokenCount": 8, "totalTokenCount": 12 }
        });
        let rec = extract_gemini(&body, "gemini-2.5-flash", "weather?").unwrap();
        let calls = rec.response.tool_calls.as_ref().unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "get_weather");
        assert_eq!(calls[0].arguments["city"], "SF");
        assert!(rec.response.content.is_none());
        assert_eq!(rec.response.finish_reason.as_deref(), Some("STOP"));
    }

    #[test]
    fn should_extract_responses_api_text_and_function_call() {
        let text_body = json!({
            "id": "resp_67ccd2bed1ec8190b14f964abc054267",
            "object": "response",
            "created_at": 1741476542,
            "status": "completed",
            "model": "gpt-4o-2024-08-06",
            "output": [
                {
                    "type": "reasoning",
                    "id": "rs_67ccd2bf17f0819081ff3bb2cf6508e6",
                    "summary": []
                },
                {
                    "type": "message",
                    "id": "msg_67ccd2bf17f0819081ff3bb2cf6508e6",
                    "status": "completed",
                    "role": "assistant",
                    "content": [{
                        "type": "output_text",
                        "text": "resp text",
                        "annotations": []
                    }]
                }
            ]
        });
        let rec = extract_responses(&text_body, "gpt-4o", "respond").unwrap();
        assert_eq!(rec.provider, "responses");
        assert_eq!(rec.response.content.as_deref(), Some("resp text"));
        assert!(rec.response.tool_calls.is_none());

        let tool_body = json!({
            "id": "resp_67ca09c5efe0819096d0511c92b8c890",
            "object": "response",
            "created_at": 1741294021,
            "status": "completed",
            "model": "gpt-4o-2024-08-06",
            "output": [{
                "type": "function_call",
                "id": "fc_67ca09c6bedc8190a7abfec07b1a1332",
                "call_id": "call_unLAR8MvFNptuiZK6K6HCy5k",
                "name": "get_weather",
                "arguments": "{\"city\":\"SF\"}",
                "status": "completed"
            }]
        });
        let rec = extract_responses(&tool_body, "gpt-4o", "weather?").unwrap();
        let calls = rec.response.tool_calls.as_ref().unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "get_weather");
        assert_eq!(
            calls[0].arguments["city"], "SF",
            "string args are PARSED to an object"
        );
        assert!(rec.response.content.is_none());
    }

    #[test]
    fn should_not_record_incomplete_responses_status() {
        // A 2xx Responses body can still be a truncated result. Replay
        // rebuilds `status: "completed"`, so a body declaring any
        // non-completed status must pass through unrecorded — the wrong
        // terminal state would break clients that branch on truncation.
        let incomplete = json!({
            "id": "resp_67ccd2bed1ec8190b14f964abc054267",
            "object": "response",
            "created_at": 1741476542,
            "status": "incomplete",
            "incomplete_details": { "reason": "max_output_tokens" },
            "model": "gpt-4o-2024-08-06",
            "output": [{
                "type": "message",
                "id": "msg_1",
                "status": "incomplete",
                "role": "assistant",
                "content": [{
                    "type": "output_text",
                    "text": "truncated par",
                    "annotations": []
                }]
            }]
        });
        assert!(
            extract_responses(&incomplete, "gpt-4o", "long ask").is_none(),
            "incomplete status must not record"
        );

        // Defensive: some OpenAI-compatible gateways omit `status`
        // entirely — absence still records (current behavior preserved).
        let no_status = json!({
            "object": "response",
            "output": [{
                "type": "message",
                "id": "msg_2",
                "role": "assistant",
                "content": [{ "type": "output_text", "text": "ok" }]
            }]
        });
        let rec = extract_responses(&no_status, "gpt-4o", "ask").unwrap();
        assert_eq!(rec.response.content.as_deref(), Some("ok"));
    }

    #[test]
    fn should_extract_completions_text() {
        let body = json!({
            "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
            "object": "text_completion",
            "created": 1589478378,
            "model": "davinci-002",
            "choices": [{
                "text": " legacy completion",
                "index": 0,
                "logprobs": null,
                "finish_reason": "length"
            }],
            "usage": { "prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12 }
        });
        let rec = extract_completions(&body, "davinci-002", "legacy prompt").unwrap();
        assert_eq!(rec.provider, "openai");
        assert_eq!(
            rec.response.content.as_deref(),
            Some(" legacy completion"),
            "content preserved verbatim, including leading whitespace"
        );
        assert_eq!(rec.response.finish_reason.as_deref(), Some("length"));
    }

    #[test]
    fn should_record_empty_args_when_tool_arguments_unparseable() {
        // OpenAI: string arguments that fail to parse as a JSON object
        // fall back to {} (with a stderr warning naming the tool call).
        let body = json!({
            "id": "chatcmpl-badargs",
            "object": "chat.completion",
            "created": 1699896916,
            "model": "gpt-4o-2024-08-06",
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": null,
                    "tool_calls": [{
                        "id": "call_badargs",
                        "type": "function",
                        "function": {
                            "name": "get_weather",
                            "arguments": "not valid json {"
                        }
                    }]
                },
                "logprobs": null,
                "finish_reason": "tool_calls"
            }]
        });
        let rec = extract_openai(&body, "gpt-4o", "weather?").unwrap();
        let calls = rec.response.tool_calls.as_ref().unwrap();
        assert_eq!(calls[0].name, "get_weather");
        assert_eq!(calls[0].arguments, json!({}));

        // Gemini: ABSENT args are legitimate (zero-arg function call) —
        // {} is recorded silently, no warning.
        let gemini_body = json!({
            "candidates": [{
                "content": {
                    "parts": [{ "functionCall": { "name": "refresh" } }],
                    "role": "model"
                },
                "finishReason": "STOP",
                "index": 0
            }]
        });
        let rec = extract_gemini(&gemini_body, "gemini-2.5-flash", "refresh it").unwrap();
        let calls = rec.response.tool_calls.as_ref().unwrap();
        assert_eq!(calls[0].name, "refresh");
        assert_eq!(calls[0].arguments, json!({}));
    }

    #[test]
    fn should_return_none_for_unextractable_real_api_variants() {
        // OpenAI: array-of-parts content (annotated-content variant) is
        // not a plain string — nothing the fixture schema can replay.
        let openai_parts = json!({
            "object": "chat.completion",
            "choices": [{
                "index": 0,
                "message": {
                    "role": "assistant",
                    "content": [{ "type": "text", "text": "chunked" }]
                },
                "finish_reason": "stop"
            }]
        });
        assert!(extract_openai(&openai_parts, "m", "u").is_none());

        // Anthropic: thinking-only content — no text, no tools.
        let thinking_only = json!({
            "type": "message",
            "role": "assistant",
            "content": [{
                "type": "thinking",
                "thinking": "hmm, tricky",
                "signature": "sig"
            }],
            "stop_reason": "end_turn"
        });
        assert!(extract_anthropic(&thinking_only, "m", "u").is_none());

        // Gemini: SAFETY-blocked candidate has NO content key at all.
        let safety_blocked = json!({
            "candidates": [{
                "finishReason": "SAFETY",
                "safetyRatings": [{
                    "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
                    "probability": "HIGH"
                }],
                "index": 0
            }]
        });
        assert!(extract_gemini(&safety_blocked, "m", "u").is_none());

        // Responses: message item with an EMPTY content array.
        let empty_message = json!({
            "object": "response",
            "output": [{
                "type": "message",
                "id": "msg_1",
                "status": "completed",
                "role": "assistant",
                "content": []
            }]
        });
        assert!(extract_responses(&empty_message, "m", "u").is_none());
    }

    #[test]
    fn should_extract_single_embedding_and_skip_multi() {
        let single = json!({
            "object": "list",
            "data": [{
                "object": "embedding",
                "index": 0,
                "embedding": [0.1, -0.2, 0.3]
            }],
            "model": "text-embedding-3-small",
            "usage": { "prompt_tokens": 2, "total_tokens": 2 }
        });
        let rec = extract_embeddings(&single, "text-embedding-3-small", "some text").unwrap();
        assert_eq!(rec.provider, "openai");
        assert_eq!(rec.match_rule.model, "text-embedding-3-small");
        assert_eq!(rec.match_rule.user_message, "some text");
        assert_eq!(
            rec.response.embedding.as_ref().unwrap(),
            &vec![0.1, -0.2, 0.3]
        );
        assert!(rec.response.content.is_none());
        assert!(rec.response.tool_calls.is_none());

        // Multi-input responses carry one data entry per input — the
        // fixture schema holds ONE vector, so these pass through unrecorded.
        let multi = json!({ "data": [{ "embedding": [0.1] }, { "embedding": [0.2] }] });
        assert!(extract_embeddings(&multi, "m", "q").is_none());
    }

    #[test]
    fn should_return_none_for_malformed_embedding_data() {
        // No data key at all.
        assert!(extract_embeddings(&json!({ "object": "list" }), "m", "q").is_none());
        // Empty data array.
        assert!(extract_embeddings(&json!({ "data": [] }), "m", "q").is_none());
        // Entry without an embedding key.
        assert!(extract_embeddings(&json!({ "data": [{ "index": 0 }] }), "m", "q").is_none());
        // Non-numeric vector values (e.g. base64-encoded floats).
        let base64 = json!({ "data": [{ "embedding": "AACAPwAAAEA=" }] });
        assert!(extract_embeddings(&base64, "m", "q").is_none());
        let mixed = json!({ "data": [{ "embedding": [0.1, "x"] }] });
        assert!(extract_embeddings(&mixed, "m", "q").is_none());
    }

    #[test]
    fn should_map_paths_to_openai_endpoints() {
        assert_eq!(
            OpenAiEndpoint::from_path("/v1/completions"),
            OpenAiEndpoint::Completions
        );
        assert_eq!(
            OpenAiEndpoint::from_path("/v1/embeddings"),
            OpenAiEndpoint::Embeddings
        );
        assert_eq!(
            OpenAiEndpoint::from_path("/v1/chat/completions"),
            OpenAiEndpoint::Chat
        );
        // Non-OpenAI paths never consult the value — Chat is a harmless default.
        assert_eq!(
            OpenAiEndpoint::from_path("/v1/messages"),
            OpenAiEndpoint::Chat
        );
    }

    #[test]
    fn should_return_none_when_nothing_extractable() {
        let empty_choices = json!({ "object": "chat.completion", "choices": [] });
        assert!(extract_openai(&empty_choices, "m", "u").is_none());
        assert!(extract_completions(&empty_choices, "m", "u").is_none());

        let empty_content = json!({ "type": "message", "content": [] });
        assert!(extract_anthropic(&empty_content, "m", "u").is_none());

        let empty_candidates = json!({ "candidates": [] });
        assert!(extract_gemini(&empty_candidates, "m", "u").is_none());

        let empty_output = json!({ "object": "response", "output": [] });
        assert!(extract_responses(&empty_output, "m", "u").is_none());
    }

    #[test]
    fn should_skip_openai_tool_call_without_function_name() {
        // Entries with no function.name (or no function at all) are
        // dropped; the text content still records.
        let body = json!({
            "choices": [{
                "message": {
                    "content": "txt",
                    "tool_calls": [
                        { "function": { "arguments": "{}" } },
                        { "id": "call_nofunction" }
                    ]
                },
                "finish_reason": "stop"
            }]
        });
        let rec = extract_openai(&body, "m", "u").unwrap();
        assert!(rec.response.tool_calls.is_none());
        assert_eq!(rec.response.content.as_deref(), Some("txt"));
    }

    #[test]
    fn should_skip_anthropic_tool_use_without_name() {
        let body = json!({
            "content": [
                { "type": "tool_use", "input": { "a": 1 } },
                { "type": "text", "text": "t" }
            ],
            "stop_reason": "end_turn"
        });
        let rec = extract_anthropic(&body, "m", "u").unwrap();
        assert!(rec.response.tool_calls.is_none());
        assert_eq!(rec.response.content.as_deref(), Some("t"));
    }

    #[test]
    fn should_skip_gemini_function_call_without_name() {
        let body = json!({
            "candidates": [{
                "content": {
                    "parts": [
                        { "functionCall": { "args": { "a": 1 } } },
                        { "text": "t" }
                    ]
                }
            }]
        });
        let rec = extract_gemini(&body, "m", "u").unwrap();
        assert!(rec.response.tool_calls.is_none());
        assert_eq!(rec.response.content.as_deref(), Some("t"));
    }

    #[test]
    fn should_skip_responses_items_with_malformed_content() {
        let body = json!({
            "output": [
                // content not an array — skipped.
                { "type": "message", "content": "not-an-array" },
                // output_text part without text; non-output_text part.
                { "type": "message", "content": [
                    { "type": "output_text" },
                    { "type": "refusal", "refusal": "no" }
                ]},
                // function_call without a name — dropped.
                { "type": "function_call", "arguments": "{}" },
                // The one extractable item.
                { "type": "message", "content": [
                    { "type": "output_text", "text": "ok" }
                ]}
            ]
        });
        let rec = extract_responses(&body, "m", "u").unwrap();
        assert!(rec.response.tool_calls.is_none());
        assert_eq!(rec.response.content.as_deref(), Some("ok"));
    }

    #[test]
    fn should_default_absent_args_to_empty_object_silently() {
        assert_eq!(string_args_or_warn("f", None), json!({}));
    }

    #[test]
    fn should_reject_non_string_non_object_args() {
        assert_eq!(parse_args(&json!(42)), None);
        assert_eq!(parse_args(&json!(["not", "an", "object"])), None);
        assert_eq!(parse_args(&json!(true)), None);
    }
}