firstpass-proxy 0.4.0

Drop-in, Anthropic-compatible LLM proxy that routes each request to the cheapest model that provably passes a quality gate, escalates on failure, and records a tamper-evident audit trace.
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
//! OpenAI **Responses API** (`POST /v1/responses`) as a translation over the Chat Completions
//! path.
//!
//! Newer OpenAI-family agents speak Responses rather than Chat Completions. Without this endpoint
//! they cannot point at Firstpass at all — not "route less well", but fail to connect. That is the
//! only parity gap on the list that locks a whole class of client out.
//!
//! ## Why a translation rather than a second engine
//!
//! The two APIs differ in shape, not in semantics: a list of turns in, a model reply out. Gating,
//! escalation, budgets, and receipts have nothing to do with which envelope carried the turns. So
//! this converts the request into the Chat Completions shape, runs the **existing** enforce
//! pipeline unchanged, and converts the reply back. One translation layer instead of a parallel
//! implementation that could drift on the parts that matter.
//!
//! ## What it does not do yet
//!
//! Streaming. `enforce` is inherently buffered — the gate has to see the whole candidate before it
//! can judge it — so a streaming Responses request is served through the observe passthrough, the
//! same fallback the Chat Completions path uses for a request it cannot gate faithfully. Reasoning
//! items, tool-call round trips, and stateful `previous_response_id` threading are also out of
//! scope here; they are passed through where harmless and otherwise left alone rather than
//! half-translated.

use serde_json::{Map, Value};

/// Convert a Responses request body into a Chat Completions one.
///
/// The pieces that actually move:
/// - `instructions` becomes a leading `system` message.
/// - `input` becomes `messages`. It may be a bare string, a list of turns, or a list of turns whose
///   `content` is itself a list of typed parts — all three are in the wild.
/// - `max_output_tokens` becomes `max_tokens`.
///
/// Unknown top-level fields are carried across untouched: this is a proxy, and a field it does not
/// understand is far more likely to be newer than this code than to be wrong.
#[must_use]
pub fn request_to_chat(body: &Value) -> Value {
    let Some(obj) = body.as_object() else {
        return body.clone();
    };
    let mut out = obj.clone();

    let mut messages: Vec<Value> = Vec::new();
    if let Some(instructions) = obj.get("instructions").and_then(Value::as_str) {
        messages.push(serde_json::json!({ "role": "system", "content": instructions }));
    }
    match obj.get("input") {
        // `input: "..."` — the single-turn shorthand.
        Some(Value::String(s)) => {
            messages.push(serde_json::json!({ "role": "user", "content": s }));
        }
        Some(Value::Array(turns)) => {
            for turn in turns {
                if let Some(m) = turn_to_message(turn) {
                    messages.push(m);
                }
            }
        }
        _ => {}
    }
    // Tool definitions: Responses puts name/parameters on the item; Chat nests them under
    // `function`.
    if let Some(Value::Array(tools)) = obj.get("tools") {
        let converted: Vec<Value> = tools.iter().filter_map(tool_to_chat).collect();
        if converted.len() == tools.len() {
            out.insert("tools".to_owned(), Value::Array(converted));
        }
    }
    // `tool_choice` naming a specific function differs in shape between the two APIs. Passing the
    // Responses spelling through unchanged does not error — Chat simply does not recognise it, so
    // the model is free to ignore a tool the caller demanded. Silent, and exactly the failure this
    // file keeps having to design against.
    if let Some(tc) = obj.get("tool_choice") {
        match tool_choice_to_chat(tc) {
            Some(v) => {
                out.insert("tool_choice".to_owned(), v);
            }
            // Unrepresentable (a hosted-tool choice). `has_untranslatable_content` routes these to
            // passthrough, so dropping here is belt-and-braces rather than the real guard.
            None => {
                out.remove("tool_choice");
            }
        }
    }
    // A caller that sent `messages` directly (some SDKs accept both) keeps them.
    if !messages.is_empty() {
        out.insert("messages".to_owned(), Value::Array(messages));
    }
    out.remove("input");
    out.remove("instructions");
    if let Some(max) = out.remove("max_output_tokens") {
        out.insert("max_tokens".to_owned(), max);
    }
    // Responses spells reasoning effort as a nested object; the provider layer normalizes dialects
    // from the flat field, so hand it the shape it already knows.
    if let Some(effort) = obj
        .get("reasoning")
        .and_then(Value::as_object)
        .and_then(|r| r.get("effort"))
        .and_then(Value::as_str)
    {
        out.insert(
            "reasoning_effort".to_owned(),
            Value::String(effort.to_owned()),
        );
        out.remove("reasoning");
    }
    Value::Object(out)
}

/// One Responses `input` turn → one Chat Completions message.
fn turn_to_message(turn: &Value) -> Option<Value> {
    let obj = turn.as_object()?;
    // Typed items carry no role/content pair — they are the tool round trip.
    match obj.get("type").and_then(Value::as_str) {
        Some("function_call") => return function_call_to_chat(obj),
        Some("function_call_output") => return function_output_to_chat(obj),
        _ => {}
    }
    let role = obj.get("role").and_then(Value::as_str).unwrap_or("user");
    let content = match obj.get("content") {
        Some(Value::String(s)) => Value::String(s.clone()),
        Some(Value::Array(parts)) => {
            // All-text collapses to a plain string (what a Chat client would have sent). Mixed
            // content keeps the typed-part array, with images carried across in Chat's spelling.
            // Dropping them here is not an option: `has_images` is computed from the TRANSLATED
            // body, so a dropped image also erases the signal that would have routed the request
            // to passthrough — and a multimodal request would be gated and served as text-only,
            // silently, having thrown away the thing it was asking about.
            if parts
                .iter()
                .all(|p| p.get("type").and_then(Value::as_str) != Some("input_image"))
            {
                let text: String = parts
                    .iter()
                    .filter_map(|p| p.get("text").and_then(Value::as_str))
                    .collect::<Vec<_>>()
                    .join("");
                Value::String(text)
            } else {
                Value::Array(parts.iter().filter_map(part_to_chat).collect())
            }
        }
        _ => return None,
    };
    Some(serde_json::json!({ "role": role, "content": content }))
}

/// One Responses tool definition → its Chat Completions spelling.
///
/// `{type: function, name, parameters}` → `{type: function, function: {name, parameters}}`.
/// Returns `None` for a tool type with no Chat equivalent (hosted tools like `web_search`), which
/// makes the caller route the whole request to passthrough rather than send a partial tool set —
/// a model missing one of its tools produces a confidently wrong plan.
fn tool_to_chat(tool: &Value) -> Option<Value> {
    let obj = tool.as_object()?;
    // Already in Chat shape (some SDKs send it): pass through.
    if obj.contains_key("function") {
        return Some(tool.clone());
    }
    if obj.get("type").and_then(Value::as_str) != Some("function") {
        return None;
    }
    let mut f = serde_json::Map::new();
    f.insert("name".to_owned(), obj.get("name")?.clone());
    if let Some(d) = obj.get("description") {
        f.insert("description".to_owned(), d.clone());
    }
    if let Some(p) = obj.get("parameters") {
        f.insert("parameters".to_owned(), p.clone());
    }
    if let Some(st) = obj.get("strict") {
        f.insert("strict".to_owned(), st.clone());
    }
    Some(serde_json::json!({ "type": "function", "function": Value::Object(f) }))
}

/// A Responses `tool_choice` → its Chat spelling.
///
/// The string forms (`auto` / `none` / `required`) are identical. Naming a specific function is
/// not: Responses says `{type: "function", name: "f"}`, Chat says
/// `{type: "function", function: {name: "f"}}`.
fn tool_choice_to_chat(tc: &Value) -> Option<Value> {
    match tc {
        Value::String(_) => Some(tc.clone()),
        Value::Object(o) => {
            if o.contains_key("function") {
                return Some(tc.clone()); // already Chat-shaped
            }
            if o.get("type").and_then(Value::as_str) != Some("function") {
                return None; // hosted-tool choice — no Chat equivalent
            }
            Some(serde_json::json!({
                "type": "function", "function": { "name": o.get("name")?.clone() }
            }))
        }
        _ => None,
    }
}

/// A Responses tool-call item → the Chat `assistant` message that carries it.
fn function_call_to_chat(item: &serde_json::Map<String, Value>) -> Option<Value> {
    Some(serde_json::json!({
        "role": "assistant",
        "content": Value::Null,
        "tool_calls": [{
            "id": item.get("call_id").or_else(|| item.get("id"))?.clone(),
            "type": "function",
            "function": {
                "name": item.get("name")?.clone(),
                // Chat wants a JSON string; Responses already sends one.
                "arguments": item.get("arguments").cloned()
                    .unwrap_or_else(|| Value::String("{}".to_owned())),
            },
        }],
    }))
}

/// A Responses tool-result item → the Chat `tool` message that carries it.
fn function_output_to_chat(item: &serde_json::Map<String, Value>) -> Option<Value> {
    let output = match item.get("output") {
        Some(Value::String(s)) => s.clone(),
        Some(other) => other.to_string(),
        None => return None,
    };
    Some(serde_json::json!({
        "role": "tool",
        "tool_call_id": item.get("call_id")?.clone(),
        "content": output,
    }))
}

/// One Responses content part → its Chat Completions spelling.
fn part_to_chat(part: &Value) -> Option<Value> {
    match part.get("type").and_then(Value::as_str) {
        Some("input_text" | "output_text" | "text") => Some(serde_json::json!({
            "type": "text",
            "text": part.get("text").and_then(Value::as_str).unwrap_or_default(),
        })),
        Some("input_image") => {
            // Responses puts the URL directly on the part; Chat nests it under `image_url`.
            let url = part.get("image_url").and_then(|u| {
                u.as_str()
                    .map(str::to_owned)
                    .or_else(|| u.get("url").and_then(Value::as_str).map(str::to_owned))
            })?;
            Some(serde_json::json!({ "type": "image_url", "image_url": { "url": url } }))
        }
        _ => None,
    }
}

/// Whether this request contains anything the translation cannot faithfully round-trip.
///
/// **Allow-list, not deny-list, and deliberately so.** Three separate content types were found to
/// be silently dropped by an earlier deny-list version of this check — images, files, and tool
/// calls — each producing a confidently wrong answer rather than an error. Enumerating what breaks
/// loses that game every time a provider adds a field; enumerating what provably round-trips does
/// not. Anything outside the list takes the passthrough path un-gated.
///
/// Un-gated is a real limitation. Being answered about content that was thrown away is a wrong
/// answer, and between the two this proxy takes the limitation.
///
/// Currently translatable: plain `user`/`assistant`/`system`/`developer` turns whose content is
/// text or text-and-image parts, function tool definitions, and the `function_call` /
/// `function_call_output` round trip — in both directions, so a tool call in the reply comes back
/// as a `function_call` item rather than vanishing.
///
/// Still excluded: **hosted** tools (`web_search`, `file_search`, `computer_use`), which run inside
/// the provider and have no Chat equivalent; reasoning items; and `previous_response_id` threading,
/// where the upstream holds history we do not have, so a translated request is not the conversation
/// the client is continuing.
#[must_use]
pub fn has_untranslatable_content(body: &Value) -> bool {
    // Hosted tools (web_search, file_search, computer_use) run inside the provider and have no
    // Chat Completions equivalent. Sending a partial tool set is worse than not routing: a model
    // missing one of its tools produces a confidently wrong plan.
    if let Some(Value::Array(tools)) = body.get("tools")
        && tools.iter().any(|t| tool_to_chat(t).is_none())
    {
        return true;
    }
    // A `tool_choice` we cannot express would let the model ignore a tool the caller demanded.
    if let Some(tc) = body.get("tool_choice")
        && tool_choice_to_chat(tc).is_none()
    {
        return true;
    }
    // Stateful threading: the upstream holds history we do not have, so a translated request is
    // not the conversation the client is actually continuing.
    if body.get("previous_response_id").is_some() {
        return true;
    }
    let Some(Value::Array(turns)) = body.get("input") else {
        return false;
    };
    turns.iter().any(|turn| {
        let Some(obj) = turn.as_object() else {
            return true;
        };
        // Typed items: ask the translator itself rather than restating its rules here. A
        // `function_call` missing its `call_id` translates to nothing, and a check that only
        // looked at `type` would wave it through and let the turn vanish — the exact silent drop
        // this allow-list exists to prevent. One source of truth cannot drift from itself.
        if obj.contains_key("type") && !obj.contains_key("role") {
            return turn_to_message(turn).is_none();
        }
        if !matches!(
            obj.get("role").and_then(Value::as_str),
            Some("user" | "assistant" | "system" | "developer")
        ) {
            return true;
        }
        match obj.get("content") {
            Some(Value::String(_)) => false,
            Some(Value::Array(parts)) => parts.iter().any(|p| {
                !matches!(
                    p.get("type").and_then(Value::as_str),
                    Some("input_text" | "output_text" | "text" | "input_image")
                )
            }),
            _ => true,
        }
    })
}

/// Convert a Chat Completions response into the Responses shape.
///
/// Reports `usage` with Responses' own field names (`input_tokens` / `output_tokens`), which is
/// what a client reads to display cost — leaving Chat's `prompt_tokens` spelling would make a
/// served request look like it consumed nothing.
#[must_use]
pub fn response_from_chat(chat: &Value) -> Value {
    let text = chat
        .pointer("/choices/0/message/content")
        .and_then(Value::as_str)
        .unwrap_or_default();
    let model = chat
        .get("model")
        .and_then(Value::as_str)
        .unwrap_or_default();
    let id = chat.get("id").and_then(Value::as_str).unwrap_or("resp_0");
    let in_tokens = chat
        .pointer("/usage/prompt_tokens")
        .and_then(Value::as_u64)
        .unwrap_or(0);
    let out_tokens = chat
        .pointer("/usage/completion_tokens")
        .and_then(Value::as_u64)
        .unwrap_or(0);

    let mut resp = Map::new();
    resp.insert("id".to_owned(), Value::String(id.to_owned()));
    resp.insert("object".to_owned(), Value::String("response".to_owned()));
    resp.insert("model".to_owned(), Value::String(model.to_owned()));

    // A reply may carry text, tool calls, or both. Dropping the tool calls here would hand an
    // agent a plausible-looking message where it expected a call to make — the failure being
    // silent is what makes it dangerous.
    let mut output: Vec<Value> = Vec::new();
    if !text.is_empty() {
        output.push(serde_json::json!({
            "type": "message",
            "id": format!("msg_{id}"),
            "status": "completed",
            "role": "assistant",
            "content": [{ "type": "output_text", "text": text, "annotations": [] }],
        }));
    }
    let tool_calls = chat
        .pointer("/choices/0/message/tool_calls")
        .and_then(Value::as_array);
    if let Some(calls) = tool_calls {
        for (i, call) in calls.iter().enumerate() {
            output.push(serde_json::json!({
                "type": "function_call",
                "id": format!("fc_{id}_{i}"),
                "status": "completed",
                "call_id": call.get("id").cloned().unwrap_or(Value::Null),
                "name": call.pointer("/function/name").cloned().unwrap_or(Value::Null),
                "arguments": call
                    .pointer("/function/arguments")
                    .cloned()
                    .unwrap_or_else(|| Value::String("{}".to_owned())),
            }));
        }
    }
    // A reply with neither text nor calls still needs a well-formed message item.
    if output.is_empty() {
        output.push(serde_json::json!({
            "type": "message",
            "id": format!("msg_{id}"),
            "status": "completed",
            "role": "assistant",
            "content": [{ "type": "output_text", "text": "", "annotations": [] }],
        }));
    }
    // `completed` means GENERATION finished, not that the agent loop is done — a response is
    // `completed` even when `output` carries a `function_call` the caller still has to run. The
    // Responses API reserves `incomplete` for a TRUNCATED response, with `incomplete_details`
    // naming the reason.
    //
    // An earlier version of this reported a tool-call turn as `incomplete` by analogy: the model
    // is waiting, so surely it is not complete. That is wrong against the spec and would stall any
    // client that checks `status == "completed"` before reading `output`.
    let truncated = chat
        .pointer("/choices/0/finish_reason")
        .and_then(Value::as_str)
        == Some("length");
    if truncated {
        resp.insert("status".to_owned(), Value::String("incomplete".to_owned()));
        resp.insert(
            "incomplete_details".to_owned(),
            serde_json::json!({ "reason": "max_output_tokens" }),
        );
    } else {
        resp.insert("status".to_owned(), Value::String("completed".to_owned()));
    }
    resp.insert("output".to_owned(), Value::Array(output));
    // The SDK convenience accessor. Populating it means `response.output_text` works rather than
    // returning nothing on a response this proxy produced.
    resp.insert("output_text".to_owned(), Value::String(text.to_owned()));
    resp.insert(
        "usage".to_owned(),
        serde_json::json!({
            "input_tokens": in_tokens,
            "output_tokens": out_tokens,
            "total_tokens": in_tokens + out_tokens,
        }),
    );
    Value::Object(resp)
}

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

    #[test]
    fn the_single_turn_string_shorthand_becomes_a_user_message() {
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5", "input": "write a haiku",
        }));
        assert_eq!(out["messages"][0]["role"], "user");
        assert_eq!(out["messages"][0]["content"], "write a haiku");
        assert!(
            out.get("input").is_none(),
            "the Responses field must not leak downstream"
        );
    }

    #[test]
    fn instructions_become_a_leading_system_message() {
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5",
            "instructions": "be terse",
            "input": [{ "role": "user", "content": "hi" }],
        }));
        assert_eq!(out["messages"][0]["role"], "system");
        assert_eq!(out["messages"][0]["content"], "be terse");
        assert_eq!(out["messages"][1]["role"], "user");
        assert!(out.get("instructions").is_none());
    }

    #[test]
    fn an_image_survives_translation_instead_of_being_silently_dropped() {
        // Caught in review. `has_images` is computed from the TRANSLATED body, so dropping the
        // image here also erased the signal that routes multimodal requests to passthrough — a
        // request with a picture would be gated and answered as text-only, having discarded the
        // very thing it asked about.
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5",
            "input": [{ "role": "user", "content": [
                { "type": "input_text", "text": "what is this?" },
                { "type": "input_image", "image_url": "data:image/png;base64,AAAA" },
            ]}],
        }));

        let parts = out["messages"][0]["content"]
            .as_array()
            .expect("mixed content stays a typed array");
        assert_eq!(parts.len(), 2, "{parts:?}");
        assert_eq!(parts[0]["type"], "text");
        // Chat nests the url; Responses puts it directly on the part.
        assert_eq!(parts[1]["type"], "image_url");
        assert_eq!(parts[1]["image_url"]["url"], "data:image/png;base64,AAAA");
    }

    #[test]
    fn content_we_cannot_represent_is_flagged_for_passthrough() {
        // A file part has no Chat equivalent here. Translating it would quietly produce a smaller
        // request than the client sent, so the caller routes it to passthrough instead.
        let with_file = serde_json::json!({
            "input": [{ "role": "user", "content": [
                { "type": "input_text", "text": "summarise" },
                { "type": "input_file", "file_id": "file_123" },
            ]}],
        });
        assert!(has_untranslatable_content(&with_file));

        let text_and_image = serde_json::json!({
            "input": [{ "role": "user", "content": [
                { "type": "input_text", "text": "hi" },
                { "type": "input_image", "image_url": "data:..." },
            ]}],
        });
        assert!(
            !has_untranslatable_content(&text_and_image),
            "text and images ARE representable — they must still be gated"
        );
        assert!(!has_untranslatable_content(
            &serde_json::json!({ "input": "plain" })
        ));
    }

    #[test]
    fn a_tool_round_trip_survives_in_both_directions() {
        // This is what unblocks gating for agentic clients. Previously the whole request took
        // un-gated passthrough because a tool call had no representation coming back.
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5",
            "tools": [{
                "type": "function", "name": "get_weather",
                "description": "look up weather",
                "parameters": { "type": "object", "properties": { "city": { "type": "string" } } },
            }],
            "input": [
                { "role": "user", "content": "weather in Paris?" },
                { "type": "function_call", "call_id": "call_1", "name": "get_weather",
                  "arguments": "{\"city\":\"Paris\"}" },
                { "type": "function_call_output", "call_id": "call_1", "output": "22C" },
            ],
        }));

        // Tool definition: Responses puts name/parameters on the item, Chat nests under `function`.
        assert_eq!(out["tools"][0]["type"], "function");
        assert_eq!(out["tools"][0]["function"]["name"], "get_weather");
        assert_eq!(out["tools"][0]["function"]["parameters"]["type"], "object");

        let msgs = out["messages"].as_array().expect("messages");
        assert_eq!(msgs.len(), 3);
        assert_eq!(msgs[1]["role"], "assistant");
        assert_eq!(msgs[1]["tool_calls"][0]["id"], "call_1");
        assert_eq!(msgs[1]["tool_calls"][0]["function"]["name"], "get_weather");
        // The tool result becomes a `tool` message keyed by the call id.
        assert_eq!(msgs[2]["role"], "tool");
        assert_eq!(msgs[2]["tool_call_id"], "call_1");
        assert_eq!(msgs[2]["content"], "22C");
    }

    #[test]
    fn a_tool_call_in_the_reply_comes_back_as_a_function_call_item() {
        // Dropping this would hand an agent a plausible-looking message where it expected a call
        // to make. Silent, and therefore dangerous.
        let chat = serde_json::json!({
            "id": "chatcmpl_1", "model": "openai/gpt-5.5",
            "choices": [{ "message": {
                "role": "assistant", "content": null,
                "tool_calls": [{
                    "id": "call_9", "type": "function",
                    "function": { "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" },
                }],
            }}],
            "usage": { "prompt_tokens": 10, "completion_tokens": 5 },
        });

        let out = response_from_chat(&chat);

        let item = &out["output"][0];
        assert_eq!(item["type"], "function_call");
        assert_eq!(item["call_id"], "call_9");
        assert_eq!(item["name"], "get_weather");
        assert_eq!(item["arguments"], "{\"city\":\"Paris\"}");
        // `completed` is correct even with a tool call pending: it means GENERATION finished, not
        // that the agent loop is done. An earlier version reported `incomplete` here by analogy,
        // which is wrong against the spec and stalls any client that gates on `completed`.
        assert_eq!(out["status"], "completed");
    }

    #[test]
    fn text_alongside_a_tool_call_keeps_both() {
        let chat = serde_json::json!({
            "id": "c", "model": "m",
            "choices": [{ "message": {
                "content": "let me check",
                "tool_calls": [{ "id": "c1", "function": { "name": "f", "arguments": "{}" } }],
            }}],
        });

        let out = response_from_chat(&chat);

        assert_eq!(out["output"][0]["type"], "message");
        assert_eq!(out["output"][0]["content"][0]["text"], "let me check");
        assert_eq!(out["output"][1]["type"], "function_call");
    }

    #[test]
    fn a_truncated_reply_is_the_only_incomplete_one() {
        // The Responses API reserves `incomplete` for truncation, with `incomplete_details` naming
        // the reason — which is what `finish_reason: "length"` means on the Chat side.
        let chat = serde_json::json!({
            "id": "c", "model": "m",
            "choices": [{ "finish_reason": "length", "message": { "content": "cut off mid-" } }],
        });

        let out = response_from_chat(&chat);

        assert_eq!(out["status"], "incomplete");
        assert_eq!(out["incomplete_details"]["reason"], "max_output_tokens");
    }

    #[test]
    fn tool_choice_is_translated_not_passed_through_in_the_wrong_shape() {
        // Responses: {type, name}. Chat: {type, function: {name}}. Forwarded unchanged this does
        // not error — Chat just does not recognise it, so the model is free to ignore a tool the
        // caller demanded. Silent, which is the whole problem.
        let out = request_to_chat(&serde_json::json!({
            "input": "hi",
            "tools": [{ "type": "function", "name": "f" }],
            "tool_choice": { "type": "function", "name": "f" },
        }));
        assert_eq!(out["tool_choice"]["type"], "function");
        assert_eq!(out["tool_choice"]["function"]["name"], "f");

        // The string forms are identical in both APIs.
        for word in ["auto", "none", "required"] {
            let out = request_to_chat(&serde_json::json!({
                "input": "hi", "tool_choice": word,
            }));
            assert_eq!(out["tool_choice"], word);
        }
    }

    #[test]
    fn a_tool_choice_we_cannot_express_routes_to_passthrough() {
        assert!(has_untranslatable_content(&serde_json::json!({
            "input": "hi",
            "tool_choice": { "type": "file_search" },
        })));
        assert!(!has_untranslatable_content(&serde_json::json!({
            "input": "hi",
            "tools": [{ "type": "function", "name": "f" }],
            "tool_choice": { "type": "function", "name": "f" },
        })));
    }

    #[test]
    fn hosted_tools_still_take_passthrough() {
        // web_search / file_search run inside the provider. Sending a partial tool set is worse
        // than not routing: a model missing one of its tools makes a confidently wrong plan.
        assert!(has_untranslatable_content(&serde_json::json!({
            "input": "search for it",
            "tools": [
                { "type": "function", "name": "f" },
                { "type": "web_search" },
            ],
        })));
    }

    #[test]
    fn function_tools_are_now_gated_rather_than_passed_through() {
        assert!(!has_untranslatable_content(&serde_json::json!({
            "input": [
                { "role": "user", "content": "weather?" },
                { "type": "function_call", "call_id": "c1", "name": "f", "arguments": "{}" },
                { "type": "function_call_output", "call_id": "c1", "output": "22C" },
            ],
            "tools": [{ "type": "function", "name": "f" }],
        })));
    }

    #[test]
    fn a_malformed_tool_item_routes_to_passthrough_rather_than_vanishing() {
        // Found in review: the gate said `function_call` translates while the translator returned
        // None for one missing its `call_id`, so the turn was silently dropped. The gate now asks
        // the translator, so the two cannot disagree.
        let no_call_id = serde_json::json!({
            "input": [{ "type": "function_call", "name": "f", "arguments": "{}" }],
        });
        assert!(has_untranslatable_content(&no_call_id));

        let no_name = serde_json::json!({
            "input": [{ "type": "function_call", "call_id": "c1", "arguments": "{}" }],
        });
        assert!(has_untranslatable_content(&no_name));

        let no_output = serde_json::json!({
            "input": [{ "type": "function_call_output", "call_id": "c1" }],
        });
        assert!(has_untranslatable_content(&no_output));

        // The well-formed pair still translates and is still gated.
        let ok = serde_json::json!({
            "input": [
                { "type": "function_call", "call_id": "c1", "name": "f", "arguments": "{}" },
                { "type": "function_call_output", "call_id": "c1", "output": "42" },
            ],
        });
        assert!(!has_untranslatable_content(&ok));
    }

    #[test]
    fn every_allowed_turn_actually_produces_a_message() {
        // The invariant behind the fix, stated directly: anything the gate allows must translate
        // to something. If these two ever disagree again, a turn goes missing with no error.
        let body = serde_json::json!({
            "input": [
                { "role": "user", "content": "hi" },
                { "role": "assistant", "content": [{ "type": "output_text", "text": "yes" }] },
                { "type": "function_call", "call_id": "c1", "name": "f", "arguments": "{}" },
                { "type": "function_call_output", "call_id": "c1", "output": "42" },
            ],
        });
        assert!(!has_untranslatable_content(&body));

        let turns = body["input"].as_array().expect("array");
        let out = request_to_chat(&body);
        assert_eq!(
            out["messages"].as_array().expect("messages").len(),
            turns.len(),
            "every allowed turn must survive translation"
        );
    }

    #[test]
    fn reasoning_items_still_take_passthrough() {
        assert!(has_untranslatable_content(&serde_json::json!({
            "input": [{ "type": "reasoning", "summary": [] }],
        })));
    }

    #[test]
    fn a_tool_using_request_is_not_translated_when_hosted() {
        // Function tools now translate both ways, so they are gated. Only provider-hosted tools
        // remain untranslatable.
        assert!(!has_untranslatable_content(&serde_json::json!({
            "input": "hi",
            "tools": [{ "type": "function", "name": "get_weather" }],
        })));
        assert!(has_untranslatable_content(&serde_json::json!({
            "input": "hi",
            "tools": [{ "type": "computer_use_preview" }],
        })));
    }

    #[test]
    fn typed_item_turns_are_not_translated() {
        // function_call / function_call_output / reasoning items carry no role+content pair.
        // The earlier deny-list version returned None for these and dropped the turn entirely.
        for item in [
            // function_call / function_call_output now translate; only these remain.
            serde_json::json!({ "type": "reasoning", "summary": [] }),
            serde_json::json!({ "type": "web_search_call", "id": "ws_1" }),
        ] {
            assert!(
                has_untranslatable_content(&serde_json::json!({ "input": [item.clone()] })),
                "must not translate {item}"
            );
        }
    }

    #[test]
    fn stateful_threading_is_not_translated() {
        // The upstream holds history we do not have, so a translated request is not the
        // conversation the client is actually continuing.
        assert!(has_untranslatable_content(&serde_json::json!({
            "input": "and then?", "previous_response_id": "resp_abc",
        })));
    }

    #[test]
    fn plain_conversations_are_still_translated_and_gated() {
        // The allow-list must not be so tight that nothing gets verified.
        assert!(!has_untranslatable_content(
            &serde_json::json!({ "input": "hi" })
        ));
        assert!(!has_untranslatable_content(&serde_json::json!({
            "input": [
                { "role": "user", "content": "hi" },
                { "role": "assistant", "content": "hello" },
                { "role": "user", "content": [
                    { "type": "input_text", "text": "and this?" },
                    { "type": "input_image", "image_url": "data:..." },
                ]},
            ],
        })));
    }

    #[test]
    fn typed_content_parts_are_flattened_to_text() {
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5",
            "input": [{ "role": "user", "content": [
                { "type": "input_text", "text": "part one " },
                { "type": "input_text", "text": "part two" },
            ]}],
        }));
        assert_eq!(out["messages"][0]["content"], "part one part two");
    }

    #[test]
    fn max_output_tokens_is_renamed_not_dropped() {
        // Dropped, the request would silently run to the provider's default ceiling — a caller
        // capping output would be billed for far more than it asked for.
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5", "input": "hi", "max_output_tokens": 256,
        }));
        assert_eq!(out["max_tokens"], 256);
        assert!(out.get("max_output_tokens").is_none());
    }

    #[test]
    fn nested_reasoning_effort_is_flattened_for_the_provider_layer() {
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5", "input": "hi", "reasoning": { "effort": "high" },
        }));
        assert_eq!(out["reasoning_effort"], "high");
        assert!(out.get("reasoning").is_none());
    }

    #[test]
    fn unknown_fields_survive_the_crossing() {
        // A field this code does not know is likelier to be newer than this code than to be wrong.
        let out = request_to_chat(&serde_json::json!({
            "model": "gpt-5.5", "input": "hi", "metadata": { "run": "abc" }, "store": false,
        }));
        assert_eq!(out["metadata"]["run"], "abc");
        assert_eq!(out["store"], false);
    }

    #[test]
    fn the_reply_carries_text_in_both_places_a_client_looks() {
        let chat = serde_json::json!({
            "id": "chatcmpl_1", "model": "openai/gpt-5.5",
            "choices": [{ "index": 0, "message": { "role": "assistant", "content": "42" } }],
            "usage": { "prompt_tokens": 10, "completion_tokens": 3 },
        });

        let out = response_from_chat(&chat);

        assert_eq!(out["object"], "response");
        assert_eq!(out["status"], "completed");
        assert_eq!(out["output"][0]["content"][0]["text"], "42");
        assert_eq!(out["output"][0]["content"][0]["type"], "output_text");
        // The SDK convenience accessor — without it `response.output_text` is empty.
        assert_eq!(out["output_text"], "42");
    }

    #[test]
    fn usage_is_renamed_or_the_call_looks_free() {
        // Responses clients read `input_tokens`/`output_tokens`. Left in Chat's spelling, a served
        // request displays as having consumed nothing.
        let chat = serde_json::json!({
            "id": "c", "model": "m",
            "choices": [{ "message": { "content": "x" } }],
            "usage": { "prompt_tokens": 1200, "completion_tokens": 300 },
        });

        let out = response_from_chat(&chat);

        assert_eq!(out["usage"]["input_tokens"], 1200);
        assert_eq!(out["usage"]["output_tokens"], 300);
        assert_eq!(out["usage"]["total_tokens"], 1500);
    }

    #[test]
    fn a_round_trip_preserves_the_conversation() {
        let original = serde_json::json!({
            "model": "gpt-5.5",
            "instructions": "be terse",
            "input": [
                { "role": "user", "content": "first" },
                { "role": "assistant", "content": "ok" },
                { "role": "user", "content": "second" },
            ],
        });

        let chat = request_to_chat(&original);
        let msgs = chat["messages"].as_array().expect("messages");

        assert_eq!(msgs.len(), 4, "system + three turns");
        assert_eq!(msgs[3]["content"], "second");
        assert_eq!(msgs[2]["role"], "assistant");
    }
}