allama 0.1.0

Low-level, provider-agnostic LLM abstractions for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
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
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
//! Structured extraction into typed Rust values.
//!
//! # Typical use
//!
//! Derive `Deserialize` and `JsonSchema` for the type, extract it from any
//! chat provider, and tell the failures apart by their reason:
//!
//! ```
//! # #[cfg(feature = "mock")]
//! # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
//! # let provider = allama::mock::MockProvider::with_text(r#"{"author": "Ada", "year": 1843}"#);
//! use allama::extract::{ExtractError, ExtractExt};
//! use allama::schemars::{self, JsonSchema};
//! use allama::{ChatRequest, Error};
//!
//! #[derive(serde::Deserialize, JsonSchema)]
//! struct Program {
//!     author: String,
//!     year: u16,
//! }
//!
//! let request = ChatRequest::new("gpt-5-mini").user("Ada published the first program in 1843.");
//! match provider.extract::<Program>(&request).await {
//!     Ok(extracted) => println!("{} ({})", extracted.value.author, extracted.value.year),
//!     Err(Error::Extract { failure, .. }) => match failure.error() {
//!         ExtractError::Refused { .. } => println!("the model declined"),
//!         ExtractError::Truncated { .. } => println!("cut off: raise max_output_tokens or lower the reasoning effort"),
//!         other => println!("unusable output: {other}"),
//!     },
//!     // A provider error (a rate limit, a timeout) is returned as itself.
//!     Err(error) => return Err(error),
//! }
//! # Ok::<(), Error>(()) }).unwrap();
//! ```
//!
//! To extract from a chat that used tools, see
//! [extracting from a conversation](Extractor#extracting-from-a-conversation):
//! a request with tools fails with [`ExtractError::ToolConflict`].
//!
//! To test code that extracts, script the value the model returns with
//! `MockResponse::structured` (feature `mock`): it answers in the form the
//! extraction asks for, whichever mode it picks.
//!
//! The [`ExtractFailure`] also keeps the output that could not be used
//! ([`response`](ExtractFailure::response)) and what every pass cost
//! ([`metadata`](ExtractFailure::metadata)); see
//! [Bookkeeping](#bookkeeping).
//!
//! An extraction derives its passes from the [`ChatRequest`] it is given
//! and keeps every field (system prompts, messages, sampling settings,
//! provider options) except the ones the selected mode replaces: the
//! response format in `Native` mode, the tools and tool choice in
//! `ForcedTool` mode. A request that carries tools of its own fails with
//! [`ExtractError::ToolConflict`] in **every** mode, before anything is
//! sent: the model would be offered both your tools and the schema, and
//! might call a tool instead of answering, so the same request must not
//! work on one provider and fail on another. To extract from a
//! conversation that used tools, start a fresh request from its messages
//! or clear the tools on a copy; see
//! [extracting from a conversation](Extractor#extracting-from-a-conversation).
//!
//! # Contents
//!
//! - [Choosing a mode](#choosing-a-mode)
//! - [Automatic fallback to forced-tool mode](#automatic-fallback-to-forced-tool-mode)
//! - [Recovery is opt-in](#recovery-is-opt-in)
//! - [Bookkeeping](#bookkeeping)
//! - [Output that is not an answer](#output-that-is-not-an-answer)
//! - [Reasoning models](#reasoning-models)
//! - [Schema enforcement](#schema-enforcement)
//! - [Enforcement floor and plans](#enforcement-floor-and-plans)
//! - [Undeclared keys](#undeclared-keys)
//! - [Target types](#target-types)
//! - [Validating values](#validating-values)
//!
//! # Choosing a mode
//!
//! Extraction derives a provider request that constrains the output to the
//! JSON Schema of `T`, sends it, and parses the result:
//!
//! - [`ExtractionMode::Native`] uses the provider's JSON Schema response
//!   format;
//! - [`ExtractionMode::ForcedTool`] forces a call to a synthetic tool whose
//!   arguments follow the schema;
//! - [`ExtractionMode::Auto`] (default) picks from the provider's capability
//!   answers, a verified mode before a guessed one: `Native` when structured
//!   output is `Supported`; else `ForcedTool` when tool calls and forced tool
//!   choice are both `Supported`; else `Native` when structured output is
//!   `Unknown`; else `ForcedTool` when neither tool answer is `Unsupported`.
//!   When the answers rule out both, it fails with
//!   [`ExtractError::Unsupported`]: set the mode, or settle the answers with
//!   a capability resolver
//!   ([`CapabilityOverrideProvider`](crate::wrappers::CapabilityOverrideProvider),
//!   or the adapter's own `with_chat_capabilities`).
//!
//! # Automatic fallback to forced-tool mode
//!
//! Reporting structured-output support does not mean every schema fits the
//! provider's native output. When `Auto` picked `Native` and the adapter
//! refuses the response format before sending (an [`Error::Unsupported`]
//! whose [`unsupported_field`](Error::unsupported_field) is
//! [`RequestField::ResponseFormat`] and whose
//! [stage](Error::unsupported_stage) is
//! [`BeforeSending`](crate::UnsupportedStage::BeforeSending), such as Anthropic's
//! `output_config.format` refusing a map), the extraction retries the pass
//! in `ForcedTool` mode. On [`ExtractExt::extract_stream`] the refusal may
//! come from the `chat_stream` call or as the stream's first item, before
//! any event; both switch modes. To see that item, `extract_stream` reads
//! the first item before returning whenever `Auto` picked `Native`; the
//! returned stream still yields it.
//!
//! The switch is recorded, not hidden:
//! [`ExtractionMetadata::mode`] reports `ForcedTool` and
//! [`ExtractionMetadata::auto_fallback`] holds the refusal. It adds no
//! [degradation](ExtractionMetadata::degradations): the forced-tool pass
//! reports its own. When the forced-tool attempt fails too before sending
//! (it is refused, or the request carries its own tools), the native
//! refusal is the primary cause: the error is an [`Error::Unsupported`]
//! naming [`RequestField::ResponseFormat`], whose message is about the
//! caller's type and gives the forced-tool failure second ("`Order` cannot
//! be extracted from `m` with its output constrained: native output was
//! refused (...), and forced-tool mode was refused too (...)"). A provider
//! error from the forced-tool call is returned as itself. Either way
//! `auto_fallback` is set in the extraction record. An explicit
//! [`ExtractionMode::Native`] never falls back.
//!
//! **Object roots.** Some providers take only an object at the root of a
//! response schema and of tool parameters (OpenAI, on Chat Completions and
//! Responses). Extracting a type whose schema is not an object there, such
//! as an enum or a `Vec`, fails with [`Error::Unsupported`] before sending,
//! in native and forced-tool mode alike. Wrap the value in a struct
//! (`struct Labeled { label: Label }`), or ask a decision model, which
//! answers choice questions natively
//! ([`DecisionProviderExt::decide_as`](crate::DecisionProviderExt::decide_as)).
//!
//! [`ExtractExt::extract`] is available on every provider and makes exactly
//! one provider call. [`ExtractExt::extract_stream`] streams the same
//! request and parses the value when the stream ends.
//!
//! # Recovery is opt-in
//!
//! An [`Extractor`] runs the same extraction with explicit recovery:
//!
//! - [`Extractor::repair`]: local JSON repair (markdown fences, prose around
//!   the value). No extra provider calls.
//! - [`Extractor::retries`]: when the output cannot be parsed, send the
//!   failure back to the model and ask again. **Each retry is another
//!   billed provider call**, reported in [`ExtractionMetadata::passes`].
//!
//! Nothing retries or repairs unless configured to.
//! [`Extractor::parse`] applies the same parsing and repair to a response
//! you already have, without calling a provider.
//!
//! # Bookkeeping
//!
//! Every extraction reports how it ran in an [`ExtractionMetadata`]: the
//! mode, the number of provider calls ([`passes`](ExtractionMetadata::passes)),
//! their token usage summed ([`usage`](ExtractionMetadata::usage)), and
//! how far the provider reported enforcing the schema
//! ([`enforcement`](ExtractionMetadata::enforcement)). A successful
//! extraction returns it in [`Extracted`]; a failed one returns
//! [`Error::Extract`] with an [`ExtractFailure`] that carries the same record
//! plus the last provider response, so the cost of failed passes and the
//! output that could not be used stay inspectable.
//!
//! A provider error during an extraction (a rate limit on a retry, say) is
//! not an extraction failure: it is returned as itself, so retry and
//! fallback policies still see `RateLimited` or `Timeout`. The record of the
//! passes made before it travels in the error's context; read it with
//! [`ExtractionMetadata::from_error`]. When a stream fails mid-way, the
//! record also holds the usage and degradations the stream reported before
//! failing; the stream is not counted as a pass, so `passes` can be `0`
//! while `usage` is not.
//!
//! # Output that is not an answer
//!
//! Before parsing, extraction checks how the response finished, and never
//! retries these failures:
//!
//! - A refusal fails with [`ExtractError::Refused`]: the model wrote one
//!   instead of the output, or the response finished with
//!   [`FinishReason::Refusal`] or [`FinishReason::ContentFilter`]. Its
//!   `text` is the model's refusal or the provider's explanation, when
//!   there is one, and [`RefusalSource`] says which. Asking again would
//!   pay for the same refusal.
//! - Output cut off at the token limit ([`FinishReason::Length`]) fails
//!   with [`ExtractError::Truncated`]: the same limit would cut it off
//!   again. On a reasoning model the limit covers the reasoning too, and
//!   the error says how much of it went to reasoning when the response
//!   reported that; see [Reasoning models](#reasoning-models).
//! - Output cut off by a full context window
//!   ([`FinishReason::ContextWindow`]) fails with
//!   [`ExtractError::Truncated`] too, with that finish reason: the input
//!   left too little room, so shorten it (raising the output limit does not
//!   help).
//!
//! Other abnormal finishes (an [`Other`](FinishReason::Other) reason, such
//! as Gemini's `malformed_function_call`) are parsed as usual; when the
//! output cannot be parsed, the [`ExtractError::Parse`] message names the
//! finish reason. Empty or whitespace-only output in `Native` mode is
//! [`ExtractError::MissingStructuredText`], not a parse failure.
//!
//! # Reasoning models
//!
//! On a reasoning model the output token limit
//! ([`ChatRequest::max_output_tokens`](field@ChatRequest::max_output_tokens),
//! or the provider's default) covers the reasoning as well as the output,
//! wherever the provider counts it so. A model that reasons at length can
//! spend most or all of the limit before writing the value, and the
//! extraction fails with [`ExtractError::Truncated`]. When the response
//! reported its reasoning tokens, the error says so ("400 of 400 output
//! tokens went to reasoning").
//!
//! Extraction rarely needs much reasoning. Ask for less on extraction
//! requests, which also costs less, or raise the limit:
//!
//! ```
//! use allama::{ChatRequest, ReasoningEffort};
//!
//! let request = ChatRequest::new("gpt-5-mini")
//!     .user("Ada published the first program in 1843.")
//!     .reasoning(ReasoningEffort::Low)
//!     .max_output_tokens(2_000);
//! # let _ = request;
//! ```
//!
//! # Schema enforcement
//!
//! Both modes ask the provider to constrain the output to the schema of
//! `T`: the response format in `Native` mode and the synthetic tool in
//! `ForcedTool` mode are both strict. Adapters rewrite the schema into
//! their grammar engine's dialect and report, as warnings on the field
//! that carries it, what they could not keep. Extraction reads those
//! warnings into [`ExtractionMetadata::enforcement`], a
//! [`SchemaEnforcement`]:
//!
//! - [`Exact`](SchemaEnforcement::Exact): constrained decoding ran over the
//!   schema of `T`, rewritten only in ways that change nothing `T` can
//!   deserialize (annotations removed, a `$ref` and its siblings merged, a
//!   provably disjoint `oneOf` written as `anyOf`, a scalar `const` written
//!   as a one-value `enum`, optional properties made
//!   nullable under strict modes that require every property; the `null`s
//!   that lets the model send are removed before deserializing, see
//!   below);
//! - [`Approximate`](SchemaEnforcement::Approximate): constrained decoding
//!   ran, over a schema the adapter had to widen, or sent to an engine that
//!   does not report what it enforces. An
//!   [`Approximated`](crate::WarningKind::Approximated) warning says what;
//! - [`NotEnforced`](SchemaEnforcement::NotEnforced): strict mode was not
//!   honoured, and an [`Unsupported`](crate::WarningKind::Unsupported)
//!   warning says why (the endpoint has none, the model lacks it, or the
//!   schema uses a construct its grammar cannot express).
//!
//! Only the field the mode relies on counts: the response format in
//! `Native` mode ([`RequestField::ResponseFormat`]) and the tools in
//! `ForcedTool` mode ([`RequestField::Tools`]). A warning about the
//! caller's own tools in `Native` mode leaves the enforcement alone. The
//! counted warnings are in
//! [`degradations`](ExtractionMetadata::degradations), reworded in terms of
//! the caller's type: "the schema of `Order`" for the synthetic tool or
//! the response schema, and `Order.attributes` for a JSON pointer such as
//! `/properties/attributes` (the response keeps the adapter's own words).
//!
//! An enforcement below `Exact` is also a warning event (with the
//! `warning-events` feature): an `Approximated` or `Unsupported` warning on
//! the counted field, coded `extraction.<enforcement>.<Type>`
//! (`extraction.not_enforced.Order`), so it is logged at `WARN` once per
//! provider, model and type, and at `DEBUG` after that.
//!
//! When strict mode cannot be honoured, the request's
//! [`UnsupportedPolicy`] decides. Adapters apply it through
//! [`apply_strict_policy`](crate::adapter::schema::apply_strict_policy), the one
//! place that decides between `NotEnforced` and failure for a schema the
//! grammar cannot express:
//!
//! - [`UnsupportedPolicy::Warn`] (the default): the schema goes out
//!   non-strict with an `Unsupported` warning, and extraction still runs,
//!   reporting `NotEnforced`, so a batch job can check one field instead of
//!   reading response warnings.
//! - [`UnsupportedPolicy::Error`]: the provider call fails with
//!   [`Error::Unsupported`] before anything is sent. It means "constrained
//!   decoding is in force, possibly over an approximated schema, or fail":
//!   an `Approximated` rewrite does not fail. A pipeline that requires
//!   `Exact` sets a [floor](self#enforcement-floor-and-plans). In `Auto` mode a refused native response format first
//!   [falls back](self#automatic-fallback-to-forced-tool-mode) to
//!   forced-tool mode, which fails the same way when it cannot constrain
//!   the output either.
//!
//! ```
//! # #[cfg(feature = "mock")]
//! # async fn example() -> allama::Result<()> {
//! use allama::extract::{ExtractExt, ExtractionMode, SchemaEnforcement};
//! use allama::mock::MockProvider;
//! use allama::schemars::{self, JsonSchema};
//! use allama::{ChatRequest, UnsupportedPolicy};
//!
//! #[derive(serde::Deserialize, JsonSchema)]
//! struct Person {
//!     name: String,
//! }
//!
//! let provider = MockProvider::with_text(r#"{"name": "Ada"}"#);
//! let request = ChatRequest::new("m")
//!     .user("Ada wrote the first program.")
//!     .with_option(ExtractionMode::Native)
//!     .unsupported_policy(UnsupportedPolicy::Error); // constrained or fail
//! let extracted = provider.extract::<Person>(&request).await?;
//! assert_eq!(extracted.metadata.enforcement, SchemaEnforcement::Exact);
//! # Ok(()) }
//! ```
//!
//! The enforcement is the adapter's own report, not a verified grammar
//! guarantee: an adapter that ignores strict mode without saying so (or a
//! mock) reports `Exact`. Even `Exact` does not guarantee every value: a
//! refusal or output cut off at the token limit is not schema output, and
//! some engines do not guarantee the capitalisation of string `enum` or
//! `const` values (Anthropic, for one). The output is deserialized into `T`
//! either way, so a value that does not match still fails with
//! [`ExtractError::Parse`].
//!
//! # Enforcement floor and plans
//!
//! [`Extractor::plan`] tells what an extraction would do before it sends
//! anything: the mode (after any automatic fallback), the enforcement its
//! first pass would report, and how much of the call the provider's
//! validation covered ([`ExtractionPlan`]). It runs the provider's
//! validation on a schema-only copy of the request.
//!
//! [`Extractor::min_enforcement`] sets a floor, off by default. With
//! [`ValidationCoverage::Encoding`] (every first-party adapter) a plan
//! below it fails before sending with [`ExtractError::EnforcementBelow`];
//! otherwise each pass is checked, and the failure carries the response
//! and its usage, so it is a paid call.
//! [`Approximate`](SchemaEnforcement::Approximate) is the practical
//! portable floor: Anthropic removes `minimum` and `maximum`, so a type
//! with an unsigned integer field is never `Exact` there.
//!
//! What adapters report for typical types (the extraction-plan tests of
//! each adapter):
//!
//! | Adapter | Mode | Strings | Unsigned integer | Map |
//! |---|---|---|---|---|
//! | OpenAI (both APIs), Azure, Groq, xAI | both | exact | exact | not enforced |
//! | Anthropic | native | exact | approximate | refused (`Auto` falls back) |
//! | Anthropic | forced tool | exact | approximate | not enforced |
//! | Gemini | native | exact | exact | exact |
//! | Gemini | forced tool | not enforced | not enforced | not enforced |
//! | Custom OpenAI-compatible endpoint, OpenRouter, Mistral | both | approximate | approximate | not enforced |
//! | Ollama | native | approximate | approximate | approximate |
//! | Ollama | forced tool | refused | refused | refused |
//! | vLLM | both | approximate | approximate | approximate |
//! | Together, Fireworks, DeepInfra | both | not enforced | not enforced | not enforced |
//! | Workers AI | native | not enforced | not enforced | not enforced |
//!
//! # Undeclared keys
//!
//! serde ignores keys a struct does not declare and fills in defaults, so
//! without constrained decoding a model that misnames a key (`e_mail` for
//! `email`) or nests one at the wrong level yields a value that
//! deserializes and is wrong: the `Option` or `#[serde(default)]` field it
//! meant is empty. On every pass whose enforcement is not `Exact` (and in
//! [`Extractor::parse`], which cannot know), extraction therefore walks the
//! output against the schema of `T` before deserializing, and rejects a
//! key that no schema there declares, with an [`ExtractError::Parse`] that
//! names the key and its path (``unknown field `e_mail`, expected one of
//! `email`, `phone` `` at `contact.e_mail`). The rejection is retryable:
//! [`Extractor::retries`] sends it back to the model, and
//! [`ExtractionMetadata::undeclared_key_rejections`] counts the passes it
//! cost. The raw output is kept.
//!
//! - **Stricter than JSON Schema and than other libraries.** An object
//!   without `additionalProperties` is treated as closed. JSON Schema
//!   leaves it open, and Pydantic (`extra='ignore'`), Zod and serde ignore
//!   unknown keys by default. With `#[serde(deny_unknown_fields)]`, schemars
//!   writes `additionalProperties: false` and serde rejects the key too, so
//!   the two agree.
//! - **Exempt:** maps (`HashMap`, `BTreeMap`) and structs with a
//!   `#[serde(flatten)]` map (`additionalProperties`), `patternProperties`,
//!   and open schemas (`serde_json::Value`, `{}`, `true`, an object
//!   schema without `properties`).
//! - **Unions:** a key counts as undeclared only when no `anyOf`/`oneOf`
//!   branch that may apply declares it; when the walk cannot pick one
//!   branch, it checks the key against all of them. A key some branch
//!   declares passes even when serde then picks another branch of an
//!   `untagged` enum.
//! - **Aliases:** `#[serde(alias)]` names are not in the schema, so a key
//!   written under an alias is rejected (and retried).
//! - **Opt out** with [`Extractor::allow_undeclared_keys`].
//!
//! The check does not close the remaining gap: a schema sent without
//! strict mode lets the model leave out `Option` and `#[serde(default)]`
//! fields, so a `NotEnforced` pass can still turn "the model did not
//! decide" into a default value. Callers who care set a
//! [floor](self#enforcement-floor-and-plans).
//!
//! # Target types
//!
//! `T` implements `serde::Deserialize` and `schemars::JsonSchema`. Derive the
//! latter through the [`allama::schemars`](crate::schemars) re-export
//! (`use allama::schemars::{self, JsonSchema};`) so it matches the
//! `schemars` version `allama` was built with.
//!
//! Strict modes that require every property make optional properties
//! nullable, so the model may send `null` for a field it would have left
//! out. Before deserializing, extraction removes those stand-in `null`s
//! ([`normalize_strict_nulls`](crate::adapter::schema::normalize_strict_nulls)): a
//! `null` for an optional property whose schema in `T` admits no `null`.
//! `#[serde(default)]` fields then get their default, while `Option<T>` and
//! `serde_json::Value` keep their `null`. A `null` for a required property
//! or a map entry is kept, so serde reports it ("invalid type: null").
//! [`Extractor::parse`] normalizes too.
//!
//! # Validating values
//!
//! A schema says what the model may send, and not every engine enforces
//! all of it (Anthropic removes `minimum` and `maximum`, see the
//! [table](self#enforcement-floor-and-plans)). To be sure of a rule,
//! check it while deserializing: a newtype with `#[serde(try_from = ..)]`
//! rejects a value that breaks it, and its error becomes the
//! [`ExtractError::Parse`] message. With [`Extractor::retries`], that
//! message is the feedback the model gets, so say what is wrong in it.
//! With `try_from`, schemars writes the schema of the type converted from
//! (`u8` here), and a `range` attribute on the newtype's field is not
//! applied; add the rule with `#[schemars(extend(..))]` on the newtype, so
//! the schema the model reads states it too:
//!
//! ```
//! # #[cfg(feature = "mock")]
//! # tokio::runtime::Builder::new_current_thread().build().unwrap().block_on(async {
//! use allama::extract::Extractor;
//! use allama::mock::MockProvider;
//! use allama::schemars::{self, JsonSchema};
//! use allama::extract::ExtractionMode;
//! use allama::ChatRequest;
//!
//! /// A rating from 1 to 5.
//! #[derive(Debug, serde::Deserialize, JsonSchema)]
//! #[serde(try_from = "u8")]
//! #[schemars(extend("minimum" = 1, "maximum" = 5))]
//! struct Rating(u8);
//!
//! impl TryFrom<u8> for Rating {
//!     type Error = String;
//!
//!     fn try_from(value: u8) -> Result<Self, Self::Error> {
//!         if (1..=5).contains(&value) {
//!             Ok(Self(value))
//!         } else {
//!             Err(format!("a rating is 1 to 5, not {value}"))
//!         }
//!     }
//! }
//!
//! #[derive(serde::Deserialize, JsonSchema)]
//! struct Review {
//!     rating: Rating,
//! }
//!
//! let provider = MockProvider::build(|b| b.text(r#"{"rating": 7}"#).text(r#"{"rating": 4}"#));
//! let request = ChatRequest::new("m")
//!     .user("Rate this review: 'Great, but slow.'")
//!     .with_option(ExtractionMode::Native);
//! let extracted = Extractor::new()
//!     .retries(1)
//!     .extract::<_, Review>(&provider, &request)
//!     .await?;
//! assert_eq!(extracted.value.rating.0, 4);
//! // The schema carries the rule.
//! assert_eq!(schemars::schema_for!(Rating).get("maximum"), Some(&5.into()));
//! // The model was told what was wrong.
//! let feedback = provider.last_request().unwrap().messages.last().unwrap().text();
//! assert!(feedback.unwrap().contains("a rating is 1 to 5, not 7"));
//! # Ok::<(), allama::Error>(()) }).unwrap();
//! ```

use std::fmt;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};

use futures_core::Stream;
use schemars::JsonSchema;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

use crate::chat::stream::{StreamBlockType, StreamCollector};
use crate::error::{EXTRACTION_METADATA_KEY, Withheld};
use crate::macros::enum_names;
use crate::provider_data::{ALLAMA_NAMESPACE, DECLINE_EXPLANATION_KEY};
use crate::{
    CapabilitySupport, ChatCapability, ChatProvider, ChatRequest, ChatResponse, Error,
    FinishReason, Message, ProviderOptions, RequestField, ResponseFormat, Result, StreamEvent,
    Subject, ToolChoice, ToolDefinition, Usage, Warning, WarningKind,
};
#[cfg(doc)]
use crate::{UnsupportedPolicy, ValidationCoverage};

mod enforcement;

pub use enforcement::ExtractionPlan;
use enforcement::Target;

const EXTRACTION_TOOL: &str = "submit_structured_output";

/// Structured extraction for every chat provider.
pub trait ExtractExt: ChatProvider {
    /// Extract a `T` from the response to `request`, in one provider call.
    ///
    /// The extraction mode is read from the request options (see
    /// [`ExtractionMode`]) and removed from the derived request. For repair
    /// or retries, use an [`Extractor`].
    ///
    /// # Errors
    ///
    /// Returns provider errors as-is, with the extraction's record attached
    /// ([`ExtractionMetadata::from_error`]), and extraction failures as
    /// [`Error::Extract`], whose [`ExtractFailure`] carries the response
    /// that could not be used.
    fn extract<'a, T>(
        &'a self,
        request: &'a ChatRequest,
    ) -> impl Future<Output = Result<Extracted<T>>> + Send + 'a
    where
        Self: Sync,
        T: JsonSchema + DeserializeOwned + Send + 'a,
    {
        async move { Extractor::new().extract(self, request).await }
    }

    /// Stream the extraction request for `request`.
    ///
    /// The returned stream yields the provider's events unchanged and keeps
    /// the raw structured output seen so far
    /// ([`ExtractionStream::partial_output`]); call
    /// [`ExtractionStream::finish`] after it ends to parse the value. Makes
    /// one provider call.
    ///
    /// # Errors
    ///
    /// Fails like [`extract`](Self::extract) before the stream opens.
    fn extract_stream<'a, T>(
        &'a self,
        request: &'a ChatRequest,
    ) -> impl Future<Output = Result<ExtractionStream<Self::Stream, T>>> + Send + 'a
    where
        Self: Sync,
        T: JsonSchema + Send + 'a,
    {
        async move { Extractor::new().extract_stream(self, request).await }
    }
}

impl<P: ChatProvider + ?Sized> ExtractExt for P {}

/// How structured output is obtained.
///
/// Attach it to a request with `request.with_option(ExtractionMode::Native)`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum ExtractionMode {
    /// Choose from the provider's reported capabilities (see the
    /// [module docs](self)).
    #[default]
    Auto,
    /// Use the provider's JSON Schema response format.
    Native,
    /// Force a call to a synthetic tool and read its arguments.
    ForcedTool,
}

enum_names!(ExtractionMode, "extraction mode", {
    Auto => "auto",
    Native => "native",
    ForcedTool => "forced_tool",
});

impl ProviderOptions for ExtractionMode {
    const NAMESPACE: &'static str = "allama.extraction";
}

/// A typed extraction result.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Extracted<T> {
    /// The parsed value.
    pub value: T,
    /// The provider response it was parsed from (the last one, after
    /// retries).
    pub response: ChatResponse,
    /// How the extraction ran, including the usage of every pass.
    pub metadata: ExtractionMetadata,
}

/// How an extraction ran: its mode and the record of its passes.
///
/// Reported on success ([`Extracted::metadata`]) and on failure
/// ([`ExtractFailure::metadata`]).
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ExtractionMetadata {
    /// The mode that was used.
    pub mode: ExtractionMode,
    /// Number of provider calls made by the extraction (1 plus retries).
    ///
    /// `0` when it failed before calling the provider, and for
    /// [`Extractor::parse`], which parses a response the caller already
    /// has. A call that failed with a provider error returned no response
    /// and is not counted (see [`from_error`](Self::from_error)), even a
    /// stream that failed mid-way, whose usage so far is still in
    /// [`usage`](Self::usage).
    pub passes: u32,
    /// The value was parsed after local repair.
    pub repaired: bool,
    /// How far the provider reported constraining the output to the schema
    /// of `T`, from the warnings it reported on the field the mode relies
    /// on (see [`SchemaEnforcement`] and the
    /// [module docs](self#schema-enforcement)). The least enforced pass
    /// wins.
    ///
    /// This reflects the adapter's own report, not a verified grammar
    /// guarantee. [`NotEnforced`](SchemaEnforcement::NotEnforced) when no
    /// pass was made, including for [`Extractor::parse`], which cannot know
    /// what the request asked for.
    #[serde(default)]
    pub enforcement: SchemaEnforcement,
    /// The warnings counted for [`enforcement`](Self::enforcement), from
    /// every pass and from a stream that failed mid-way, each distinct
    /// warning once: the warnings about the
    /// response format ([`RequestField::ResponseFormat`]) in `Native` mode,
    /// or about the tools ([`RequestField::Tools`]) in `ForcedTool` mode.
    ///
    /// The response keeps every warning ([`ChatResponse::warnings`]),
    /// including those about other fields, which are not counted.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub degradations: Vec<Warning>,
    /// Token usage summed over every pass, including the passes whose
    /// output could not be used (see [`Usage`]'s `Add`). `None` when nothing
    /// reported usage.
    ///
    /// It also holds the usage a stream reported before it failed mid-way
    /// ([`ExtractionStream::finish`]). That stream returned no response and
    /// is not a pass, so the record of a failed stream can pair
    /// [`passes`](Self::passes) `0` with billed usage.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub usage: Option<Usage>,
    /// The refusal that made an [`Auto`](ExtractionMode::Auto) extraction
    /// switch from native output to forced-tool mode, when it did (see the
    /// [module docs](self#automatic-fallback-to-forced-tool-mode)).
    /// [`mode`](Self::mode) is then [`ForcedTool`](ExtractionMode::ForcedTool).
    ///
    /// The refused attempt sent nothing and is not counted in
    /// [`passes`](Self::passes).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub auto_fallback: Option<AutoFallback>,
    /// How many passes were rejected for a key the schema of `T` does not
    /// declare (see [undeclared keys](self#undeclared-keys)), whether the
    /// extraction asked again or failed. Each one asked again is a billed
    /// pass, so this is the cost of the check.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub undeclared_key_rejections: u32,
}

impl ExtractionMetadata {
    /// The record of an extraction in `mode` that has made no provider calls
    /// yet.
    #[must_use]
    pub fn new(mode: ExtractionMode) -> Self {
        Self {
            mode,
            passes: 0,
            repaired: false,
            enforcement: SchemaEnforcement::NotEnforced,
            degradations: Vec::new(),
            usage: None,
            auto_fallback: None,
            undeclared_key_rejections: 0,
        }
    }

    /// The pass record attached to a provider error that ended an
    /// extraction.
    ///
    /// Extraction returns provider errors unchanged (so
    /// [`Error::is_retryable`], [`Error::retry_after`] and the variant stay
    /// what the provider reported) and attaches the record of the passes
    /// made before the error to its context, under `allama.extraction` in
    /// [`ErrorContext::metadata`](crate::ErrorContext::metadata). It is
    /// attached on every provider error from [`ExtractExt::extract`],
    /// [`ExtractExt::extract_stream`], [`ExtractionStream::finish`] and the
    /// [`Extractor`] methods, including one on the first call
    /// ([`passes`](Self::passes) `0`). For an error that ended a failover it
    /// is on that error's own context, not on the errors it
    /// [fell back from](Error::fallback_from).
    ///
    /// `None` when the error carries no record: it did not come from an
    /// extraction, or its variant has no context ([`Error::Serialization`]).
    /// An [`Error::Extract`] carries its record in
    /// [`ExtractFailure::metadata`] instead.
    ///
    /// ```
    /// # #[cfg(feature = "mock")]
    /// # async fn example() {
    /// use allama::extract::{ExtractionMetadata, ExtractionMode, Extractor};
    /// use allama::mock::{ChatResponseBuilder, MockProvider};
    /// use allama::schemars::{self, JsonSchema};
    /// use allama::{ChatRequest, Error};
    ///
    /// #[derive(Debug, serde::Deserialize, JsonSchema)]
    /// struct Person {
    ///     name: String,
    /// }
    ///
    /// let provider = MockProvider::build(|b| {
    ///     b.response(ChatResponseBuilder::new().text("not json").usage(500, 50).build())
    ///         .error(Error::rate_limited("slow down"))
    /// });
    /// let request = ChatRequest::new("m")
    ///     .user("Ada wrote the first program.")
    ///     .with_option(ExtractionMode::Native);
    /// let error = Extractor::new()
    ///     .retries(1)
    ///     .extract::<_, Person>(&provider, &request)
    ///     .await
    ///     .unwrap_err();
    /// assert!(matches!(error, Error::RateLimited { .. })); // not reclassified
    /// let metadata = ExtractionMetadata::from_error(&error).unwrap();
    /// assert_eq!(metadata.passes, 1);
    /// assert_eq!(metadata.usage.and_then(|u| u.input_tokens), Some(500));
    /// # }
    /// ```
    #[must_use]
    pub fn from_error(error: &Error) -> Option<Self> {
        let record = error.context()?.metadata.get(EXTRACTION_METADATA_KEY)?;
        Self::deserialize(record).ok()
    }

    /// Attach this record to a provider error that ended the extraction,
    /// for [`from_error`](Self::from_error).
    fn attach_to(&self, mut error: Error) -> Error {
        if let Some(context) = error.context_mut()
            && let Ok(record) = serde_json::to_value(self)
        {
            context
                .metadata
                .insert(EXTRACTION_METADATA_KEY.to_owned(), record);
        }
        error
    }

    /// The fallback to forced-tool mode that `error` calls for, when this
    /// extraction may still take it: the caller asked for
    /// [`Auto`](ExtractionMode::Auto), it picked
    /// [`Native`](ExtractionMode::Native), no pass was made yet, and the
    /// response format was refused: by the adapter before sending, or, when
    /// `Native` was only a guess from an `Unknown` answer (`guessed`), by
    /// the provider itself. A provider's 4xx refusal produced nothing to
    /// bill, so the fallback still never pays twice for the output.
    fn auto_fallback_for(
        &self,
        requested: ExtractionMode,
        guessed: bool,
        error: &Error,
    ) -> Option<AutoFallback> {
        use crate::UnsupportedStage::{BeforeSending, Provider};
        let stage = error.unsupported_stage()?;
        let refused_here = stage == BeforeSending || (guessed && stage == Provider);
        if !self.may_fall_back(requested)
            || error.unsupported_field() != Some(RequestField::ResponseFormat)
            || !refused_here
        {
            return None;
        }
        // The record reaches the redacted log view, so it keeps only text an
        // adapter authored (every `Degradations` refusal is).
        let message = if error.message_origin() == crate::MessageOrigin::Authored {
            error.message()
        } else {
            String::new()
        };
        Some(AutoFallback::new(RequestField::ResponseFormat, message).with_stage(stage))
    }

    /// Whether a refusal of the native response format would still switch
    /// this extraction to forced-tool mode.
    fn may_fall_back(&self, requested: ExtractionMode) -> bool {
        requested == ExtractionMode::Auto
            && self.mode == ExtractionMode::Native
            && self.passes == 0
            && self.auto_fallback.is_none()
    }

    /// Switch to forced-tool mode after `fallback`.
    fn fall_back(&mut self, fallback: AutoFallback) {
        self.mode = ExtractionMode::ForcedTool;
        self.auto_fallback = Some(fallback);
    }

    /// Count one provider call, add its usage, and record the warnings it
    /// reported on the field the mode relies on.
    ///
    /// Every extraction request asks for strict mode, so the enforcement
    /// follows from the counted warnings alone. The counted warnings are
    /// kept in terms of the caller's type, `label`.
    fn record_pass(&mut self, response: &ChatResponse, label: &str) {
        self.passes = self.passes.saturating_add(1);
        self.record_usage_and_degradations(response, label);
        self.enforcement = SchemaEnforcement::from_degradations(&self.degradations);
    }

    /// Add the usage and degradations a stream reported before it failed.
    ///
    /// The stream returned no response, so it is not counted as a pass, and
    /// [`enforcement`](Self::enforcement) stays as it was.
    fn record_failed_stream(&mut self, partial: &ChatResponse, label: &str) {
        self.record_usage_and_degradations(partial, label);
    }

    fn record_usage_and_degradations(&mut self, response: &ChatResponse, label: &str) {
        if let Some(usage) = &response.usage {
            self.usage = Some(self.usage.take().unwrap_or_default() + usage);
        }
        let field = counted_field(self.mode);
        for warning in response
            .warnings
            .iter()
            .filter(|w| field.is_some_and(|field| is_about(w, field)))
        {
            let warning = enforcement::in_caller_terms(warning, label);
            if !self.degradations.contains(&warning) {
                self.degradations.push(warning);
            }
        }
    }
}

/// Shows the mode, passes and enforcement, and the other fields when they
/// are set.
impl fmt::Debug for ExtractionMetadata {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            mode,
            passes,
            repaired,
            enforcement,
            degradations,
            usage,
            auto_fallback,
            undeclared_key_rejections,
        } = self;
        let mut debug = f.debug_struct("ExtractionMetadata");
        debug
            .field("mode", mode)
            .field("passes", passes)
            .field("enforcement", enforcement);
        if *repaired {
            debug.field("repaired", repaired);
        }
        if !degradations.is_empty() {
            debug.field("degradations", degradations);
        }
        if let Some(usage) = usage {
            debug.field("usage", usage);
        }
        if let Some(auto_fallback) = auto_fallback {
            debug.field("auto_fallback", auto_fallback);
        }
        if *undeclared_key_rejections > 0 {
            debug.field("undeclared_key_rejections", undeclared_key_rejections);
        }
        debug.finish()
    }
}

/// How far the provider reported constraining an extraction's output to
/// the schema of `T` ([`ExtractionMetadata::enforcement`]).
///
/// It is derived from the warnings the adapter reported on the field the
/// extraction mode relies on; see the
/// [module docs](self#schema-enforcement). The enum is `#[non_exhaustive]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SchemaEnforcement {
    /// Constrained decoding ran over the schema of `T`, with only rewrites
    /// that change nothing `T` can deserialize. No warning was counted.
    Exact,
    /// Constrained decoding ran over a rewritten (widened) schema, or over
    /// a schema sent unchecked to an engine that does not report what it
    /// enforces. An `Approximated` warning says what.
    Approximate,
    /// Strict mode was not honoured, and an `Unsupported` warning says why;
    /// or no provider call was made.
    #[default]
    NotEnforced,
}

enum_names!(SchemaEnforcement, "schema enforcement", {
    Exact => "exact",
    Approximate => "approximate",
    NotEnforced => "not_enforced",
});

impl SchemaEnforcement {
    /// Whether this enforcement is at least `floor`: `Exact` satisfies
    /// every floor, `Approximate` satisfies `Approximate` and
    /// `NotEnforced`, and `NotEnforced` only itself.
    ///
    /// ```
    /// use allama::extract::SchemaEnforcement;
    ///
    /// assert!(SchemaEnforcement::Exact.satisfies(SchemaEnforcement::Approximate));
    /// assert!(!SchemaEnforcement::NotEnforced.satisfies(SchemaEnforcement::Approximate));
    /// ```
    #[must_use]
    pub fn satisfies(self, floor: Self) -> bool {
        self.rank() >= floor.rank()
    }

    fn rank(self) -> u8 {
        match self {
            Self::NotEnforced => 0,
            Self::Approximate => 1,
            Self::Exact => 2,
        }
    }

    /// The enforcement the counted warnings of an extraction show: any
    /// `Unsupported` warning means strict mode was not honoured; any other
    /// warning means it ran over an approximation.
    fn from_degradations(warnings: &[Warning]) -> Self {
        if warnings.iter().any(|w| w.kind == WarningKind::Unsupported) {
            Self::NotEnforced
        } else if warnings.is_empty() {
            Self::Exact
        } else {
            Self::Approximate
        }
    }
}

/// Why an [`Auto`](ExtractionMode::Auto) extraction switched from native
/// output to forced-tool mode ([`ExtractionMetadata::auto_fallback`]).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AutoFallback {
    /// The request field that was refused ([`Error::unsupported_field`]):
    /// [`RequestField::ResponseFormat`].
    pub field: RequestField,
    /// The refusal's message, as the adapter wrote it. Empty when the
    /// message was not [authored](Error::authored), since the record is
    /// part of the redacted log view.
    pub message: String,
    /// Where it was refused ([`Error::unsupported_stage`]): before sending,
    /// or by the provider when native output was a guess from an `Unknown`
    /// answer. A record without one was refused before sending.
    #[serde(default = "before_sending")]
    pub stage: crate::UnsupportedStage,
}

fn before_sending() -> crate::UnsupportedStage {
    crate::UnsupportedStage::BeforeSending
}

impl AutoFallback {
    /// A fallback after the adapter refused `field` with `message` before
    /// sending.
    #[must_use]
    pub fn new(field: RequestField, message: impl Into<String>) -> Self {
        Self {
            field,
            message: message.into(),
            stage: crate::UnsupportedStage::BeforeSending,
        }
    }

    /// Set where the refusal happened.
    #[must_use]
    pub fn with_stage(mut self, stage: crate::UnsupportedStage) -> Self {
        self.stage = stage;
        self
    }
}

/// A failed extraction: why it failed, the last provider response, and the
/// record of the passes it made.
///
/// Carried by [`Error::Extract`]:
///
/// ```
/// # #[cfg(feature = "mock")]
/// # async fn example() {
/// use allama::extract::{ExtractError, ExtractionMode, Extractor};
/// use allama::mock::{ChatResponseBuilder, MockProvider};
/// use allama::schemars::{self, JsonSchema};
/// use allama::{ChatRequest, Error};
///
/// #[derive(Debug, serde::Deserialize, JsonSchema)]
/// struct Person {
///     name: String,
/// }
///
/// let provider = MockProvider::build(|b| {
///     b.response(ChatResponseBuilder::new().text("not json").usage(10, 3).build())
///         .response(ChatResponseBuilder::new().text("still not json").usage(14, 4).build())
/// });
/// let request = ChatRequest::new("m")
///     .user("Ada wrote the first program.")
///     .with_option(ExtractionMode::Native);
/// let Err(Error::Extract { failure, .. }) = Extractor::new()
///     .retries(1)
///     .extract::<_, Person>(&provider, &request)
///     .await
/// else {
///     panic!("expected an extraction failure");
/// };
/// assert!(matches!(failure.error(), ExtractError::Parse { .. }));
/// assert_eq!(failure.response().and_then(|r| r.text()).as_deref(), Some("still not json"));
/// assert_eq!(failure.metadata().passes, 2);
/// assert_eq!(failure.metadata().usage.as_ref().and_then(|u| u.input_tokens), Some(24));
/// # }
/// ```
///
/// [`Error`]'s serde form includes the response; its redacted logging view
/// ([`Error::as_log`]) does not. Its `Debug` withholds the response, and the
/// model's text in the error (see [`ExtractError`]).
#[derive(Clone, Serialize, Deserialize)]
pub struct ExtractFailure {
    error: ExtractError,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    response: Option<ChatResponse>,
    metadata: ExtractionMetadata,
}

impl ExtractFailure {
    /// A failure without a response (extraction stopped before or without
    /// one).
    #[must_use]
    pub fn new(error: ExtractError, metadata: ExtractionMetadata) -> Self {
        Self {
            error,
            response: None,
            metadata,
        }
    }

    /// Attach the response whose output could not be used.
    #[must_use]
    pub fn with_response(mut self, response: ChatResponse) -> Self {
        self.response = Some(response);
        self
    }

    /// Why extraction failed.
    #[must_use]
    pub fn error(&self) -> &ExtractError {
        &self.error
    }

    /// The last provider response: the one whose output could not be used.
    /// `None` when extraction failed before calling the provider.
    #[must_use]
    pub fn response(&self) -> Option<&ChatResponse> {
        self.response.as_ref()
    }

    /// How the extraction ran up to the failure: mode, provider calls made,
    /// and their summed usage.
    #[must_use]
    pub fn metadata(&self) -> &ExtractionMetadata {
        &self.metadata
    }

    /// Take the last provider response.
    #[must_use]
    pub fn into_response(self) -> Option<ChatResponse> {
        self.response
    }
}

impl fmt::Display for ExtractFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.error.fmt(f)
    }
}

impl fmt::Debug for ExtractFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut debug = f.debug_struct("ExtractFailure");
        debug.field("error", &self.error);
        if self.response.is_some() {
            debug.field("response", &format_args!("<withheld>"));
        }
        debug.field("metadata", &self.metadata).finish()
    }
}

impl std::error::Error for ExtractFailure {}

impl From<ExtractFailure> for Error {
    fn from(failure: ExtractFailure) -> Self {
        Error::extract(failure)
    }
}

/// Runs extraction with explicit, opt-in recovery.
///
/// The default extractor behaves exactly like [`ExtractExt::extract`]: one
/// provider call, strict parsing.
///
/// ```
/// # #[cfg(feature = "mock")]
/// # async fn example() -> allama::Result<()> {
/// use allama::ChatRequest;
/// use allama::extract::Extractor;
/// use allama::mock::MockProvider;
/// use allama::schemars::{self, JsonSchema};
///
/// #[derive(serde::Deserialize, JsonSchema)]
/// struct Person {
///     name: String,
/// }
///
/// let provider = MockProvider::with_text("```json\n{\"name\": \"Ada\"}\n```");
/// let request = ChatRequest::new("m")
///     .user("Ada wrote the first program.")
///     .with_option(allama::extract::ExtractionMode::Native);
/// let extracted = Extractor::new()
///     .repair(true) // strip the markdown fence locally
///     .retries(1) // ask once more if it still does not parse
///     .extract::<_, Person>(&provider, &request)
///     .await?;
/// assert!(extracted.metadata.repaired);
/// # Ok(()) }
/// ```
///
/// # Extracting from a conversation
///
/// An extraction keeps every field of the request it is given (see
/// [Typical use](self#typical-use)). To extract from a conversation without
/// the rest of the request, start a fresh request from its messages, or
/// clear the tools on a copy. Either way, ask for the value in a final user
/// message: a conversation that ends on the assistant's answer would send
/// that answer as a [prefill](crate::adapter::chat::retain_turns), and several
/// providers refuse a prefill with structured output.
///
/// ```
/// use allama::extract::ExtractionMode;
/// use allama::{ChatRequest, ToolDefinition};
///
/// let chat = ChatRequest::new("m")
///     .user("Who wrote the first program?")
///     .assistant("Ada Lovelace, in 1843.")
///     .tools([ToolDefinition::new("lookup", serde_json::json!({"type": "object"}))])
///     .temperature(0.7);
///
/// // Only the model and the conversation, with the instruction last:
/// let fresh = ChatRequest::new(&chat.model)
///     .extend_messages(chat.messages.clone())
///     .user("Extract the person from this conversation.")
///     .with_option(ExtractionMode::ForcedTool);
///
/// // Everything but the tools:
/// let mut without_tools = chat
///     .clone()
///     .user("Extract the person from this conversation.")
///     .with_option(ExtractionMode::ForcedTool);
/// without_tools.tools.clear();
/// without_tools.tool_choice = None;
/// # assert!(fresh.tools.is_empty() && fresh.temperature.is_none());
/// # assert_eq!(without_tools.temperature, Some(0.7));
/// # assert!(matches!(fresh.messages.last(), Some(allama::Message::User(_))));
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct Extractor {
    repair: bool,
    retries: u32,
    allow_undeclared_keys: bool,
    min_enforcement: Option<SchemaEnforcement>,
}

impl Extractor {
    /// An extractor with no recovery.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Repair malformed output locally before giving up: strip markdown code
    /// fences and prose around the JSON value. Makes no provider calls;
    /// [`ExtractionMetadata::repaired`] reports when it was needed.
    ///
    /// When the repaired value is valid JSON but does not fit `T`, the
    /// [`ExtractError::Parse`] message (and the retry feedback) is its error,
    /// such as an unknown enum variant, not the error of the unrepaired
    /// text.
    #[must_use]
    pub fn repair(mut self, enabled: bool) -> Self {
        self.repair = enabled;
        self
    }

    /// When the output cannot be used (unparseable, no structured text, or
    /// the extraction tool was not called once), send the failure back to
    /// the model and ask again, up to `retries` times.
    ///
    /// The feedback names the failure: output that is not valid JSON ("it
    /// was not valid JSON: ...") is told apart from valid JSON that does not
    /// fit the schema ("it did not match the schema at `items[2].price`:
    /// ..."). The path ([`ExtractError::Parse`]'s `path`) names the field
    /// to fix; it is found by deserializing again, only after a failure.
    ///
    /// Each retry is one more provider call, billed and with its own
    /// latency; [`ExtractionMetadata`] counts the calls and sums their usage,
    /// on success and on failure. Truncated output
    /// ([`ExtractError::Truncated`]) is not retried: the same token limit
    /// would cut it off again. Neither is a refusal
    /// ([`ExtractError::Refused`]): asking again would pay for the same
    /// refusal.
    ///
    /// A retry replays the failed reply. Each tool call in it is answered
    /// with an ordinary tool result whose text says why it could not be
    /// used, not a failed one ([`ToolMessage::is_error`](crate::ToolMessage::is_error)),
    /// so the retry turn is sent as is on every adapter, including one
    /// whose endpoint has no tool error flag under
    /// [`UnsupportedPolicy::Error`].
    ///
    /// Provider errors are not retried here; use
    /// [`RetryingProvider`](crate::wrappers::RetryingProvider) for those. A
    /// provider error on a retry is returned as itself, not as an extraction
    /// failure, with the passes and usage so far attached: read them with
    /// [`ExtractionMetadata::from_error`].
    #[must_use]
    pub fn retries(mut self, retries: u32) -> Self {
        self.retries = retries;
        self
    }

    /// Accept output with keys the schema of `T` does not declare, as serde
    /// does, instead of rejecting it (the default; see
    /// [undeclared keys](self#undeclared-keys)).
    ///
    /// ```
    /// # #[cfg(feature = "mock")]
    /// # async fn example() -> allama::Result<()> {
    /// use allama::extract::{ExtractionMode, Extractor};
    /// use allama::mock::{ChatResponseBuilder, MockProvider};
    /// use allama::schemars::{self, JsonSchema};
    /// use allama::{ChatRequest, RequestField, Warning};
    ///
    /// #[derive(serde::Deserialize, JsonSchema)]
    /// struct Person {
    ///     name: String,
    /// }
    ///
    /// // A provider that reports it approximated the schema, so the
    /// // output is checked for undeclared keys.
    /// let approximated = Warning::new(
    ///     allama::WarningKind::Approximated,
    ///     allama::Subject::Field { field: RequestField::ResponseFormat },
    ///     "the grammar engine may not enforce every keyword; the schema was sent unchecked",
    /// );
    /// let provider = MockProvider::with_response(
    ///     ChatResponseBuilder::new()
    ///         .text(r#"{"name": "Ada", "born": 1815}"#)
    ///         .warning(approximated)
    ///         .build(),
    /// );
    /// let request = ChatRequest::new("m").user("Ada").with_option(ExtractionMode::Native);
    /// let extracted = Extractor::new()
    ///     .allow_undeclared_keys(true)
    ///     .extract::<_, Person>(&provider, &request)
    ///     .await?;
    /// assert_eq!(extracted.value.name, "Ada");
    /// # Ok(()) }
    /// ```
    #[must_use]
    pub fn allow_undeclared_keys(mut self, allow: bool) -> Self {
        self.allow_undeclared_keys = allow;
        self
    }

    /// Require at least `floor` schema enforcement
    /// ([`ExtractionMetadata::enforcement`]), failing with
    /// [`ExtractError::EnforcementBelow`] otherwise. Off by default.
    ///
    /// When the provider's validation is a dry run of the call
    /// ([`ValidationCoverage::Encoding`], every first-party adapter), the
    /// floor is checked before sending, on a [plan](Self::plan) of the
    /// extraction, and a plan below it costs nothing. Otherwise
    /// ([`ValidationCoverage::Capabilities`]) the enforcement is known only
    /// from the response, so the floor is checked after each pass, and the
    /// failure carries that pass's response and usage: **it is a paid
    /// call**.
    ///
    /// [`Approximate`](SchemaEnforcement::Approximate) is the practical
    /// portable floor: [`Exact`](SchemaEnforcement::Exact) rules out
    /// providers whose strict mode drops keywords the schema of `T` uses,
    /// such as Anthropic, which removes `minimum` and `maximum`, so any
    /// unsigned integer field is `Approximate` there.
    #[must_use]
    pub fn min_enforcement(mut self, floor: SchemaEnforcement) -> Self {
        self.min_enforcement = Some(floor);
        self
    }

    /// Extract a `T` from the response to `request`. Every field of
    /// `request` is kept except the ones the selected mode replaces
    /// (response format, or tools and tool choice); see
    /// [extracting from a conversation](Self#extracting-from-a-conversation).
    ///
    /// # Errors
    ///
    /// See [`ExtractExt::extract`]. After the last retry, the last
    /// extraction failure is returned, with the last response and the usage
    /// of every pass.
    pub async fn extract<P, T>(&self, provider: &P, request: &ChatRequest) -> Result<Extracted<T>>
    where
        P: ChatProvider + ?Sized,
        T: JsonSchema + DeserializeOwned + Send,
    {
        let target = Target::new::<T>(provider.provider_name(), &request.model);
        let requested = requested_mode(request);
        self.check_floor_before_sending(provider, request, &target, false)?;
        let (mode, guessed) = select_mode(provider, request);
        let mut metadata = ExtractionMetadata::new(mode);
        let mut derived = target.derive(request, &metadata)?;
        let mut retries_left = self.retries;
        loop {
            let response = match provider.chat(&derived).await {
                Ok(response) => response,
                Err(error) => {
                    let Some(fallback) = metadata.auto_fallback_for(requested, guessed, &error)
                    else {
                        return Err(target.pre_send_error(error, &metadata));
                    };
                    metadata.fall_back(fallback);
                    derived = target.derive(request, &metadata)?;
                    continue;
                }
            };
            metadata.record_pass(&response, &target.label);
            target.report_enforcement(&metadata);
            if let Some(failure) = self.below_floor(&metadata) {
                return Err(failure.with_response(response).into());
            }
            let mode = metadata.mode;
            let parsed = parse_value::<T>(
                &response,
                mode,
                &target.provider,
                self.repair,
                Some(&target.schema),
                self.rejects_undeclared(&metadata),
            );
            match parsed {
                Ok((value, repaired)) => {
                    metadata.repaired = repaired;
                    return Ok(Extracted {
                        value,
                        response,
                        metadata,
                    });
                }
                Err(rejection) => {
                    if rejection.undeclared {
                        metadata.undeclared_key_rejections += 1;
                    }
                    if retries_left > 0 && rejection.error.worth_asking_again() {
                        retries_left -= 1;
                        ask_again(&mut derived, response, mode, &rejection);
                    } else {
                        return Err(ExtractFailure::new(rejection.error, metadata)
                            .with_response(response)
                            .into());
                    }
                }
            }
        }
    }

    /// Parse a `T` from a response you already have, with this extractor's
    /// repair setting. Makes no provider calls.
    ///
    /// Use it for responses obtained outside an extraction call: a batch
    /// result, a recorded response, or a request you built yourself with a
    /// [`ResponseFormat::JsonSchema`]. `mode` says where the output is: the
    /// response text ([`Native`](ExtractionMode::Native)) or the arguments
    /// of the single `submit_structured_output` tool call
    /// ([`ForcedTool`](ExtractionMode::ForcedTool)).
    /// [`Auto`](ExtractionMode::Auto) has nothing to choose from and fails
    /// with [`ExtractError::Unsupported`].
    ///
    /// Strict `null`s are normalized against the schema of `T`, as in
    /// [`extract`](Self::extract); a parse failure keeps the text the model
    /// sent.
    ///
    /// The response is returned in [`Extracted`] or in the failure. The
    /// metadata reports no passes, no usage, and no degradations (they are
    /// on the response), and
    /// [`enforcement`](ExtractionMetadata::enforcement) is
    /// [`NotEnforced`](SchemaEnforcement::NotEnforced): whether the request
    /// asked for strict mode is unknown here.
    ///
    /// ```
    /// use allama::ChatResponse;
    /// use allama::extract::{ExtractionMode, Extractor};
    /// use allama::schemars::{self, JsonSchema};
    ///
    /// #[derive(serde::Deserialize, JsonSchema)]
    /// struct Person {
    ///     name: String,
    /// }
    ///
    /// let response = ChatResponse::new(vec![allama::ContentBlock::text(
    ///     "```json\n{\"name\": \"Ada\"}\n```",
    /// )]);
    /// let extracted = Extractor::new()
    ///     .repair(true)
    ///     .parse::<Person>(response, ExtractionMode::Native)?;
    /// assert_eq!(extracted.value.name, "Ada");
    /// assert!(extracted.metadata.repaired);
    /// # Ok::<(), allama::Error>(())
    /// ```
    ///
    /// # Errors
    ///
    /// Returns [`Error::Extract`] when the output cannot be used.
    pub fn parse<T: JsonSchema + DeserializeOwned>(
        &self,
        response: ChatResponse,
        mode: ExtractionMode,
    ) -> Result<Extracted<T>> {
        let provider = response.provider.as_deref().unwrap_or("unknown").to_owned();
        let metadata = ExtractionMetadata::new(mode);
        if mode == ExtractionMode::Auto {
            return Err(ExtractFailure::new(
                ExtractError::Unsupported { mode, provider },
                metadata,
            )
            .with_response(response)
            .into());
        }
        let model = response.model.clone().unwrap_or_default();
        let target = Target::new::<T>(&provider, &model);
        let reject_undeclared = self.rejects_undeclared(&metadata);
        finish_extraction(response, metadata, &target, self.repair, reject_undeclared)
    }

    /// Stream the extraction of a `T` from the response to `request`. Local
    /// repair applies when the stream is finished; retries do not (a retry
    /// would be a new stream, which is the caller's call to make).
    ///
    /// # Errors
    ///
    /// See [`ExtractExt::extract_stream`].
    pub async fn extract_stream<P, T>(
        &self,
        provider: &P,
        request: &ChatRequest,
    ) -> Result<ExtractionStream<P::Stream, T>>
    where
        P: ChatProvider + ?Sized,
        T: JsonSchema,
    {
        let target = Target::new::<T>(provider.provider_name(), &request.model);
        let requested = requested_mode(request);
        self.check_floor_before_sending(provider, request, &target, true)?;
        let (mode, guessed) = select_mode(provider, request);
        let mut metadata = ExtractionMetadata::new(mode);
        loop {
            let derived = target.derive(request, &metadata)?;
            let mut stream = match provider.chat_stream(&derived).await {
                Ok(stream) => Box::pin(stream),
                Err(error) => {
                    let Some(fallback) = metadata.auto_fallback_for(requested, guessed, &error)
                    else {
                        return Err(target.pre_send_error(error, &metadata));
                    };
                    metadata.fall_back(fallback);
                    continue;
                }
            };
            // The stream contract allows a refusal as the first item too, so
            // while a fallback is possible, read that item before handing the
            // stream on.
            let mut first = None;
            if metadata.may_fall_back(requested) {
                let item = std::future::poll_fn(|cx| stream.as_mut().poll_next(cx)).await;
                if let Some(Err(error)) = &item
                    && let Some(fallback) = metadata.auto_fallback_for(requested, guessed, error)
                {
                    metadata.fall_back(fallback);
                    continue;
                }
                first = Some(item);
            }
            return Ok(ExtractionStream {
                inner: stream,
                first,
                collector: StreamCollector::new(),
                partial: String::new(),
                tool_index: None,
                mode: metadata.mode,
                auto_fallback: metadata.auto_fallback,
                target,
                extractor: *self,
                done: false,
                _value: PhantomData,
            });
        }
    }
}

/// A streamed extraction: the provider's events, then a parsed value.
///
/// Created by [`ExtractExt::extract_stream`]. Read the stream to the end,
/// then call [`finish`](Self::finish). It ends after the provider's first
/// error, as every [`ChatStream`](crate::ChatStream) does.
pub struct ExtractionStream<S, T> {
    inner: Pin<Box<S>>,
    /// The first item, when it was read before the stream was handed on
    /// (to check for a refusal); `Some(None)` when the stream had ended.
    first: Option<Option<Result<StreamEvent>>>,
    collector: StreamCollector,
    partial: String,
    tool_index: Option<usize>,
    mode: ExtractionMode,
    auto_fallback: Option<AutoFallback>,
    /// The schema of `T` and how messages name it.
    target: Target,
    /// Repair, undeclared keys and the floor.
    extractor: Extractor,
    done: bool,
    _value: PhantomData<fn() -> T>,
}

impl<S, T> ExtractionStream<S, T> {
    /// The mode the extraction uses: `ForcedTool` after an
    /// [automatic fallback](self#automatic-fallback-to-forced-tool-mode).
    #[must_use]
    pub fn mode(&self) -> ExtractionMode {
        self.mode
    }

    /// The raw structured output received so far: the response text in
    /// `Native` mode, the extraction tool's arguments in `ForcedTool` mode.
    ///
    /// It is usually an incomplete JSON document; parse it with a
    /// partial-JSON parser to show progress.
    #[must_use]
    pub fn partial_output(&self) -> &str {
        &self.partial
    }

    /// Parse the value from the collected response.
    ///
    /// # Errors
    ///
    /// Returns the stream's error when it failed or ended early, with the
    /// extraction's record attached ([`ExtractionMetadata::from_error`]).
    /// The failed stream is not counted as a pass, but the usage and the
    /// [degradations](ExtractionMetadata::degradations) it reported before
    /// failing are in the record, so `passes` can be `0` while `usage`
    /// holds the tokens already billed. Returns [`Error::Extract`] (with the
    /// collected response) when the output cannot be used.
    pub fn finish(self) -> Result<Extracted<T>>
    where
        T: DeserializeOwned,
    {
        let mut metadata = ExtractionMetadata::new(self.mode);
        metadata.auto_fallback = self.auto_fallback;
        // A stream that fails mid-way returns no response, but what it
        // reported before failing (usage, warnings) is still the caller's.
        let mut failed = metadata.clone();
        failed.record_failed_stream(self.collector.response_metadata(), &self.target.label);
        let response = self
            .collector
            .finish()
            .map_err(|error| failed.attach_to(error))?;
        metadata.record_pass(&response, &self.target.label);
        self.target.report_enforcement(&metadata);
        if let Some(failure) = self.extractor.below_floor(&metadata) {
            return Err(failure.with_response(response).into());
        }
        let reject_undeclared = self.extractor.rejects_undeclared(&metadata);
        finish_extraction(
            response,
            metadata,
            &self.target,
            self.extractor.repair,
            reject_undeclared,
        )
    }

    fn observe(&mut self, event: &StreamEvent) {
        match event {
            StreamEvent::TextDelta { text, .. } if self.mode == ExtractionMode::Native => {
                self.partial.push_str(text);
            }
            StreamEvent::BlockStart {
                index,
                block_type: StreamBlockType::ToolCall,
                name: Some(name),
                ..
            } if name == EXTRACTION_TOOL && self.tool_index.is_none() => {
                self.tool_index = Some(*index);
            }
            StreamEvent::ToolCallDelta { index, arguments } if self.tool_index == Some(*index) => {
                self.partial.push_str(arguments);
            }
            _ => {}
        }
    }
}

/// Counts and state, never content.
impl<S, T> fmt::Debug for ExtractionStream<S, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ExtractionStream")
            .field("mode", &self.mode)
            .field("collector", &self.collector)
            .field("partial_output_len", &self.partial.len())
            .field("done", &self.done)
            .finish_non_exhaustive()
    }
}

impl<S, T> Stream for ExtractionStream<S, T>
where
    S: Stream<Item = Result<StreamEvent>>,
{
    type Item = Result<StreamEvent>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = &mut *self;
        if this.done {
            return Poll::Ready(None);
        }
        let poll = match this.first.take() {
            Some(item) => Poll::Ready(item),
            None => this.inner.as_mut().poll_next(cx),
        };
        match &poll {
            Poll::Ready(Some(Ok(event))) => {
                this.observe(event);
                if let Err(error) = this.collector.push_ref(event) {
                    this.done = true;
                    this.collector.set_terminal_error(error.clone());
                    return Poll::Ready(Some(Err(error)));
                }
            }
            // A chat stream ends after its first error.
            Poll::Ready(Some(Err(error))) => {
                this.done = true;
                this.collector.set_terminal_error(error.clone());
            }
            Poll::Ready(None) => this.done = true,
            Poll::Pending => {}
        }
        poll
    }
}

/// Why an extraction failed after (or before) calling the provider.
///
/// The enum and its variants are `#[non_exhaustive]`: match variants with
/// `..`.
///
/// `Debug` shows the length of the model's text (a parse failure's raw
/// output, message and path, and a refusal's text), never the text itself;
/// `Display` shows all but the raw output.
#[derive(Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ExtractError {
    /// No mode could be selected: the provider's capability answers rule out
    /// both native output and forced tool calls (or [`Extractor::parse`] was
    /// given [`Auto`](ExtractionMode::Auto)).
    #[non_exhaustive]
    Unsupported {
        /// Requested mode.
        mode: ExtractionMode,
        /// Provider name.
        provider: String,
    },
    /// The request carries tools of its own, which extraction refuses in
    /// every mode before sending. Clear `tools` and `tool_choice`, or start
    /// a fresh request from the messages; see
    /// [extracting from a conversation](Extractor#extracting-from-a-conversation).
    #[non_exhaustive]
    ToolConflict {
        /// Provider name.
        provider: String,
    },
    /// The response had no text to parse (`Native`).
    #[non_exhaustive]
    MissingStructuredText {
        /// Provider name.
        provider: String,
    },
    /// The response had no extraction tool call (`ForcedTool`).
    #[non_exhaustive]
    MissingToolCall {
        /// Provider name.
        provider: String,
    },
    /// The response had several extraction tool calls.
    #[non_exhaustive]
    UnexpectedToolCallCount {
        /// Provider name.
        provider: String,
        /// Number of calls.
        count: usize,
    },
    /// The model stopped at the output token limit
    /// ([`FinishReason::Length`]), or filled the model's context window
    /// ([`FinishReason::ContextWindow`]), before finishing the structured
    /// output. `finish_reason` says which.
    ///
    /// Checked before parsing and never retried: asking again under the
    /// same limit would cut the output off again. At the output token
    /// limit, raise `max_output_tokens` or shorten the expected output; at
    /// the context window, shorten the input (truncate or summarize the
    /// conversation), since the output cap is not what stopped it.
    ///
    /// On a reasoning model the limit covers reasoning too (see
    /// [`ChatRequest::max_output_tokens`](field@ChatRequest::max_output_tokens)),
    /// so reasoning may have used up part or all of it. When the response
    /// reported its reasoning tokens, `Display` says how many of the output
    /// tokens went to reasoning; lowering the reasoning effort then leaves
    /// more of the limit for the output.
    #[non_exhaustive]
    Truncated {
        /// Mode used.
        mode: ExtractionMode,
        /// Provider name.
        provider: String,
        /// What cut the output off: [`FinishReason::Length`] or
        /// [`FinishReason::ContextWindow`].
        #[serde(default = "output_limit")]
        finish_reason: FinishReason,
        /// Output tokens the response reported, reasoning included
        /// ([`Usage::output_tokens`]); `None` when it did not report them.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        output_tokens: Option<u64>,
        /// Reasoning tokens the response reported, a subset of
        /// `output_tokens` ([`Usage::reasoning_tokens`]); `None` when it did
        /// not report them.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        reasoning_tokens: Option<u64>,
    },
    /// The model or the provider declined to produce the output: the model
    /// wrote a refusal instead, or the response finished with
    /// [`FinishReason::Refusal`] or [`FinishReason::ContentFilter`].
    ///
    /// Checked before parsing and never retried: asking again would pay for
    /// the same refusal. The last response, with any partial output and the
    /// provider's own decline data, is on the [`ExtractFailure`].
    #[non_exhaustive]
    Refused {
        /// Mode used.
        mode: ExtractionMode,
        /// Provider name.
        provider: String,
        /// The response's finish reason, when it reported one.
        finish_reason: Option<FinishReason>,
        /// What the model or the provider said, when it said anything: the
        /// model's refusal, or the provider's explanation of its decline
        /// (`source` says which). Upstream text: it is left out of the
        /// redacted [`ErrorLog`](crate::ErrorLog).
        text: Option<String>,
        /// Where `text` came from.
        source: RefusalSource,
    },
    /// The schema enforcement is below the floor the extractor requires
    /// ([`Extractor::min_enforcement`]).
    ///
    /// Before sending when the provider's validation is a dry run of the
    /// call; after a pass otherwise, with its response on the
    /// [`ExtractFailure`]. Never retried: asking again gets the same
    /// enforcement.
    #[non_exhaustive]
    EnforcementBelow {
        /// The floor.
        required: SchemaEnforcement,
        /// What the provider reported, or would report.
        achieved: SchemaEnforcement,
        /// Why, from the counted degradations, in terms of the caller's
        /// type.
        reason: String,
    },
    /// The output was not valid JSON or did not match the schema of `T`,
    /// including a key the schema does not declare (see
    /// [undeclared keys](self#undeclared-keys)).
    #[non_exhaustive]
    Parse {
        /// Mode used.
        mode: ExtractionMode,
        /// Provider name.
        provider: String,
        /// Raw output: the text the model sent, before repair or `null`
        /// normalization.
        raw: String,
        /// Parse error message.
        ///
        /// With [`Extractor::repair`], this is the repaired value's error
        /// when the repair found valid JSON (it reached `T`, so its error
        /// says what is wrong), and the raw output's error otherwise. When
        /// the response finished abnormally for a reason other than a
        /// refusal or the token limit (an [`Other`](FinishReason::Other)
        /// reason), the message ends with it, as in "(finish reason
        /// `malformed_function_call`)".
        message: String,
        /// Where in the output the value that does not fit `T` is, such as
        /// `items[2].price`. The retry feedback names it, so the model
        /// knows which field to fix.
        ///
        /// `None` for output that is not valid JSON and for a failure at
        /// the root. Only the first error is reported, and the path stops
        /// at an `untagged` enum or a `flatten`ed field (serde buffers
        /// their content, so the path names the enum or the struct holding
        /// the `flatten`).
        path: Option<String>,
    },
}

impl ExtractError {
    /// The raw output, for parse failures.
    #[must_use]
    pub fn raw_output(&self) -> Option<&str> {
        match self {
            Self::Parse { raw, .. } => Some(raw),
            _ => None,
        }
    }

    /// The message for the redacted [`ErrorLog`](crate::ErrorLog): the
    /// `Display` text, without a parse failure's detail, which may quote the
    /// model's output.
    pub(crate) fn log_message(&self) -> String {
        match self {
            Self::Parse { mode, provider, .. } => {
                format!("{provider}: failed to parse the output using mode {mode:?}")
            }
            Self::Refused {
                provider,
                finish_reason,
                ..
            } => refused_without_text(provider, finish_reason.as_ref()),
            other => other.to_string(),
        }
    }

    /// Whether sending the failure back to the model may help.
    fn worth_asking_again(&self) -> bool {
        matches!(
            self,
            Self::MissingStructuredText { .. }
                | Self::MissingToolCall { .. }
                | Self::UnexpectedToolCallCount { .. }
                | Self::Parse { .. }
        )
    }
}

impl fmt::Display for ExtractError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unsupported {
                mode: ExtractionMode::Auto,
                provider,
            } => write!(
                f,
                "{provider}: the provider reports that the model supports neither structured output nor forced tool calls, so extraction mode Auto has no mode to pick; set ExtractionMode explicitly, or override the capability answers with a capability resolver"
            ),
            Self::Unsupported { mode, provider } => write!(
                f,
                "{provider}: extraction mode {mode:?} is not supported; set ExtractionMode explicitly or override the capability answers with a capability resolver"
            ),
            Self::ToolConflict { provider } => write!(
                f,
                "{provider}: extraction cannot be combined with request tools; clear `tools` and `tool_choice`, or start a fresh request from the messages"
            ),
            Self::MissingStructuredText { provider } => {
                write!(f, "{provider}: the model did not return structured text")
            }
            Self::MissingToolCall { provider } => write!(
                f,
                "{provider}: the model did not call the extraction tool '{EXTRACTION_TOOL}'"
            ),
            Self::UnexpectedToolCallCount { provider, count } => write!(
                f,
                "{provider}: the model returned {count} extraction tool calls, expected one"
            ),
            Self::Truncated {
                mode,
                provider,
                finish_reason: FinishReason::ContextWindow,
                ..
            } => write!(
                f,
                "{provider}: the model filled its context window before finishing the structured output (mode {mode:?}): shorten the input, a higher output token limit does not help"
            ),
            Self::Truncated {
                mode,
                provider,
                output_tokens,
                reasoning_tokens,
                ..
            } => {
                write!(
                    f,
                    "{provider}: the model stopped at the output token limit before finishing the structured output (mode {mode:?})"
                )?;
                if let Some(share) =
                    crate::usage::reasoning_share(*output_tokens, *reasoning_tokens)
                {
                    write!(
                        f,
                        "; {share}: raise max_output_tokens or lower the reasoning effort"
                    )?;
                }
                Ok(())
            }
            Self::Refused {
                provider,
                text: Some(text),
                ..
            } => write!(f, "{provider}: the model refused: {text}"),
            Self::Refused {
                provider,
                finish_reason,
                ..
            } => f.write_str(&refused_without_text(provider, finish_reason.as_ref())),
            Self::EnforcementBelow {
                required,
                achieved,
                reason,
            } => write!(
                f,
                "schema enforcement `{achieved}` is below the required `{required}`: {reason}"
            ),
            Self::Parse {
                mode,
                provider,
                message,
                path,
                ..
            } => {
                write!(
                    f,
                    "{provider}: failed to parse the output using mode {mode:?}: "
                )?;
                if let Some(path) = path {
                    write!(f, "at `{path}`: ")?;
                }
                f.write_str(message)
            }
        }
    }
}

impl fmt::Debug for ExtractError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Unsupported { mode, provider } => f
                .debug_struct("Unsupported")
                .field("mode", mode)
                .field("provider", provider)
                .finish(),
            Self::ToolConflict { provider } => f
                .debug_struct("ToolConflict")
                .field("provider", provider)
                .finish(),
            Self::MissingStructuredText { provider } => f
                .debug_struct("MissingStructuredText")
                .field("provider", provider)
                .finish(),
            Self::MissingToolCall { provider } => f
                .debug_struct("MissingToolCall")
                .field("provider", provider)
                .finish(),
            Self::UnexpectedToolCallCount { provider, count } => f
                .debug_struct("UnexpectedToolCallCount")
                .field("provider", provider)
                .field("count", count)
                .finish(),
            Self::Truncated {
                mode,
                provider,
                finish_reason,
                output_tokens,
                reasoning_tokens,
            } => {
                let mut debug = f.debug_struct("Truncated");
                debug
                    .field("mode", mode)
                    .field("provider", provider)
                    .field("finish_reason", finish_reason);
                if let Some(output_tokens) = output_tokens {
                    debug.field("output_tokens", output_tokens);
                }
                if let Some(reasoning_tokens) = reasoning_tokens {
                    debug.field("reasoning_tokens", reasoning_tokens);
                }
                debug.finish()
            }
            Self::Refused {
                mode,
                provider,
                finish_reason,
                text,
                source,
            } => {
                let mut debug = f.debug_struct("Refused");
                debug.field("mode", mode).field("provider", provider);
                if let Some(finish_reason) = finish_reason {
                    debug.field("finish_reason", finish_reason);
                }
                if let Some(text) = text {
                    debug.field("text", &Withheld::shown_by_display(text));
                }
                debug.field("source", source).finish()
            }
            Self::EnforcementBelow {
                required,
                achieved,
                reason,
            } => f
                .debug_struct("EnforcementBelow")
                .field("required", required)
                .field("achieved", achieved)
                .field("reason", reason)
                .finish(),
            // The output, the parse error and the path all come from the
            // model's text.
            Self::Parse {
                mode,
                provider,
                raw,
                message,
                path,
            } => {
                let mut debug = f.debug_struct("Parse");
                debug
                    .field("mode", mode)
                    .field("provider", provider)
                    .field("raw", &Withheld::new(raw))
                    .field("message", &Withheld::shown_by_display(message));
                if let Some(path) = path {
                    debug.field("path", &Withheld::shown_by_display(path));
                }
                debug.finish()
            }
        }
    }
}

impl std::error::Error for ExtractError {}

/// Where the text of an [`ExtractError::Refused`] came from.
///
/// The enum is `#[non_exhaustive]`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum RefusalSource {
    /// The model wrote the refusal, in place of the output.
    Model,
    /// The provider declined on the model's behalf and explained why, in
    /// its own decline data (Anthropic's `stop_details`, Gemini's
    /// `finishMessage`; see
    /// [Decline explanations](crate::provider_data#decline-explanations)).
    Provider,
    /// Nothing explains the refusal: only the finish reason reports it.
    None,
}

/// The `Display` of a refusal with no text, also its redacted log line.
fn refused_without_text(provider: &str, finish_reason: Option<&FinishReason>) -> String {
    match finish_reason {
        Some(reason) => format!(
            "{provider}: the model refused to produce the structured output (finish reason `{reason}`)"
        ),
        None => format!("{provider}: the model refused to produce the structured output"),
    }
}

/// The mode the caller asked for, before [`select_mode`] resolves `Auto`.
fn requested_mode(request: &ChatRequest) -> ExtractionMode {
    request
        .option::<ExtractionMode>()
        .copied()
        .unwrap_or_default()
}

/// Resolve [`Auto`](ExtractionMode::Auto) from the provider's answers, in
/// this order:
///
/// 1. `Native` when structured output is `Supported`;
/// 2. `ForcedTool` when tool calls and forced tool choice are both
///    `Supported`;
/// 3. `Native` when structured output is `Unknown` (a guess: the provider
///    decides when the request is sent);
/// 4. `ForcedTool` when neither tool calls nor forced tool choice is
///    `Unsupported` (a guess too);
/// 5. otherwise `Auto`, which fails: the answers rule out both modes.
///
/// A verified mode is preferred over a guessed one, and a guess over a
/// failure: `Unknown` never blocks a request. The flag says whether the mode
/// is step 3's guess, so a provider's own refusal of the response format
/// may still fall back.
fn select_mode<P: ChatProvider + ?Sized>(
    provider: &P,
    request: &ChatRequest,
) -> (ExtractionMode, bool) {
    match requested_mode(request) {
        ExtractionMode::Auto => {
            use CapabilitySupport::{Supported, Unknown, Unsupported};
            let answer = |c| provider.chat_capability(&request.model, c);
            let native = answer(ChatCapability::StructuredOutput);
            let tools = answer(ChatCapability::ToolCalls);
            let forced = answer(ChatCapability::ForcedToolChoice);
            match (native, tools, forced) {
                (Supported, _, _) => (ExtractionMode::Native, false),
                (_, Supported, Supported) => (ExtractionMode::ForcedTool, false),
                (Unknown, _, _) => (ExtractionMode::Native, true),
                (_, Supported | Unknown, Supported | Unknown) => {
                    (ExtractionMode::ForcedTool, false)
                }
                (Unsupported, _, _) => (ExtractionMode::Auto, false),
            }
        }
        mode => (mode, false),
    }
}

fn derive_request(
    request: &ChatRequest,
    mode: ExtractionMode,
    provider: &str,
    name: &str,
    schema: serde_json::Value,
) -> std::result::Result<ChatRequest, ExtractError> {
    if mode != ExtractionMode::Auto && !request.tools.is_empty() {
        return Err(ExtractError::ToolConflict {
            provider: provider.to_owned(),
        });
    }
    let mut derived = request.clone();
    derived.options.remove::<ExtractionMode>();
    derived.options.remove_json(ExtractionMode::NAMESPACE);
    match mode {
        ExtractionMode::Native => {
            derived.response_format = Some(ResponseFormat::json_schema(schema).name(name));
        }
        ExtractionMode::ForcedTool => {
            derived.tools = vec![
                ToolDefinition::new(EXTRACTION_TOOL, schema)
                    .description("Submit extracted structured data")
                    .strict(true),
            ];
            derived.tool_choice = Some(ToolChoice::Specific {
                name: EXTRACTION_TOOL.to_owned(),
            });
            derived.response_format = None;
            // `parallel_tool_calls` stays the caller's. Dialects whose strict
            // mode breaks under parallel calls (OpenAI fine-tuned models,
            // Azure OpenAI) turn them off in the adapter, which knows the
            // dialect; core does not guess it from the model id.
        }
        ExtractionMode::Auto => {
            return Err(ExtractError::Unsupported {
                mode,
                provider: provider.to_owned(),
            });
        }
    }
    Ok(derived)
}

/// The request field whose warnings count for `mode`'s enforcement: the
/// one that carries the schema.
fn counted_field(mode: ExtractionMode) -> Option<RequestField> {
    match mode {
        ExtractionMode::Native => Some(RequestField::ResponseFormat),
        ExtractionMode::ForcedTool => Some(RequestField::Tools),
        ExtractionMode::Auto => None,
    }
}

/// Whether `warning` is about `field`.
fn is_about(warning: &Warning, field: RequestField) -> bool {
    warning.subject == Subject::Field { field }
}

/// Parse the value from a response the extraction is done with: one that
/// will not be retried.
fn finish_extraction<T: DeserializeOwned>(
    response: ChatResponse,
    mut metadata: ExtractionMetadata,
    target: &Target,
    repair: bool,
    reject_undeclared: bool,
) -> Result<Extracted<T>> {
    let parsed = parse_value::<T>(
        &response,
        metadata.mode,
        &target.provider,
        repair,
        Some(&target.schema),
        reject_undeclared,
    );
    match parsed {
        Ok((value, repaired)) => {
            metadata.repaired = repaired;
            Ok(Extracted {
                value,
                response,
                metadata,
            })
        }
        Err(rejection) => {
            if rejection.undeclared {
                metadata.undeclared_key_rejections += 1;
            }
            Err(ExtractFailure::new(rejection.error, metadata)
                .with_response(response)
                .into())
        }
    }
}

/// Output that could not be used: why, and for a parse failure, whether
/// the output was JSON at all, which the retry feedback tells apart.
struct Rejection {
    error: ExtractError,
    /// The parse failed on JSON syntax (or early end of input), not on the
    /// shape of `T`.
    invalid_json: bool,
    /// The parse failed on a key the schema of `T` does not declare.
    undeclared: bool,
}

impl From<ExtractError> for Rejection {
    fn from(error: ExtractError) -> Self {
        Self {
            error,
            invalid_json: false,
            undeclared: false,
        }
    }
}

fn is_zero(n: &u32) -> bool {
    *n == 0
}

/// Parse the value, returning whether local repair was needed.
///
/// With the schema of `T`, the stand-in `null`s of optional properties are
/// removed first ([`normalize_strict_nulls`](crate::adapter::schema::normalize_strict_nulls)). The
/// raw output in a parse error stays the text the model sent.
///
/// With repair on, a parse error reports the repair candidate's error when
/// the candidate is valid JSON (it reached `T`, so its error is the one
/// that matters), and the raw text's error otherwise.
fn parse_value<T: DeserializeOwned>(
    response: &ChatResponse,
    mode: ExtractionMode,
    provider: &str,
    repair: bool,
    schema: Option<&serde_json::Value>,
    reject_undeclared: bool,
) -> std::result::Result<(T, bool), Rejection> {
    if let Some(refused) = refusal(response, mode, provider) {
        return Err(refused.into());
    }
    if let Some(finish_reason @ (FinishReason::Length | FinishReason::ContextWindow)) =
        &response.finish_reason
    {
        let usage = response.usage.as_ref();
        return Err(ExtractError::Truncated {
            mode,
            provider: provider.to_owned(),
            finish_reason: finish_reason.clone(),
            output_tokens: usage.and_then(|usage| usage.output_tokens),
            reasoning_tokens: usage.and_then(|usage| usage.reasoning_tokens),
        }
        .into());
    }
    let raw = match mode {
        ExtractionMode::ForcedTool => {
            let calls: Vec<_> = response
                .tool_calls()
                .filter(|call| call.name == EXTRACTION_TOOL)
                .collect();
            match calls.as_slice() {
                [] => {
                    return Err(ExtractError::MissingToolCall {
                        provider: provider.to_owned(),
                    }
                    .into());
                }
                [call] => call.arguments.clone(),
                many => {
                    return Err(ExtractError::UnexpectedToolCallCount {
                        provider: provider.to_owned(),
                        count: many.len(),
                    }
                    .into());
                }
            }
        }
        _ => response
            .text()
            .filter(|text| !text.trim().is_empty())
            .ok_or_else(|| ExtractError::MissingStructuredText {
                provider: provider.to_owned(),
            })?,
    };
    let mut failure = match crate::schema::from_str_checked(&raw, schema, reject_undeclared) {
        Ok(value) => return Ok((value, false)),
        Err(failure) => failure,
    };
    if repair && let Some(candidate) = repair_candidate(&raw) {
        match crate::schema::from_str_checked(candidate, schema, reject_undeclared) {
            Ok(value) => return Ok((value, true)),
            Err(candidate_failure) if !is_json_syntax_error(&candidate_failure.error) => {
                failure = candidate_failure;
            }
            Err(_) => {}
        }
    }
    let mut message = failure.error.to_string();
    if let Some(reason) = response
        .finish_reason
        .as_ref()
        .filter(|reason| !reason.finished_normally())
    {
        message.push_str(&format!(" (finish reason `{reason}`)"));
    }
    Err(Rejection {
        invalid_json: is_json_syntax_error(&failure.error),
        undeclared: failure.undeclared,
        error: ExtractError::Parse {
            mode,
            provider: provider.to_owned(),
            message,
            path: failure.path,
            raw,
        },
    })
}

/// The refusal in `response`, if it is one: a refusal the model wrote, or a
/// `Refusal` or `ContentFilter` finish.
fn refusal(response: &ChatResponse, mode: ExtractionMode, provider: &str) -> Option<ExtractError> {
    let written = model_refusal(response);
    let declined = matches!(
        response.finish_reason,
        Some(FinishReason::Refusal | FinishReason::ContentFilter)
    );
    if written.is_none() && !declined {
        return None;
    }
    let (text, source) = match (written, decline_explanation(response)) {
        (Some(text), _) => (Some(text.to_owned()), RefusalSource::Model),
        (None, Some(explanation)) => (Some(explanation.to_owned()), RefusalSource::Provider),
        (None, None) => (None, RefusalSource::None),
    };
    Some(ExtractError::Refused {
        mode,
        provider: provider.to_owned(),
        finish_reason: response.finish_reason.clone(),
        text,
        source,
    })
}

/// The refusal the model wrote in place of the output: its
/// [`Refusal`](crate::Refusal) block.
fn model_refusal(response: &ChatResponse) -> Option<&str> {
    response.first_refusal()
}

/// The finish reason of a [`ExtractError::Truncated`] recorded before it
/// had one: the output token limit.
fn output_limit() -> FinishReason {
    FinishReason::Length
}

/// The provider's explanation of its decline, stored by the adapter under
/// its namespace (see
/// [Decline explanations](crate::provider_data#decline-explanations)).
fn decline_explanation(response: &ChatResponse) -> Option<&str> {
    response
        .provider_data
        .namespaces()
        .filter(|(namespace, _)| *namespace != ALLAMA_NAMESPACE)
        .find_map(|(_, entries)| entries.get(DECLINE_EXPLANATION_KEY)?.as_str())
        .filter(|text| !text.trim().is_empty())
}

/// Whether `error` means the text is not valid JSON, as opposed to valid
/// JSON that does not fit the target type.
fn is_json_syntax_error(error: &serde_json::Error) -> bool {
    matches!(
        error.classify(),
        serde_json::error::Category::Syntax | serde_json::error::Category::Eof
    )
}

/// The JSON value inside `raw`, without a markdown fence or surrounding
/// prose. `None` when there is nothing to strip.
fn repair_candidate(raw: &str) -> Option<&str> {
    let trimmed = raw.trim();
    // A fenced block: skip the info string (```json) and cut at the closing
    // fence, ignoring prose after it.
    if let Some((_, body)) = trimmed
        .strip_prefix("```")
        .and_then(|fenced| fenced.split_once('\n'))
    {
        let body = body.find("```").map_or(body, |end| &body[..end]).trim();
        if !body.is_empty() {
            return Some(body);
        }
    }
    let start = trimmed.find(['{', '['])?;
    let end = trimmed.rfind(['}', ']'])?;
    let candidate = trimmed.get(start..=end)?;
    (candidate.len() < trimmed.len()).then_some(candidate)
}

/// Extend `request` with the failed response and feedback, for a retry.
fn ask_again(
    request: &mut ChatRequest,
    response: ChatResponse,
    mode: ExtractionMode,
    rejection: &Rejection,
) {
    let calls: Vec<_> = response.tool_calls().cloned().collect();
    let has_content = !response.content.is_empty();
    if has_content {
        request.push_message(response.into_assistant_message());
    }
    let problem = match &rejection.error {
        ExtractError::Parse { message, .. } if rejection.invalid_json => {
            format!("it was not valid JSON: {message}")
        }
        ExtractError::Parse {
            message,
            path: Some(path),
            ..
        } => format!("it did not match the schema at `{path}`: {message}"),
        ExtractError::Parse { message, .. } => format!("it did not match the schema: {message}"),
        ExtractError::MissingStructuredText { .. } => "it contained no text".to_owned(),
        ExtractError::MissingToolCall { .. } => {
            format!("it did not call the `{EXTRACTION_TOOL}` tool")
        }
        ExtractError::UnexpectedToolCallCount { count, .. } => {
            format!("it called the `{EXTRACTION_TOOL}` tool {count} times instead of once")
        }
        other => other.to_string(),
    };
    let forced = mode == ExtractionMode::ForcedTool;
    // Every replayed tool call needs a result before the next turn, in any
    // mode (a native-mode request may carry the caller's tools). An ordinary
    // result, not a failed one: the text already says the call failed, and
    // an endpoint with no tool error flag would otherwise warn about the
    // flag (the bundled adapters warn `approximated.tool_result_error`), or,
    // in an adapter that refuses it, fail the turn under the strict policy.
    if has_content {
        for call in &calls {
            let feedback = if forced {
                format!(
                    "The previous call could not be used: {problem}. Call `{EXTRACTION_TOOL}` once with arguments that match its schema."
                )
            } else {
                "This tool call was not run: a structured reply is required.".to_owned()
            };
            request.push_tool_result(call, feedback);
        }
    }
    if forced && has_content && !calls.is_empty() {
        return;
    }
    request.push_message(Message::user(if forced {
        format!(
            "The previous reply could not be used: {problem}. Call `{EXTRACTION_TOOL}` once with the extracted data."
        )
    } else {
        format!(
            "The previous reply could not be used: {problem}. Reply again with only the JSON value, matching the schema exactly."
        )
    }));
}

fn schema_name<T>(schema: &serde_json::Value) -> String {
    let raw = schema
        .get("title")
        .and_then(serde_json::Value::as_str)
        .map(str::to_owned)
        .unwrap_or_else(|| {
            std::any::type_name::<T>()
                .rsplit("::")
                .next()
                .unwrap_or("structured_output")
                .to_owned()
        });
    let mut name = String::with_capacity(raw.len());
    for ch in raw.chars() {
        if ch.is_ascii_alphanumeric() {
            name.push(ch.to_ascii_lowercase());
        } else if !name.ends_with('_') && !name.is_empty() {
            name.push('_');
        }
    }
    let trimmed = name.trim_matches('_');
    if trimmed.is_empty() {
        "structured_output".into()
    } else {
        trimmed.into()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mock::{ChatResponseBuilder, MockProvider, MockStreamEvent, MockStreamingProvider};
    use crate::{CapabilitySupport, UserContent};
    use futures_util::StreamExt;
    use serde_json::json;

    #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
    struct Person {
        name: String,
        age: u8,
    }

    fn request() -> ChatRequest {
        ChatRequest::new("m").user("Alice is 31")
    }

    fn alice() -> Person {
        Person {
            name: "Alice".into(),
            age: 31,
        }
    }

    /// The mock the docs recommend for handler tests answers an extraction
    /// in every mode.
    #[tokio::test]
    async fn extract_works_against_the_test_provider_in_every_mode() {
        #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
        #[serde(rename_all = "snake_case")]
        enum Category {
            Billing,
            Bug,
        }

        #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
        struct Triage {
            category: Category,
            urgent: bool,
            age: u8,
        }

        let provider = crate::mock::TestProvider::new();
        for mode in [
            ExtractionMode::Auto,
            ExtractionMode::Native,
            ExtractionMode::ForcedTool,
        ] {
            let request = ChatRequest::new("m")
                .user("My invoice is wrong")
                .with_option(mode);
            let extracted = provider
                .extract::<Triage>(&request)
                .await
                .unwrap_or_else(|error| panic!("{mode:?}: {error}"));
            assert_eq!(
                extracted.value,
                Triage {
                    category: Category::Billing,
                    urgent: true,
                    age: 0,
                },
                "{mode:?}"
            );
            let expected = match mode {
                ExtractionMode::ForcedTool => ExtractionMode::ForcedTool,
                _ => ExtractionMode::Native,
            };
            assert_eq!(extracted.metadata.mode, expected);
        }

        let mut stream = provider
            .extract_stream::<Triage>(&ChatRequest::new("m").user("My invoice is wrong"))
            .await
            .unwrap();
        while stream.next().await.is_some() {}
        assert_eq!(stream.finish().unwrap().value.category, Category::Billing);
    }

    #[tokio::test]
    async fn native_mode_uses_json_schema() {
        let provider = MockProvider::with_text(r#"{"name":"Alice","age":31}"#)
            .with_chat_capability(
                ChatCapability::StructuredOutput,
                CapabilitySupport::Supported,
            );
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(
            extracted.metadata,
            ExtractionMetadata {
                mode: ExtractionMode::Native,
                passes: 1,
                repaired: false,
                enforcement: SchemaEnforcement::Exact,
                degradations: Vec::new(),
                usage: None,
                auto_fallback: None,
                undeclared_key_rejections: 0,
            }
        );
        let sent = provider.last_request().unwrap();
        let Some(ResponseFormat::JsonSchema { name, strict, .. }) = sent.response_format else {
            panic!("expected json schema");
        };
        assert_eq!(name.as_deref(), Some("person"));
        assert_eq!(strict, Some(true));
    }

    /// Tier 2: verified tool answers beat a guess at native output.
    #[tokio::test]
    async fn auto_picks_forced_tool_without_structured_output() {
        let provider =
            MockProvider::with_tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
                .with_supported_chat_capabilities([
                    ChatCapability::ToolCalls,
                    ChatCapability::ForcedToolChoice,
                ]);
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.metadata.mode, ExtractionMode::ForcedTool);
        let sent = provider.last_request().unwrap();
        assert_eq!(
            sent.tool_choice,
            Some(ToolChoice::Specific {
                name: EXTRACTION_TOOL.into()
            })
        );
        assert!(sent.option::<ExtractionMode>().is_none());
    }

    /// The refusal an adapter returns, through `Degradations`, for a
    /// response format it cannot send.
    fn refused(field: RequestField) -> Error {
        crate::Degradations::new("demo", crate::UnsupportedPolicy::Warn)
            .unsupported(field, "map-typed properties cannot be expressed")
    }

    fn refusal_record() -> AutoFallback {
        AutoFallback::new(
            RequestField::ResponseFormat,
            "cannot send `response_format`: map-typed properties cannot be expressed",
        )
    }

    fn alice_call() -> serde_json::Value {
        json!({"name": "Alice", "age": 31})
    }

    const NATIVE_AND_TOOLS: [ChatCapability; 2] =
        [ChatCapability::StructuredOutput, ChatCapability::ToolCalls];

    /// When `Auto` only guessed native output from an `Unknown` answer, a
    /// provider's own refusal of the response format (a 4xx, nothing to
    /// bill) also falls back; after a verified `Supported` it does not.
    #[tokio::test]
    async fn a_guessed_native_mode_falls_back_on_the_providers_refusal() {
        let upstream = || {
            Error::unsupported_by_provider(
                Some(crate::Subject::Field {
                    field: RequestField::ResponseFormat,
                }),
                "HTTP 400: the model does not support `response_format`",
            )
            .authored()
        };
        let unknown = MockProvider::build(|b| {
            b.error(upstream())
                .tool_call("c", EXTRACTION_TOOL, alice_call())
        })
        .with_chat_capability(ChatCapability::StructuredOutput, CapabilitySupport::Unknown)
        .with_chat_capability(ChatCapability::ToolCalls, CapabilitySupport::Unknown)
        .with_chat_capability(ChatCapability::ForcedToolChoice, CapabilitySupport::Unknown);
        let extracted = unknown.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.metadata.mode, ExtractionMode::ForcedTool);
        let fallback = extracted.metadata.auto_fallback.unwrap();
        assert_eq!(fallback.stage, crate::UnsupportedStage::Provider);
        assert_eq!(fallback.field, RequestField::ResponseFormat);

        let verified = MockProvider::build(|b| {
            b.error(upstream())
                .tool_call("c", EXTRACTION_TOOL, alice_call())
        })
        .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let err = verified.extract::<Person>(&request()).await.unwrap_err();
        assert_eq!(
            err.unsupported_stage(),
            Some(crate::UnsupportedStage::Provider)
        );
        assert_eq!(verified.call_count(), 1);
    }

    #[tokio::test]
    async fn auto_retries_a_refused_response_format_in_forced_tool_mode() {
        let provider = MockProvider::build(|b| {
            b.error(refused(RequestField::ResponseFormat)).tool_call(
                "c",
                EXTRACTION_TOOL,
                alice_call(),
            )
        })
        .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(
            extracted.metadata,
            ExtractionMetadata {
                mode: ExtractionMode::ForcedTool,
                passes: 1,
                repaired: false,
                // The switch adds no degradation; the forced-tool pass
                // reported none.
                enforcement: SchemaEnforcement::Exact,
                degradations: Vec::new(),
                usage: None,
                auto_fallback: Some(refusal_record()),
                undeclared_key_rejections: 0,
            }
        );
        let [native, forced] = provider.requests().try_into().unwrap();
        assert!(native.response_format.is_some());
        assert!(native.tools.is_empty());
        assert!(forced.response_format.is_none());
        assert_eq!(
            forced.tool_choice,
            Some(ToolChoice::Specific {
                name: EXTRACTION_TOOL.into()
            })
        );
    }

    #[tokio::test]
    async fn only_a_refused_response_format_falls_back_and_only_from_auto() {
        let cases = [
            // An explicit mode is the caller's choice.
            (
                request().with_option(ExtractionMode::Native),
                refused(RequestField::ResponseFormat),
            ),
            // A refusal of another field, or one that names none (an
            // upstream reply mapped to `Unsupported`), is not about native
            // output.
            (request(), refused(RequestField::Temperature)),
            (request(), Error::unsupported("HTTP 400")),
            (request(), rate_limited()),
            // The provider refused the response format it received: the
            // call was sent, so Auto does not double-bill by retrying it.
            (
                request(),
                Error::unsupported_by_provider(
                    Some(crate::Subject::Field {
                        field: RequestField::ResponseFormat,
                    }),
                    "HTTP 400",
                ),
            ),
        ];
        for (request, error) in cases {
            let provider = MockProvider::build(|b| {
                b.error(error.clone())
                    .tool_call("c", EXTRACTION_TOOL, alice_call())
            })
            .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
            let err = provider.extract::<Person>(&request).await.unwrap_err();
            assert_eq!(err.kind_name(), error.kind_name(), "{err:?}");
            assert_eq!(err.unsupported_field(), error.unsupported_field());
            let record = ExtractionMetadata::from_error(&err).unwrap();
            assert_eq!(record.mode, ExtractionMode::Native);
            assert_eq!(record.auto_fallback, None);
            assert_eq!(provider.call_count(), 1, "{error:?}");
        }
    }

    #[tokio::test]
    async fn a_refusal_after_a_pass_does_not_switch_modes() {
        let provider = MockProvider::build(|b| {
            b.text("not json")
                .error(refused(RequestField::ResponseFormat))
        })
        .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let err = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request())
            .await
            .unwrap_err();
        assert_eq!(err.unsupported_field(), Some(RequestField::ResponseFormat));
        let record = ExtractionMetadata::from_error(&err).unwrap();
        assert_eq!((record.mode, record.passes), (ExtractionMode::Native, 1));
        assert_eq!(record.auto_fallback, None);
    }

    #[tokio::test]
    async fn a_refusal_ending_a_fallback_chain_falls_back() {
        let chained = refused(RequestField::ResponseFormat).with_fallback_from(rate_limited());
        let provider = MockProvider::build(|b| {
            b.error(chained)
                .tool_call("c", EXTRACTION_TOOL, alice_call())
        })
        .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.metadata.mode, ExtractionMode::ForcedTool);
        assert_eq!(extracted.metadata.auto_fallback, Some(refusal_record()));
    }

    #[tokio::test]
    async fn a_failed_forced_tool_attempt_reports_the_native_refusal_first() {
        let provider = MockProvider::build(|b| {
            b.error(refused(RequestField::ResponseFormat))
                .error(refused(RequestField::Tools))
        })
        .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let err = provider.extract::<Person>(&request()).await.unwrap_err();
        // The native refusal is the primary cause, in terms of the type.
        assert_eq!(err.unsupported_field(), Some(RequestField::ResponseFormat));
        assert_eq!(
            err.message(),
            "`Person` cannot be extracted from `m` with its output constrained: \
             native output was refused (cannot send `response_format`: map-typed properties cannot be expressed), \
             and forced-tool mode was refused too (cannot send `tools`: map-typed properties cannot be expressed)"
        );
        assert!(!err.message().contains(EXTRACTION_TOOL));
        let record = ExtractionMetadata::from_error(&err).unwrap();
        assert_eq!(
            (record.mode, record.passes),
            (ExtractionMode::ForcedTool, 0)
        );
        assert_eq!(record.auto_fallback, Some(refusal_record()));
        assert_eq!(provider.call_count(), 2);
    }

    /// A request with tools of its own is refused
    /// the same way in every mode, before anything is sent, so the same
    /// code does not work on one provider and fail on another.
    #[tokio::test]
    async fn request_tools_conflict_in_every_mode() {
        let request = request().tools([ToolDefinition::new("other", json!({"type": "object"}))]);
        for mode in [
            None,
            Some(ExtractionMode::Native),
            Some(ExtractionMode::ForcedTool),
        ] {
            for capabilities in [&NATIVE_AND_TOOLS[..], &[ChatCapability::ToolCalls][..]] {
                let provider = MockProvider::with_text(r#"{"name":"Alice","age":31}"#)
                    .with_supported_chat_capabilities(capabilities.iter().copied());
                let request = match mode {
                    Some(mode) => request.clone().with_option(mode),
                    None => request.clone(),
                };
                let failure = failure(provider.extract::<Person>(&request).await.unwrap_err());
                assert!(
                    matches!(failure.error(), ExtractError::ToolConflict { .. }),
                    "{mode:?}: {}",
                    failure.error()
                );
                assert!(
                    failure
                        .error()
                        .to_string()
                        .contains("start a fresh request")
                );
                assert_eq!(failure.metadata().passes, 0);
                assert_eq!(provider.call_count(), 0, "{mode:?}");
            }
        }
    }

    #[tokio::test]
    async fn a_refusal_without_authored_text_keeps_no_message() {
        // Only `Degradations` sets the field, and its text is authored; a
        // deserialized error may not be.
        let mut json = serde_json::to_value(refused(RequestField::ResponseFormat)).unwrap();
        json["data"]["context"]
            .as_object_mut()
            .unwrap()
            .remove("message_origin");
        let unauthored: Error = serde_json::from_value(json).unwrap();
        assert_eq!(unauthored.message_origin(), crate::MessageOrigin::Unknown);
        let provider = MockProvider::build(|b| {
            b.error(unauthored)
                .tool_call("c", EXTRACTION_TOOL, alice_call())
        })
        .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(
            extracted.metadata.auto_fallback,
            Some(AutoFallback::new(RequestField::ResponseFormat, ""))
        );
    }

    async fn drain<S, T>(stream: &mut ExtractionStream<S, T>) -> usize
    where
        S: Stream<Item = Result<StreamEvent>>,
    {
        let mut events = 0;
        while let Some(event) = stream.next().await {
            event.unwrap();
            events += 1;
        }
        events
    }

    /// A provider whose stream is its items as given, with no
    /// `finish_stream` to keep it to the stream rules.
    struct RawStream(std::sync::Mutex<Option<Vec<Result<StreamEvent>>>>);

    impl crate::ProviderIdentity for RawStream {
        fn provider_name(&self) -> &str {
            "raw"
        }

        fn reads_namespace(&self, namespace: &str) -> bool {
            namespace == "raw"
        }
    }

    impl ChatProvider for RawStream {
        type Stream = futures_util::stream::Iter<std::vec::IntoIter<Result<StreamEvent>>>;

        async fn chat(&self, _: &ChatRequest) -> Result<ChatResponse> {
            unreachable!("streams only")
        }

        async fn chat_stream(&self, _: &ChatRequest) -> Result<Self::Stream> {
            let items = self.0.lock().unwrap().take().unwrap_or_default();
            Ok(futures_util::stream::iter(items))
        }

        fn chat_capability(&self, _: &str, _: ChatCapability) -> CapabilitySupport {
            CapabilitySupport::Supported
        }
    }

    #[tokio::test]
    async fn a_streamed_extraction_ends_after_the_first_error() {
        let provider = RawStream(std::sync::Mutex::new(Some(vec![
            Ok(StreamEvent::response_start(None, None)),
            Err(Error::stream_decode("broken")),
            Ok(StreamEvent::text_delta(0, "after")),
        ])));
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        assert!(matches!(
            stream.next().await,
            Some(Ok(StreamEvent::ResponseStart { .. }))
        ));
        assert!(matches!(stream.next().await, Some(Err(_))));
        assert!(stream.next().await.is_none());
        assert!(stream.finish().is_err());
    }

    #[tokio::test]
    async fn streamed_auto_falls_back_on_a_setup_refusal() {
        let provider = MockStreamingProvider::build(|b| {
            b.setup_error(refused(RequestField::ResponseFormat))
                .tool_call("c", EXTRACTION_TOOL, alice_call())
        })
        .with_chat_capability(
            ChatCapability::StructuredOutput,
            CapabilitySupport::Supported,
        );
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        assert_eq!(stream.mode(), ExtractionMode::ForcedTool);
        drain(&mut stream).await;
        let extracted = stream.finish().unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.metadata.mode, ExtractionMode::ForcedTool);
        assert_eq!(extracted.metadata.passes, 1);
        assert_eq!(extracted.metadata.auto_fallback, Some(refusal_record()));
        assert_eq!(provider.call_count(), 2);
        assert!(provider.requests()[1].tool_choice.is_some());
    }

    #[tokio::test]
    async fn streamed_auto_falls_back_on_a_refusal_as_the_first_item() {
        let provider = MockStreamingProvider::new([
            vec![MockStreamEvent::from(refused(RequestField::ResponseFormat))],
            MockStreamEvent::tool_call_response("c", EXTRACTION_TOOL, alice_call()),
        ])
        .with_chat_capability(
            ChatCapability::StructuredOutput,
            CapabilitySupport::Supported,
        );
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        assert_eq!(stream.mode(), ExtractionMode::ForcedTool);
        drain(&mut stream).await;
        assert_eq!(
            serde_json::from_str::<serde_json::Value>(stream.partial_output()).unwrap(),
            alice_call()
        );
        let extracted = stream.finish().unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.metadata.auto_fallback, Some(refusal_record()));
        assert_eq!(provider.call_count(), 2);
    }

    #[tokio::test]
    async fn streamed_auto_yields_the_first_item_it_read() {
        let provider = MockStreamingProvider::with_text(r#"{"name":"Alice","age":31}"#)
            .with_chat_capability(
                ChatCapability::StructuredOutput,
                CapabilitySupport::Supported,
            );
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        let events = drain(&mut stream).await;
        let expected = MockStreamEvent::text_response(r#"{"name":"Alice","age":31}"#).len();
        assert_eq!(events, expected);
        let extracted = stream.finish().unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.metadata.mode, ExtractionMode::Native);
        assert_eq!(extracted.metadata.auto_fallback, None);

        // A first item that is not a refusal of the response format is
        // yielded as the stream's error.
        let provider = MockStreamingProvider::with_stream([MockStreamEvent::from(refused(
            RequestField::Tools,
        ))])
        .with_chat_capability(
            ChatCapability::StructuredOutput,
            CapabilitySupport::Supported,
        );
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        let first = stream.next().await.unwrap().unwrap_err();
        assert_eq!(first.unsupported_field(), Some(RequestField::Tools));
        assert!(stream.next().await.is_none());
        let err = stream.finish().unwrap_err();
        assert_eq!(err.unsupported_field(), Some(RequestField::Tools));
        assert_eq!(provider.call_count(), 1);

        // An empty stream was cut short: it fails where it would have.
        let provider = MockStreamingProvider::with_stream(Vec::<MockStreamEvent>::new())
            .with_chat_capability(
                ChatCapability::StructuredOutput,
                CapabilitySupport::Supported,
            );
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        let first = stream.next().await.unwrap().unwrap_err();
        assert!(matches!(
            first,
            Error::Stream {
                kind: crate::StreamErrorKind::Incomplete,
                ..
            }
        ));
        assert!(stream.next().await.is_none());
    }

    #[tokio::test]
    async fn streamed_failed_fallback_returns_the_forced_tool_error() {
        let provider = MockStreamingProvider::build(|b| {
            b.setup_error(refused(RequestField::ResponseFormat))
                .setup_error(rate_limited())
        })
        .with_chat_capability(
            ChatCapability::StructuredOutput,
            CapabilitySupport::Supported,
        );
        let Err(err) = provider.extract_stream::<Person>(&request()).await else {
            panic!("expected the forced-tool attempt's error");
        };
        assert!(matches!(err, Error::RateLimited { .. }), "{err:?}");
        let record = ExtractionMetadata::from_error(&err).unwrap();
        assert_eq!(record.mode, ExtractionMode::ForcedTool);
        assert_eq!(record.auto_fallback, Some(refusal_record()));

        // An explicit native stream never switches.
        let provider =
            MockStreamingProvider::build(|b| b.setup_error(refused(RequestField::ResponseFormat)));
        let request = request().with_option(ExtractionMode::Native);
        let Err(err) = provider.extract_stream::<Person>(&request).await else {
            panic!("expected the refusal");
        };
        assert_eq!(err.unsupported_field(), Some(RequestField::ResponseFormat));
        assert_eq!(provider.call_count(), 1);
    }

    #[test]
    fn the_auto_fallback_record_round_trips() {
        let mut metadata = ExtractionMetadata::new(ExtractionMode::Native);
        metadata.fall_back(refusal_record());
        let value = serde_json::to_value(&metadata).unwrap();
        assert_eq!(value["mode"], "forced_tool");
        assert_eq!(value["auto_fallback"]["field"], "response_format");
        assert_eq!(
            serde_json::from_value::<ExtractionMetadata>(value).unwrap(),
            metadata
        );
        let plain = serde_json::to_value(ExtractionMetadata::new(ExtractionMode::Native)).unwrap();
        assert!(plain.get("auto_fallback").is_none(), "{plain}");
    }

    /// Tier 5: the answers rule out both modes.
    #[tokio::test]
    async fn auto_skips_forced_tool_when_the_model_rejects_it() {
        let provider =
            MockProvider::with_tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
                .with_chat_capability(ChatCapability::ToolCalls, CapabilitySupport::Supported)
                .with_chat_capability(
                    ChatCapability::ForcedToolChoice,
                    CapabilitySupport::Unsupported,
                )
                .with_chat_capability(
                    ChatCapability::StructuredOutput,
                    CapabilitySupport::Unsupported,
                );
        let err = provider.extract::<Person>(&request()).await.unwrap_err();
        let Error::Extract { failure, .. } = err else {
            panic!("expected extract error");
        };
        assert!(
            matches!(failure.error(), ExtractError::Unsupported { .. }),
            "{failure:?}"
        );
        assert!(failure.response().is_none());
        assert_eq!(failure.metadata().passes, 0);
        assert_eq!(provider.call_count(), 0);
    }

    #[tokio::test]
    async fn explicit_mode_option_and_parse_errors() {
        let provider = MockProvider::with_text("not json");
        let request = request().with_option(ExtractionMode::Native);
        let err = provider.extract::<Person>(&request).await.unwrap_err();
        let Error::Extract { failure, .. } = err else {
            panic!("expected extract error");
        };
        assert_eq!(failure.error().raw_output(), Some("not json"));
    }

    /// Tier 3: with nothing known, native output is a guess, never a
    /// failure.
    #[tokio::test]
    async fn unknown_capabilities_guess_native_output() {
        let provider = MockProvider::with_text(r#"{"name":"Alice","age":31}"#);
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.metadata.mode, ExtractionMode::Native);
        assert!(provider.last_request().unwrap().response_format.is_some());

        // Even when the tools are verified, forced choice is not.
        let provider = MockProvider::with_text(r#"{"name":"Alice","age":31}"#)
            .with_chat_capability(ChatCapability::ToolCalls, CapabilitySupport::Supported);
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.metadata.mode, ExtractionMode::Native);
    }

    /// Tier 4: without native output, unknown tool answers are a guess too.
    #[tokio::test]
    async fn without_native_output_unknown_tools_are_guessed() {
        let provider =
            MockProvider::with_tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
                .with_chat_capability(
                    ChatCapability::StructuredOutput,
                    CapabilitySupport::Unsupported,
                );
        let extracted = provider.extract::<Person>(&request()).await.unwrap();
        assert_eq!(extracted.metadata.mode, ExtractionMode::ForcedTool);

        let provider = MockProvider::empty()
            .with_chat_capability(
                ChatCapability::StructuredOutput,
                CapabilitySupport::Unsupported,
            )
            .with_chat_capability(ChatCapability::ToolCalls, CapabilitySupport::Unsupported);
        let err = provider.extract::<Person>(&request()).await.unwrap_err();
        assert!(
            matches!(err, Error::Extract { ref failure, .. } if matches!(failure.error(), ExtractError::Unsupported { .. })),
            "{err:?}"
        );
        assert!(err.to_string().contains("capability resolver"), "{err}");
        assert_eq!(provider.call_count(), 0);
    }

    #[tokio::test]
    async fn forced_tool_conflicts_with_request_tools() {
        let provider = MockProvider::empty();
        let request = request()
            .with_option(ExtractionMode::ForcedTool)
            .tools([ToolDefinition::new("other", json!({"type": "object"}))]);
        assert!(provider.extract::<Person>(&request).await.is_err());
    }

    #[tokio::test]
    async fn extraction_through_wrappers() {
        let provider = crate::wrappers::RetryingProvider::new(
            MockProvider::with_response(
                ChatResponseBuilder::new()
                    .text(r#"{"name":"Bob","age":4}"#)
                    .build(),
            ),
            crate::wrappers::RetryPolicy::default(),
        );
        let request = ChatRequest::new("m")
            .user("Bob is 4")
            .with_option(ExtractionMode::Native);
        let extracted: Extracted<Person> =
            Extractor::new().extract(&provider, &request).await.unwrap();
        assert_eq!(extracted.value.name, "Bob");
    }

    #[test]
    fn repair_candidates() {
        assert_eq!(
            repair_candidate("```json\n{\"a\": 1}\n```"),
            Some("{\"a\": 1}")
        );
        assert_eq!(repair_candidate("```\n[1]\n```\n"), Some("[1]"));
        assert_eq!(
            repair_candidate("Sure! Here it is: {\"a\": 1}. Anything else?"),
            Some("{\"a\": 1}")
        );
        assert_eq!(repair_candidate("{\"a\": 1}"), None);
        assert_eq!(repair_candidate("no json here"), None);
        assert_eq!(repair_candidate("} backwards {"), None);
        assert_eq!(
            repair_candidate("```json\n{\"a\": 1}\n```\nHope this helps!"),
            Some("{\"a\": 1}")
        );
        assert_eq!(repair_candidate("```{\"a\": 1}```"), Some("{\"a\": 1}"));
    }

    #[tokio::test]
    async fn repair_is_opt_in_and_reported() {
        let fenced = "```json\n{\"name\":\"Alice\",\"age\":31}\n```";
        let request = request().with_option(ExtractionMode::Native);

        let strict = MockProvider::with_text(fenced);
        assert!(strict.extract::<Person>(&request).await.is_err());

        let provider = MockProvider::with_text(fenced);
        let extracted = Extractor::new()
            .repair(true)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value, alice());
        assert!(extracted.metadata.repaired);
        assert_eq!(provider.call_count(), 1);
    }

    #[tokio::test]
    async fn native_retries_send_the_failure_back() {
        let provider = MockProvider::build(|b| {
            b.text(r#"{"name":"Alice"}"#)
                .text(r#"{"name":"Alice","age":31}"#)
        });
        let request = request().with_option(ExtractionMode::Native);
        let extracted = Extractor::new()
            .retries(2)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.metadata.passes, 2);

        let retry = provider.last_request().unwrap();
        assert_eq!(retry.messages.len(), 3);
        assert!(retry.messages[1].as_assistant().is_some());
        let UserContent::Text(feedback) = &retry.messages[2].as_user().unwrap().content else {
            panic!("expected text feedback");
        };
        assert!(feedback.contains("missing field `age`"), "{feedback}");
    }

    #[tokio::test]
    async fn native_retries_answer_tool_calls_before_asking_again() {
        let provider = MockProvider::build(|b| {
            b.tool_call("c1", "lookup", json!({"q": "Alice"}))
                .text(r#"{"name":"Alice","age":31}"#)
        });
        // A model may call a tool it was not offered.
        let request = request().with_option(ExtractionMode::Native);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value, alice());

        let retry = provider.last_request().unwrap();
        assert_eq!(retry.messages.len(), 4);
        let result = retry.messages[2].as_tool().unwrap();
        assert_eq!(result.tool_call_id, "c1");
        // An ordinary result whose text says the call was not run.
        assert!(!result.is_error);
        let feedback = result.content.to_text().unwrap_or_default();
        assert!(feedback.contains("was not run"), "{feedback}");
        assert!(retry.messages[3].as_user().is_some());
    }

    #[tokio::test]
    async fn forced_tool_retries_answer_the_failed_call() {
        let provider = MockProvider::build(|b| {
            b.tool_call("c1", EXTRACTION_TOOL, json!({"name": "Alice"}))
                .tool_call("c2", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
        });
        let request = request().with_option(ExtractionMode::ForcedTool);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.metadata.passes, 2);

        let retry = provider.last_request().unwrap();
        let result = retry.messages[2].as_tool().unwrap();
        assert_eq!(result.tool_call_id, "c1");
        assert!(!result.is_error);
        let feedback = result.content.to_text().unwrap_or_default();
        assert!(feedback.contains("could not be used"), "{feedback}");
        assert!(feedback.contains("missing field `age`"), "{feedback}");
        assert_eq!(
            retry.tool_choice,
            Some(ToolChoice::Specific {
                name: EXTRACTION_TOOL.into()
            })
        );
    }

    /// The retry turn answered the failed call with a
    /// failed tool result, which an endpoint with no tool error flag refuses
    /// under the strict policy, so a strict extraction tripped over its own
    /// retry.
    #[tokio::test]
    async fn a_strict_retry_sends_on_an_endpoint_without_a_tool_error_flag() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};

        use crate::mock::FunctionProvider;

        let calls = Arc::new(AtomicUsize::new(0));
        let counter = Arc::clone(&calls);
        // Refuses a failed tool result under the strict policy, as an
        // adapter for such an endpoint does.
        let provider = FunctionProvider::new(move |request: &ChatRequest| {
            if counter.fetch_add(1, Ordering::SeqCst) == 0 {
                return Ok(ChatResponseBuilder::new()
                    .tool_call("c1", EXTRACTION_TOOL, json!({"name": "Alice"}))
                    .build());
            }
            if request
                .messages
                .iter()
                .any(|m| m.as_tool().is_some_and(|t| t.is_error))
            {
                return Err(Error::unsupported("no tool error flag"));
            }
            Ok(ChatResponseBuilder::new()
                .tool_call("c2", EXTRACTION_TOOL, alice_call())
                .build())
        })
        .with_chat_capability(ChatCapability::ToolCalls, CapabilitySupport::Supported)
        .with_chat_capability(
            ChatCapability::ForcedToolChoice,
            CapabilitySupport::Supported,
        );
        let request = request()
            .with_option(ExtractionMode::ForcedTool)
            .unsupported_policy(crate::UnsupportedPolicy::Error);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(calls.load(Ordering::SeqCst), 2);
    }

    #[tokio::test]
    async fn retries_are_bounded_and_skip_provider_errors() {
        let provider = MockProvider::build(|b| b.text("nope").text("still nope"));
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Extract { .. }));
        assert_eq!(provider.call_count(), 2);

        let failing = MockProvider::build(|b| b.error(Error::auth("bad key")));
        let err = Extractor::new()
            .retries(3)
            .extract::<_, Person>(&failing, &request)
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Auth { .. }));
        assert_eq!(failing.call_count(), 1);
    }

    #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
    enum Currency {
        Usd,
        Eur,
    }

    #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
    struct Price {
        currency: Currency,
    }

    /// The retry feedback the model received: the last user message.
    fn retry_feedback(provider: &MockProvider) -> String {
        let retry = provider.last_request().unwrap();
        let UserContent::Text(feedback) =
            &retry.messages.last().unwrap().as_user().unwrap().content
        else {
            panic!("expected text feedback");
        };
        feedback.clone()
    }

    #[tokio::test]
    async fn repair_reports_the_candidate_error_when_it_is_json() {
        let raw = "Here:\n{\"currency\":\"Gbp\"}";
        let provider = MockProvider::build(|b| b.text(raw).text(raw));
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .repair(true)
            .retries(1)
            .extract::<_, Price>(&provider, &request)
            .await
            .unwrap_err();
        let failure = failure(err);
        let ExtractError::Parse {
            message,
            raw: reported,
            ..
        } = failure.error()
        else {
            panic!("expected a parse failure, got {failure:?}");
        };
        assert!(message.contains("unknown variant `Gbp`"), "{message}");
        assert_eq!(reported, raw);

        let feedback = retry_feedback(&provider);
        assert!(
            feedback.contains("it did not match the schema at `currency`: unknown variant `Gbp`"),
            "{feedback}"
        );
    }

    #[tokio::test]
    async fn repair_keeps_the_raw_error_when_the_candidate_is_not_json() {
        let raw = "Here: {\"currency\": Usd}";
        let provider = MockProvider::with_text(raw);
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .repair(true)
            .extract::<_, Price>(&provider, &request)
            .await
            .unwrap_err();
        let failure = failure(err);
        let ExtractError::Parse {
            message,
            raw: reported,
            ..
        } = failure.error()
        else {
            panic!("expected a parse failure, got {failure:?}");
        };
        let raw_error = serde_json::from_str::<serde_json::Value>(raw)
            .unwrap_err()
            .to_string();
        assert_eq!(message, &raw_error);
        assert_eq!(reported, raw);
    }

    #[tokio::test]
    async fn retry_feedback_tells_json_errors_from_schema_errors() {
        let request = request().with_option(ExtractionMode::Native);

        let trailing_comma = MockProvider::build(|b| {
            b.text(r#"{"name":"Alice","age":31,}"#)
                .text(r#"{"name":"Alice","age":31}"#)
        });
        Extractor::new()
            .retries(1)
            .extract::<_, Person>(&trailing_comma, &request)
            .await
            .unwrap();
        let feedback = retry_feedback(&trailing_comma);
        assert!(feedback.contains("it was not valid JSON: "), "{feedback}");
        assert!(!feedback.contains("schema:"), "{feedback}");

        let wrong_type = MockProvider::build(|b| {
            b.text(r#"{"name":"Alice","age":"31"}"#)
                .text(r#"{"name":"Alice","age":31}"#)
        });
        Extractor::new()
            .retries(1)
            .extract::<_, Person>(&wrong_type, &request)
            .await
            .unwrap();
        let feedback = retry_feedback(&wrong_type);
        assert!(
            feedback.contains("it did not match the schema at `age`: invalid type"),
            "{feedback}"
        );

        // Cut-off JSON (an early end of input) is a JSON error too.
        let cut_off = MockProvider::build(|b| {
            b.text(r#"{"name":"Alice""#)
                .text(r#"{"name":"Alice","age":31}"#)
        });
        Extractor::new()
            .retries(1)
            .extract::<_, Person>(&cut_off, &request)
            .await
            .unwrap();
        assert!(retry_feedback(&cut_off).contains("it was not valid JSON: "));
    }

    #[tokio::test]
    async fn forced_tool_retry_feedback_tells_json_errors_from_schema_errors() {
        let provider = MockProvider::build(|b| {
            b.response(ChatResponse::new(vec![crate::ContentBlock::ToolCall(
                crate::ToolCall::new("c1", EXTRACTION_TOOL, r#"{"name":"Alice","#),
            )]))
            .tool_call("c2", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
        });
        let request = request().with_option(ExtractionMode::ForcedTool);
        Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        let retry = provider.last_request().unwrap();
        let result = retry.messages[2].as_tool().unwrap();
        let feedback = format!("{:?}", result.content);
        assert!(feedback.contains("it was not valid JSON: "), "{feedback}");
    }

    /// An order whose line items were extracted with one wrong field.
    #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
    struct Order {
        items: Vec<LineItem>,
    }

    #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
    struct LineItem {
        name: String,
        price: u32,
        #[serde(default)]
        tags: Vec<String>,
    }

    #[tokio::test]
    async fn parse_failures_name_the_field_path() {
        // The stand-in `null`s are removed first, so the path names the
        // wrong price, not the `tags` a strict mode made nullable.
        let raw = r#"{"items":[{"name":"a","price":1,"tags":null},{"name":"b","price":"2","tags":null}]}"#;
        let fixed = r#"{"items":[{"name":"a","price":1},{"name":"b","price":2}]}"#;
        let native = MockProvider::build(|b| b.text(raw).text(fixed));
        let request = request().with_option(ExtractionMode::Native);
        Extractor::new()
            .retries(1)
            .extract::<_, Order>(&native, &request)
            .await
            .unwrap();
        let feedback = retry_feedback(&native);
        assert!(
            feedback.contains(
                "it did not match the schema at `items[1].price`: invalid type: string \"2\""
            ),
            "{feedback}"
        );

        // The error holds the path, and its `Display` names it.
        let failure = failure(
            MockProvider::with_text(raw)
                .extract::<Order>(&request)
                .await
                .unwrap_err(),
        );
        let ExtractError::Parse { path, message, .. } = failure.error() else {
            panic!("expected a parse failure, got {failure:?}");
        };
        assert_eq!(path.as_deref(), Some("items[1].price"));
        assert_eq!(message, "invalid type: string \"2\", expected u32");
        let shown = failure.error().to_string();
        assert!(
            shown.ends_with(
                "using mode Native: at `items[1].price`: invalid type: string \"2\", expected u32"
            ),
            "{shown}"
        );

        // A missing field names the object that lacks it.
        let missing = parse_failure_of::<Order>(r#"{"items":[{"name":"a"}]}"#).await;
        assert_eq!(missing.0.as_deref(), Some("items[0]"));
        assert!(missing.1.contains("missing field `price`"), "{}", missing.1);

        // No path for invalid JSON or a failure at the root.
        assert_eq!(parse_failure_of::<Order>(r#"{"items":["#).await.0, None);
        assert_eq!(parse_failure_of::<Order>(r#""text""#).await.0, None);
    }

    #[tokio::test]
    async fn forced_tool_feedback_names_the_field_path() {
        let provider = MockProvider::build(|b| {
            b.tool_call("c1", EXTRACTION_TOOL, json!({"name": "Alice", "age": "31"}))
                .tool_call("c2", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
        });
        let request = request().with_option(ExtractionMode::ForcedTool);
        Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        let retry = provider.last_request().unwrap();
        let result = retry.messages[2].as_tool().unwrap();
        let feedback = format!("{:?}", result.content);
        assert!(
            feedback.contains("it did not match the schema at `age`: invalid type"),
            "{feedback}"
        );
    }

    #[tokio::test]
    async fn repaired_output_reports_the_candidate_path() {
        let raw = "Here you go:\n```json\n{\"items\":[{\"name\":\"a\",\"price\":-1}]}\n```";
        let (path, message) = parse_failure_with::<Order>(raw, Extractor::new().repair(true)).await;
        assert_eq!(path.as_deref(), Some("items[0].price"));
        assert!(message.contains("invalid value: integer `-1`"), "{message}");
    }

    /// The path and message of the parse failure for a native reply `raw`.
    async fn parse_failure_of<T>(raw: &str) -> (Option<String>, String)
    where
        T: DeserializeOwned + JsonSchema + fmt::Debug + Send + 'static,
    {
        parse_failure_with::<T>(raw, Extractor::new()).await
    }

    async fn parse_failure_with<T>(raw: &str, extractor: Extractor) -> (Option<String>, String)
    where
        T: DeserializeOwned + JsonSchema + fmt::Debug + Send + 'static,
    {
        let provider = MockProvider::with_text(raw);
        let request = request().with_option(ExtractionMode::Native);
        let failure = failure(
            extractor
                .extract::<_, T>(&provider, &request)
                .await
                .unwrap_err(),
        );
        let ExtractError::Parse { path, message, .. } = failure.error() else {
            panic!("expected a parse failure, got {failure:?}");
        };
        (path.clone(), message.clone())
    }

    #[tokio::test]
    async fn streamed_native_extraction() {
        let provider = MockStreamingProvider::with_text(r#"{"name":"Alice","age":31}"#);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        let mut events = 0;
        while let Some(event) = stream.next().await {
            event.unwrap();
            events += 1;
        }
        assert!(events > 0);
        assert_eq!(stream.partial_output(), r#"{"name":"Alice","age":31}"#);
        let extracted = stream.finish().unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.metadata.mode, ExtractionMode::Native);
        assert!(provider.requests()[0].response_format.is_some());
    }

    /// A strict mode made the optional properties nullable; the model sent
    /// `null` for each.
    #[derive(Debug, PartialEq, Deserialize, JsonSchema)]
    struct Profile {
        name: String,
        #[serde(default)]
        tags: Vec<String>,
        nickname: Option<String>,
    }

    const STRICT_NULLS: &str = r#"{"name":"Alice","tags":null,"nickname":null}"#;

    fn alice_profile() -> Profile {
        Profile {
            name: "Alice".into(),
            tags: Vec::new(),
            nickname: None,
        }
    }

    #[tokio::test]
    async fn strict_nulls_are_normalized_in_both_modes() {
        let native = MockProvider::with_text(STRICT_NULLS);
        let native_request = request().with_option(ExtractionMode::Native);
        let extracted = native.extract::<Profile>(&native_request).await.unwrap();
        assert_eq!(extracted.value, alice_profile());
        assert!(!extracted.metadata.repaired);

        let forced = MockProvider::with_tool_call(
            "c",
            EXTRACTION_TOOL,
            serde_json::from_str::<serde_json::Value>(STRICT_NULLS).unwrap(),
        );
        let forced_request = request().with_option(ExtractionMode::ForcedTool);
        let extracted = forced.extract::<Profile>(&forced_request).await.unwrap();
        assert_eq!(extracted.value, alice_profile());

        // Repair and normalization combine.
        let fenced = MockProvider::with_text(format!("```json\n{STRICT_NULLS}\n```"));
        let extracted = Extractor::new()
            .repair(true)
            .extract::<_, Profile>(&fenced, &native_request)
            .await
            .unwrap();
        assert_eq!(extracted.value, alice_profile());
        assert!(extracted.metadata.repaired);
    }

    #[tokio::test]
    async fn strict_nulls_are_normalized_in_streamed_extraction() {
        let native = MockStreamingProvider::with_text(STRICT_NULLS);
        let native_request = request().with_option(ExtractionMode::Native);
        let mut stream = native
            .extract_stream::<Profile>(&native_request)
            .await
            .unwrap();
        while stream.next().await.is_some() {}
        assert_eq!(stream.finish().unwrap().value, alice_profile());

        let forced = MockStreamingProvider::with_stream(MockStreamEvent::tool_call_response(
            "c",
            EXTRACTION_TOOL,
            serde_json::from_str::<serde_json::Value>(STRICT_NULLS).unwrap(),
        ));
        let forced_request = request().with_option(ExtractionMode::ForcedTool);
        let mut stream = forced
            .extract_stream::<Profile>(&forced_request)
            .await
            .unwrap();
        while stream.next().await.is_some() {}
        assert_eq!(stream.finish().unwrap().value, alice_profile());
    }

    #[tokio::test]
    async fn parse_failures_keep_the_raw_output_the_model_sent() {
        // `name` is required: its null is not a strict-mode stand-in.
        let raw = r#"{"name":null,"tags":null}"#;
        let provider = MockProvider::with_text(raw);
        let request = request().with_option(ExtractionMode::Native);
        let err = provider.extract::<Profile>(&request).await.unwrap_err();
        let Error::Extract { failure, .. } = err else {
            panic!("expected extract error");
        };
        assert_eq!(failure.error().raw_output(), Some(raw));
        // serde sees the null, not a missing field.
        let ExtractError::Parse { message, .. } = failure.error() else {
            panic!("expected a parse error");
        };
        assert!(message.starts_with("invalid type: null"), "{message}");
    }

    #[tokio::test]
    async fn streamed_forced_tool_extraction_tracks_the_tool_arguments() {
        let provider = MockStreamingProvider::with_stream(MockStreamEvent::tool_call_response(
            "c",
            EXTRACTION_TOOL,
            json!({"name": "Alice", "age": 31}),
        ));
        let request = request().with_option(ExtractionMode::ForcedTool);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        assert_eq!(
            serde_json::from_str::<serde_json::Value>(stream.partial_output()).unwrap(),
            json!({"name": "Alice", "age": 31})
        );
        assert_eq!(stream.finish().unwrap().value, alice());
    }

    #[tokio::test]
    async fn streamed_extraction_reports_stream_failures() {
        let provider = MockStreamingProvider::with_stream([
            MockStreamEvent::from(StreamEvent::ResponseStart {
                id: None,
                model: None,
            }),
            MockStreamEvent::from(StreamEvent::TextDelta {
                index: 0,
                text: r#"{"name":"#.into(),
            }),
            MockStreamEvent::from(Error::overloaded("busy")),
        ]);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        assert_eq!(stream.partial_output(), r#"{"name":"#);
        assert!(matches!(stream.finish(), Err(Error::Overloaded { .. })));
    }

    fn failure(err: Error) -> ExtractFailure {
        match err {
            Error::Extract { failure, .. } => *failure,
            other => panic!("expected an extraction failure, got {other:?}"),
        }
    }

    fn text_with_usage(text: &str, input: u64, output: u64) -> ChatResponse {
        ChatResponseBuilder::new()
            .text(text)
            .usage(input, output)
            .build()
    }

    #[tokio::test]
    async fn metadata_sums_usage_over_every_pass() {
        let provider = MockProvider::build(|b| {
            b.response(text_with_usage(r#"{"name":"Alice"}"#, 10, 4))
                .response(text_with_usage(r#"{"name":"Alice","age":31}"#, 20, 6))
        });
        let request = request().with_option(ExtractionMode::Native);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.metadata.passes, 2);
        let usage = extracted.metadata.usage.unwrap();
        assert_eq!(
            (usage.input_tokens, usage.output_tokens),
            (Some(30), Some(10))
        );
        // The response keeps its own usage.
        assert_eq!(extracted.response.usage.unwrap().input_tokens, Some(20));
    }

    #[tokio::test]
    async fn failures_carry_the_last_response_and_the_pass_record() {
        let provider = MockProvider::build(|b| {
            b.response(text_with_usage("nope", 10, 2))
                .response(text_with_usage("still nope", 11, 3))
        });
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();
        assert!(!err.is_retryable());
        let failure = failure(err);
        assert!(matches!(failure.error(), ExtractError::Parse { .. }));
        assert_eq!(
            failure.response().and_then(ChatResponse::text).as_deref(),
            Some("still nope")
        );
        assert_eq!(failure.metadata().mode, ExtractionMode::Native);
        assert_eq!(failure.metadata().passes, 2);
        assert_eq!(
            failure.metadata().usage.as_ref().unwrap().input_tokens,
            Some(21)
        );
        assert_eq!(
            failure.into_response().unwrap().text().as_deref(),
            Some("still nope")
        );
    }

    #[tokio::test]
    async fn truncated_output_fails_before_parsing_and_is_not_retried() {
        let provider = MockProvider::build(|b| {
            b.response(
                ChatResponseBuilder::new()
                    .text(r#"{"name":"Alice","age":31}"#)
                    .finish_reason(FinishReason::Length)
                    .usage(10, 64)
                    .build(),
            )
            .text(r#"{"name":"Alice","age":31}"#)
        });
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .repair(true)
            .retries(2)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();
        assert_eq!(provider.call_count(), 1);
        let failure = failure(err);
        assert!(
            matches!(
                failure.error(),
                ExtractError::Truncated {
                    mode: ExtractionMode::Native,
                    ..
                }
            ),
            "{failure:?}"
        );
        assert!(failure.to_string().contains("output token limit"));
        assert_eq!(failure.metadata().passes, 1);
        assert_eq!(
            failure.metadata().usage.as_ref().unwrap().output_tokens,
            Some(64)
        );
        assert_eq!(
            failure.response().unwrap().finish_reason,
            Some(FinishReason::Length)
        );
    }

    #[tokio::test]
    async fn a_full_context_window_is_truncation_with_its_own_remedy() {
        let provider = MockProvider::build(|b| {
            b.response(
                ChatResponseBuilder::new()
                    .text(r#"{"name":"Ali"#)
                    .finish_reason(FinishReason::ContextWindow)
                    .usage(190_000, 64)
                    .build(),
            )
            .text(r#"{"name":"Alice","age":31}"#)
        });
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .retries(2)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();
        assert!(!err.is_retryable());
        assert_eq!(provider.call_count(), 1);
        let failure = failure(err);
        assert!(
            matches!(
                failure.error(),
                ExtractError::Truncated {
                    finish_reason: FinishReason::ContextWindow,
                    ..
                }
            ),
            "{failure:?}"
        );
        let message = failure.to_string();
        assert!(message.contains("context window"), "{message}");
        assert!(message.contains("shorten the input"), "{message}");
        assert!(!message.contains("raise max_output_tokens"), "{message}");
        assert_eq!(failure.metadata().passes, 1);
    }

    #[tokio::test]
    async fn truncation_says_how_much_went_to_reasoning_when_reported() {
        let truncated = |usage: Usage| {
            ChatResponseBuilder::new()
                .finish_reason(FinishReason::Length)
                .usage_value(usage)
                .build()
        };
        let request = request().with_option(ExtractionMode::Native);

        let provider = MockProvider::with_response(truncated(
            Usage::new().output_tokens(400).reasoning_tokens(400),
        ));
        let reasoned = failure(provider.extract::<Person>(&request).await.unwrap_err());
        assert!(matches!(
            reasoned.error(),
            ExtractError::Truncated {
                output_tokens: Some(400),
                reasoning_tokens: Some(400),
                ..
            }
        ));
        assert!(
            reasoned.error().to_string().ends_with(
                "; 400 of 400 output tokens went to reasoning: raise max_output_tokens or lower the reasoning effort"
            ),
            "{}",
            reasoned.error()
        );

        // Without reported reasoning, nothing is claimed.
        let provider = MockProvider::with_response(truncated(Usage::new().output_tokens(400)));
        let unreported = failure(provider.extract::<Person>(&request).await.unwrap_err());
        assert!(!unreported.error().to_string().contains("reasoning"));
    }

    #[tokio::test]
    async fn truncated_forced_tool_arguments_are_reported() {
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice"}))
                .finish_reason(FinishReason::Length)
                .build(),
        );
        let request = request().with_option(ExtractionMode::ForcedTool);
        let failure = failure(provider.extract::<Person>(&request).await.unwrap_err());
        assert!(matches!(
            failure.error(),
            ExtractError::Truncated {
                mode: ExtractionMode::ForcedTool,
                ..
            }
        ));
    }

    fn declined(reason: FinishReason) -> ChatResponseBuilder {
        ChatResponseBuilder::new()
            .text("I can't help with that.")
            .finish_reason(reason)
            .usage(10, 6)
    }

    #[tokio::test]
    async fn refusals_fail_before_parsing_and_are_not_retried() {
        for reason in [FinishReason::Refusal, FinishReason::ContentFilter] {
            let provider = MockProvider::build(|b| {
                b.response(declined(reason.clone()).build())
                    .text(r#"{"name":"Alice","age":31}"#)
            });
            let request = request().with_option(ExtractionMode::Native);
            let err = Extractor::new()
                .repair(true)
                .retries(2)
                .extract::<_, Person>(&provider, &request)
                .await
                .unwrap_err();
            assert_eq!(provider.call_count(), 1, "{reason}");
            assert!(!err.is_retryable());
            let failure = failure(err);
            let ExtractError::Refused {
                mode,
                finish_reason,
                text,
                source,
                ..
            } = failure.error()
            else {
                panic!("a refusal expected: {failure:?}");
            };
            assert_eq!(*mode, ExtractionMode::Native);
            assert_eq!(finish_reason.as_ref(), Some(&reason));
            // Nothing explains it: the text the model wrote before the
            // finish is partial output, not the refusal.
            assert_eq!((text, source), (&None, &RefusalSource::None));
            assert_eq!(
                failure.to_string(),
                format!(
                    "mock: the model refused to produce the structured output (finish reason `{reason}`)"
                )
            );
            assert_eq!(failure.metadata().passes, 1);
            assert_eq!(
                failure.metadata().usage.as_ref().unwrap().output_tokens,
                Some(6)
            );
        }
    }

    #[tokio::test]
    async fn a_providers_explanation_is_the_refusal_text() {
        let mut provider_data = crate::ProviderData::new();
        provider_data.insert_raw(
            "acme",
            DECLINE_EXPLANATION_KEY,
            json!("The request touches a restricted topic."),
        );
        let response = ChatResponseBuilder::new()
            .finish_reason(FinishReason::Refusal)
            .build()
            .provider_data(provider_data);
        let provider = MockProvider::with_response(response);
        let request = request().with_option(ExtractionMode::ForcedTool);
        let failure = failure(provider.extract::<Person>(&request).await.unwrap_err());
        assert!(
            matches!(
                failure.error(),
                ExtractError::Refused {
                    mode: ExtractionMode::ForcedTool,
                    text: Some(text),
                    source: RefusalSource::Provider,
                    ..
                } if text == "The request touches a restricted topic."
            ),
            "{failure:?}"
        );
        assert_eq!(
            failure.to_string(),
            "mock: the model refused: The request touches a restricted topic."
        );
        // The redacted log line leaves the upstream text out.
        let log = Error::from(failure).as_log().to_string();
        assert!(!log.contains("restricted"), "{log}");
        assert!(log.contains("finish reason `refusal`"), "{log}");
    }

    #[tokio::test]
    async fn a_streamed_refusal_fails_at_finish() {
        let provider =
            MockStreamingProvider::from_responses([declined(FinishReason::ContentFilter).build()]);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let failure = failure(stream.finish().unwrap_err());
        assert!(
            matches!(
                failure.error(),
                ExtractError::Refused {
                    finish_reason: Some(FinishReason::ContentFilter),
                    ..
                }
            ),
            "{failure:?}"
        );
    }

    #[tokio::test]
    async fn odd_finishes_are_named_in_the_parse_message() {
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .text("{\"name\":")
                .finish_reason(FinishReason::Other("malformed_function_call".into()))
                .build(),
        );
        let request = request().with_option(ExtractionMode::Native);
        let odd = failure(provider.extract::<Person>(&request).await.unwrap_err());
        let ExtractError::Parse { message, .. } = odd.error() else {
            panic!("a parse failure expected: {odd:?}");
        };
        assert!(
            message.ends_with("(finish reason `malformed_function_call`)"),
            "{message}"
        );

        // A normal finish adds nothing.
        let provider = MockProvider::with_text("{\"name\":");
        let normal = failure(provider.extract::<Person>(&request).await.unwrap_err());
        let ExtractError::Parse { message, .. } = normal.error() else {
            panic!("a parse failure expected: {normal:?}");
        };
        assert!(!message.contains("finish reason"), "{message}");
    }

    #[tokio::test]
    async fn blank_output_is_missing_structured_text() {
        for text in ["", "  \n\t "] {
            let provider = MockProvider::with_text(text);
            let request = request().with_option(ExtractionMode::Native);
            let failure = failure(provider.extract::<Person>(&request).await.unwrap_err());
            assert!(
                matches!(failure.error(), ExtractError::MissingStructuredText { .. }),
                "{text:?}: {failure:?}"
            );
        }
    }

    #[test]
    fn refusal_errors_round_trip() {
        let error = ExtractError::Refused {
            mode: ExtractionMode::Native,
            provider: "acme".into(),
            finish_reason: Some(FinishReason::ContentFilter),
            text: Some("filtered".into()),
            source: RefusalSource::Provider,
        };
        let json = serde_json::to_value(&error).unwrap();
        assert_eq!(json["type"], "refused");
        assert_eq!(json["source"], "provider");
        let back: ExtractError = serde_json::from_value(json).unwrap();
        assert_eq!(back.to_string(), "acme: the model refused: filtered");
    }

    fn rate_limited() -> Error {
        Error::rate_limited("HTTP 429")
            .authored()
            .with_provider("mock")
            .with_status(429)
            .with_retry_after(std::time::Duration::from_secs(7))
    }

    #[tokio::test]
    async fn provider_errors_on_a_retry_carry_the_earlier_passes() {
        let provider = MockProvider::build(|b| {
            b.response(text_with_usage(r#"{"name":"Alice"}"#, 500, 50))
                .error(rate_limited())
        });
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();

        // The provider error is returned unchanged, not reclassified.
        assert!(matches!(err, Error::RateLimited { .. }), "{err:?}");
        assert_eq!(err.kind_name(), "rate_limited");
        assert!(err.is_retryable());
        assert_eq!(err.retry_after(), Some(std::time::Duration::from_secs(7)));

        let metadata = ExtractionMetadata::from_error(&err).unwrap();
        assert_eq!(metadata.mode, ExtractionMode::Native);
        assert_eq!(metadata.passes, 1);
        let usage = metadata.usage.unwrap();
        assert_eq!(
            (usage.input_tokens, usage.output_tokens),
            (Some(500), Some(50))
        );
    }

    #[tokio::test]
    async fn provider_errors_on_the_first_pass_carry_an_empty_record() {
        let provider = MockProvider::build(|b| b.error(rate_limited()));
        let request = request().with_option(ExtractionMode::ForcedTool);
        let err = provider.extract::<Person>(&request).await.unwrap_err();
        assert!(matches!(err, Error::RateLimited { .. }), "{err:?}");
        assert_eq!(
            ExtractionMetadata::from_error(&err),
            Some(ExtractionMetadata::new(ExtractionMode::ForcedTool))
        );
    }

    #[tokio::test]
    async fn stream_setup_errors_carry_an_empty_record() {
        let provider = MockStreamingProvider::build(|b| b.setup_error(rate_limited()));
        let request = request().with_option(ExtractionMode::Native);
        let Err(err) = provider.extract_stream::<Person>(&request).await else {
            panic!("expected a setup error");
        };
        assert!(matches!(err, Error::RateLimited { .. }), "{err:?}");
        assert_eq!(err.retry_after(), Some(std::time::Duration::from_secs(7)));
        assert_eq!(
            ExtractionMetadata::from_error(&err),
            Some(ExtractionMetadata::new(ExtractionMode::Native))
        );
    }

    #[tokio::test]
    async fn stream_errors_returned_by_finish_carry_the_record() {
        let provider = MockStreamingProvider::with_stream([
            MockStreamEvent::from(StreamEvent::ResponseStart {
                id: None,
                model: None,
            }),
            MockStreamEvent::from(Error::overloaded("busy")),
        ]);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let err = stream.finish().unwrap_err();
        assert!(matches!(err, Error::Overloaded { .. }), "{err:?}");
        assert!(err.is_retryable());
        assert_eq!(
            ExtractionMetadata::from_error(&err),
            Some(ExtractionMetadata::new(ExtractionMode::Native))
        );
    }

    /// A stream that reported a degradation and its usage, then failed
    /// before finishing.
    fn failing_mid_stream() -> Vec<MockStreamEvent> {
        vec![
            MockStreamEvent::from(StreamEvent::response_start(None, None)),
            MockStreamEvent::from(StreamEvent::warning(strict_dropped(
                RequestField::ResponseFormat,
            ))),
            MockStreamEvent::from(StreamEvent::text_delta(0, r#"{"name":"#)),
            MockStreamEvent::from(
                crate::chat::stream::MetadataBuilder::new()
                    .with_usage(Usage::new().input_tokens(900).output_tokens(4))
                    .build(),
            ),
            MockStreamEvent::from(Error::overloaded("busy")),
        ]
    }

    #[tokio::test]
    async fn a_stream_failing_mid_way_keeps_its_usage_and_degradations() {
        let provider = MockStreamingProvider::with_stream(failing_mid_stream());
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let err = stream.finish().unwrap_err();
        assert!(matches!(err, Error::Overloaded { .. }), "{err:?}");
        let record = ExtractionMetadata::from_error(&err).unwrap();
        // Billed, but not a pass: the stream returned no response.
        assert_eq!(record.passes, 0);
        assert_eq!(
            record.usage.as_ref().and_then(|u| u.input_tokens),
            Some(900)
        );
        assert_eq!(record.usage.as_ref().and_then(|u| u.output_tokens), Some(4));
        assert_eq!(
            record.degradations,
            [strict_dropped(RequestField::ResponseFormat)]
        );
        assert_eq!(record.enforcement, SchemaEnforcement::NotEnforced);
    }

    #[tokio::test]
    async fn a_stream_ending_early_keeps_its_usage() {
        // No error item: the stream just stops before `ResponseStop`.
        let mut events = failing_mid_stream();
        events.pop();
        let provider = MockStreamingProvider::with_stream(events);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let err = stream.finish().unwrap_err();
        assert!(matches!(err, Error::Stream { .. }), "{err:?}");
        let record = ExtractionMetadata::from_error(&err).unwrap();
        assert_eq!(record.passes, 0);
        assert_eq!(record.usage.and_then(|u| u.input_tokens), Some(900));
    }

    #[tokio::test]
    async fn a_failed_stream_after_a_fallback_keeps_both_records() {
        let mut events = vec![
            MockStreamEvent::from(StreamEvent::response_start(None, None)),
            MockStreamEvent::from(StreamEvent::tool_call_start(0, "c", EXTRACTION_TOOL)),
            MockStreamEvent::from(StreamEvent::tool_call_delta(0, r#"{"name":"#)),
        ];
        events.extend(failing_mid_stream().into_iter().skip(3));
        let provider = MockStreamingProvider::build(|b| {
            b.setup_error(refused(RequestField::ResponseFormat))
                .stream(events)
        })
        .with_chat_capability(
            ChatCapability::StructuredOutput,
            CapabilitySupport::Supported,
        );
        let mut stream = provider.extract_stream::<Person>(&request()).await.unwrap();
        while stream.next().await.is_some() {}
        let err = stream.finish().unwrap_err();
        let record = ExtractionMetadata::from_error(&err).unwrap();
        assert_eq!(record.mode, ExtractionMode::ForcedTool);
        assert_eq!(record.auto_fallback, Some(refusal_record()));
        assert_eq!(record.passes, 0);
        assert_eq!(record.usage.and_then(|u| u.input_tokens), Some(900));
    }

    #[tokio::test]
    async fn a_failed_failover_carries_the_record_on_its_own_context() {
        let provider = MockProvider::build(|b| {
            b.response(text_with_usage("nope", 10, 2))
                .error(rate_limited().with_fallback_from(Error::timeout("primary")))
        });
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();
        assert!(matches!(err, Error::RateLimited { .. }), "{err:?}");
        assert_eq!(ExtractionMetadata::from_error(&err).unwrap().passes, 1);
        let primary = err.fallback_from().unwrap();
        assert!(matches!(primary, Error::Timeout { .. }), "{primary:?}");
        assert!(ExtractionMetadata::from_error(primary).is_none());
    }

    #[tokio::test]
    async fn errors_without_a_record_report_none() {
        assert_eq!(ExtractionMetadata::from_error(&rate_limited()), None);
        // A variant without a context cannot carry the record.
        let provider = MockProvider::build(|b| {
            b.error(Error::serialization(
                serde_json::from_str::<serde_json::Value>("{").unwrap_err(),
            ))
        });
        let request = request().with_option(ExtractionMode::Native);
        let err = provider.extract::<Person>(&request).await.unwrap_err();
        assert!(matches!(err, Error::Serialization { .. }));
        assert_eq!(ExtractionMetadata::from_error(&err), None);
    }

    #[tokio::test]
    async fn error_log_keeps_the_extraction_record() {
        let provider = MockProvider::build(|b| {
            b.response(text_with_usage(r#"{"name":"Alice"}"#, 500, 50))
                .error(rate_limited().with_body("secret body"))
        });
        let request = request().with_option(ExtractionMode::Native);
        let mut err = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap_err();
        err.context_mut()
            .unwrap()
            .metadata
            .insert("vendor.detail".into(), json!("may quote input"));

        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["allama.extraction"]["passes"], 1);
        assert_eq!(log["allama.extraction"]["mode"], "native");
        assert_eq!(log["allama.extraction"]["usage"]["input_tokens"], 500);
        assert!(log.get("vendor.detail").is_none());
        assert!(!log.to_string().contains("may quote input"));
        assert_eq!(
            err.as_log().to_string(),
            "kind=rate_limited provider=mock status=429 body_len=11 \
             allama.extraction.mode=native allama.extraction.passes=1 \
             allama.extraction.input_tokens=500 allama.extraction.output_tokens=50 \
             message=\"HTTP 429\""
        );
    }

    #[tokio::test]
    async fn missing_text_after_tool_calls_keeps_the_response() {
        let provider = MockProvider::with_tool_call("c1", "lookup", json!({"q": "Alice"}))
            .with_chat_capability(
                ChatCapability::StructuredOutput,
                CapabilitySupport::Supported,
            );
        let failure = failure(provider.extract::<Person>(&request()).await.unwrap_err());
        assert!(matches!(
            failure.error(),
            ExtractError::MissingStructuredText { .. }
        ));
        let calls: Vec<_> = failure.response().unwrap().tool_calls().collect();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].name, "lookup");
    }

    #[tokio::test]
    async fn extract_keeps_sampling_settings() {
        let provider = MockProvider::with_text(r#"{"name":"Alice","age":31}"#);
        let request = request()
            .temperature(0.0)
            .seed(7)
            .with_option(ExtractionMode::Native);
        Extractor::new()
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        let sent = provider.last_request().unwrap();
        assert_eq!((sent.temperature, sent.seed), (Some(0.0), Some(7)));
    }

    #[test]
    fn parse_uses_an_existing_response() {
        let response = ChatResponseBuilder::new()
            .text("Here: {\"name\":\"Alice\",\"age\":31}")
            .usage(5, 5)
            .build();
        let extracted = Extractor::new()
            .repair(true)
            .parse::<Person>(response.clone(), ExtractionMode::Native)
            .unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(extracted.response, response);
        assert_eq!(
            extracted.metadata,
            ExtractionMetadata {
                mode: ExtractionMode::Native,
                passes: 0,
                repaired: true,
                enforcement: SchemaEnforcement::NotEnforced,
                degradations: Vec::new(),
                usage: None,
                auto_fallback: None,
                undeclared_key_rejections: 0,
            }
        );

        let strict = failure(
            Extractor::new()
                .parse::<Person>(response.clone(), ExtractionMode::Native)
                .unwrap_err(),
        );
        assert!(matches!(strict.error(), ExtractError::Parse { .. }));
        assert_eq!(strict.response(), Some(&response));

        let tool = ChatResponseBuilder::new()
            .tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
            .build();
        let extracted = Extractor::new()
            .parse::<Person>(tool, ExtractionMode::ForcedTool)
            .unwrap();
        assert_eq!(extracted.value, alice());
    }

    #[test]
    fn parse_rejects_auto() {
        let mut response = ChatResponse::new(vec![crate::ContentBlock::text("{}")]);
        response.provider = Some("demo".into());
        let failure = failure(
            Extractor::new()
                .parse::<Person>(response, ExtractionMode::Auto)
                .unwrap_err(),
        );
        assert!(matches!(
            failure.error(),
            ExtractError::Unsupported { provider, .. } if provider == "demo"
        ));
        assert!(failure.response().is_some());
    }

    #[tokio::test]
    async fn streamed_failures_carry_the_collected_response() {
        let provider = MockStreamingProvider::from_responses([text_with_usage("nope", 3, 1)]);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let failure = failure(stream.finish().unwrap_err());
        assert!(matches!(failure.error(), ExtractError::Parse { .. }));
        assert_eq!(failure.metadata().passes, 1);
        assert_eq!(
            failure.metadata().usage.as_ref().unwrap().input_tokens,
            Some(3)
        );
        assert_eq!(
            failure.response().and_then(ChatResponse::text).as_deref(),
            Some("nope")
        );
    }

    #[tokio::test]
    async fn error_log_omits_the_failed_response() {
        let provider = MockProvider::with_text("secret model output");
        let request = request().with_option(ExtractionMode::Native);
        let err = provider.extract::<Person>(&request).await.unwrap_err();

        let log = serde_json::to_value(err.as_log()).unwrap();
        assert_eq!(log["type"], "extract");
        assert!(!log.to_string().contains("secret model output"), "{log}");
        assert!(!err.as_log().to_string().contains("secret model output"));

        // The full serde form keeps it, and round-trips.
        let full = serde_json::to_value(&err).unwrap();
        assert!(full.to_string().contains("secret model output"));
        let back = failure(serde_json::from_value(full).unwrap());
        assert_eq!(
            back.response().and_then(ChatResponse::text).as_deref(),
            Some("secret model output")
        );
        assert_eq!(back.metadata().passes, 1);
    }

    #[tokio::test]
    async fn the_synthetic_tool_is_strict() {
        let provider =
            MockProvider::with_tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}));
        let request = request().with_option(ExtractionMode::ForcedTool);
        let extracted = provider.extract::<Person>(&request).await.unwrap();
        assert_eq!(extracted.metadata.enforcement, SchemaEnforcement::Exact);
        let sent = provider.last_request().unwrap();
        let tools = sent.tools;
        assert_eq!(tools.len(), 1);
        assert_eq!(tools[0].name, EXTRACTION_TOOL);
        assert_eq!(tools[0].strict, Some(true));
        assert_eq!(sent.parallel_tool_calls, None);
    }

    /// Extraction never sets `parallel_tool_calls`, whatever the model id:
    /// the caller's value reaches the adapter, which applies its dialect's
    /// rule.
    #[tokio::test]
    async fn the_callers_parallel_tool_calls_is_kept() {
        for model in ["gpt-4.1", "ft:gpt-4o-mini-2024-07-18:acme::abc123"] {
            for (setting, mode) in [
                (None, ExtractionMode::ForcedTool),
                (Some(true), ExtractionMode::ForcedTool),
                (Some(false), ExtractionMode::ForcedTool),
                (Some(true), ExtractionMode::Native),
            ] {
                let provider = if mode == ExtractionMode::Native {
                    MockProvider::with_text(r#"{"name":"Alice","age":31}"#)
                } else {
                    MockProvider::with_tool_call(
                        "c",
                        EXTRACTION_TOOL,
                        json!({"name": "Alice", "age": 31}),
                    )
                };
                let mut request = ChatRequest::new(model)
                    .user("Alice is 31")
                    .with_option(mode);
                request.parallel_tool_calls = setting;
                provider.extract::<Person>(&request).await.unwrap();
                assert_eq!(
                    provider.last_request().unwrap().parallel_tool_calls,
                    setting,
                    "{model} {mode:?}"
                );
            }
        }
    }

    fn strict_dropped(field: RequestField) -> Warning {
        Warning::unsupported(field, "strict is not supported; sent without strict")
            .with_provider("mock")
    }

    #[tokio::test]
    async fn unsupported_degradations_are_reported_as_not_enforced() {
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
                .warning(strict_dropped(RequestField::Tools))
                .warning(Warning::unsupported(RequestField::Seed, "seed dropped"))
                .build(),
        );
        let request = request().with_option(ExtractionMode::ForcedTool);
        let extracted = provider.extract::<Person>(&request).await.unwrap();
        assert_eq!(extracted.value, alice());
        assert_eq!(
            extracted.metadata.enforcement,
            SchemaEnforcement::NotEnforced
        );
        // Only tool warnings count in `ForcedTool` mode.
        assert_eq!(
            extracted.metadata.degradations,
            [strict_dropped(RequestField::Tools)]
        );
        assert_eq!(extracted.response.warnings.len(), 2);
    }

    fn approximated(field: RequestField) -> Warning {
        Warning::new(
            WarningKind::Approximated,
            Subject::Field { field },
            "`pattern` was removed",
        )
        .with_provider("mock")
    }

    #[tokio::test]
    async fn approximated_degradations_are_reported_as_approximate() {
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .text(r#"{"name":"Alice","age":31}"#)
                .warning(approximated(RequestField::ResponseFormat))
                .build(),
        );
        let request = request().with_option(ExtractionMode::Native);
        let extracted = provider.extract::<Person>(&request).await.unwrap();
        assert_eq!(
            extracted.metadata.enforcement,
            SchemaEnforcement::Approximate
        );
        assert_eq!(
            extracted.metadata.degradations,
            [approximated(RequestField::ResponseFormat)]
        );
    }

    #[tokio::test]
    async fn only_the_field_the_mode_relies_on_counts() {
        // Native mode: a tools warning leaves the response format enforced.
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .text(r#"{"name":"Alice","age":31}"#)
                .warning(strict_dropped(RequestField::Tools))
                .build(),
        );
        let native = request().with_option(ExtractionMode::Native);
        let extracted = provider.extract::<Person>(&native).await.unwrap();
        assert_eq!(extracted.metadata.enforcement, SchemaEnforcement::Exact);
        assert!(extracted.metadata.degradations.is_empty());
        assert_eq!(extracted.response.warnings.len(), 1);

        // `ForcedTool` mode: a response-format warning does not count.
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .tool_call("c", EXTRACTION_TOOL, json!({"name": "Alice", "age": 31}))
                .warning(strict_dropped(RequestField::ResponseFormat))
                .build(),
        );
        let request = request().with_option(ExtractionMode::ForcedTool);
        let extracted = provider.extract::<Person>(&request).await.unwrap();
        assert_eq!(extracted.metadata.enforcement, SchemaEnforcement::Exact);
        assert!(extracted.metadata.degradations.is_empty());
    }

    #[tokio::test]
    async fn the_least_enforced_pass_wins() {
        let provider = MockProvider::build(|b| {
            b.response(
                ChatResponseBuilder::new()
                    .text("not json")
                    .warning(approximated(RequestField::ResponseFormat))
                    .build(),
            )
            .response(
                ChatResponseBuilder::new()
                    .text(r#"{"name":"Alice","age":31}"#)
                    .warning(strict_dropped(RequestField::ResponseFormat))
                    .build(),
            )
        });
        let request = request().with_option(ExtractionMode::Native);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(
            extracted.metadata.enforcement,
            SchemaEnforcement::NotEnforced
        );
        assert_eq!(extracted.metadata.degradations.len(), 2);
    }

    #[test]
    fn enforcement_levels_serialize_in_snake_case() {
        for (level, name) in [
            (SchemaEnforcement::Exact, "exact"),
            (SchemaEnforcement::Approximate, "approximate"),
            (SchemaEnforcement::NotEnforced, "not_enforced"),
        ] {
            assert_eq!(serde_json::to_value(level).unwrap(), name);
            assert_eq!(
                serde_json::from_value::<SchemaEnforcement>(json!(name)).unwrap(),
                level
            );
        }
    }

    #[tokio::test]
    async fn degradations_are_collected_once_over_every_pass() {
        let degraded = |text: &str| {
            ChatResponseBuilder::new()
                .text(text)
                .warning(strict_dropped(RequestField::ResponseFormat))
                .build()
        };
        let provider = MockProvider::build(|b| {
            b.response(degraded(r#"{"name":"Alice"}"#))
                .response(degraded(r#"{"name":"Alice","age":31}"#))
        });
        let request = request().with_option(ExtractionMode::Native);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Person>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.metadata.passes, 2);
        assert_eq!(
            extracted.metadata.enforcement,
            SchemaEnforcement::NotEnforced
        );
        assert_eq!(
            extracted.metadata.degradations,
            [strict_dropped(RequestField::ResponseFormat)]
        );
    }

    #[tokio::test]
    async fn extraction_failures_carry_the_degradations() {
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .text("not json")
                .warning(strict_dropped(RequestField::ResponseFormat))
                .build(),
        );
        let request = request().with_option(ExtractionMode::Native);
        let failure = failure(provider.extract::<Person>(&request).await.unwrap_err());
        assert!(matches!(failure.error(), ExtractError::Parse { .. }));
        assert_eq!(
            failure.metadata().enforcement,
            SchemaEnforcement::NotEnforced
        );
        assert_eq!(
            failure.metadata().degradations,
            [strict_dropped(RequestField::ResponseFormat)]
        );

        // Nothing was sent, so nothing was enforced.
        let provider = MockProvider::empty()
            .with_chat_capability(
                ChatCapability::StructuredOutput,
                CapabilitySupport::Unsupported,
            )
            .with_chat_capability(ChatCapability::ToolCalls, CapabilitySupport::Unsupported);
        let failure = failure_of(provider.extract::<Person>(&ChatRequest::new("m")).await);
        assert_eq!(failure.metadata().passes, 0);
        assert_eq!(
            failure.metadata().enforcement,
            SchemaEnforcement::NotEnforced
        );
        assert!(failure.metadata().degradations.is_empty());
    }

    fn failure_of<T: fmt::Debug>(result: Result<T>) -> ExtractFailure {
        failure(result.unwrap_err())
    }

    #[tokio::test]
    async fn streamed_extraction_reports_the_streamed_degradations() {
        let degraded = |text: &str| {
            ChatResponseBuilder::new()
                .text(text)
                .warning(strict_dropped(RequestField::ResponseFormat))
                .build()
        };
        let request = request().with_option(ExtractionMode::Native);

        let provider =
            MockStreamingProvider::from_responses([degraded(r#"{"name":"Alice","age":31}"#)]);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let extracted = stream.finish().unwrap();
        assert_eq!(
            extracted.metadata.enforcement,
            SchemaEnforcement::NotEnforced
        );
        assert_eq!(
            extracted.metadata.degradations,
            [strict_dropped(RequestField::ResponseFormat)]
        );

        let provider = MockStreamingProvider::from_responses([degraded("nope")]);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let failure = failure(stream.finish().unwrap_err());
        assert_eq!(
            failure.metadata().enforcement,
            SchemaEnforcement::NotEnforced
        );
        assert_eq!(
            failure.metadata().degradations,
            [strict_dropped(RequestField::ResponseFormat)]
        );

        let provider = MockStreamingProvider::with_text(r#"{"name":"Alice","age":31}"#);
        let mut stream = provider.extract_stream::<Person>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let extracted = stream.finish().unwrap();
        assert_eq!(extracted.metadata.enforcement, SchemaEnforcement::Exact);
        assert!(extracted.metadata.degradations.is_empty());
    }

    #[test]
    fn parse_normalizes_strict_nulls_and_keeps_the_raw_output() {
        let response = ChatResponseBuilder::new().text(STRICT_NULLS).build();
        let extracted = Extractor::new()
            .parse::<Profile>(response, ExtractionMode::Native)
            .unwrap();
        assert_eq!(extracted.value, alice_profile());
        assert_eq!(
            extracted.metadata.enforcement,
            SchemaEnforcement::NotEnforced
        );

        let tool = ChatResponseBuilder::new()
            .tool_call(
                "c",
                EXTRACTION_TOOL,
                serde_json::from_str::<serde_json::Value>(STRICT_NULLS).unwrap(),
            )
            .build();
        let extracted = Extractor::new()
            .parse::<Profile>(tool, ExtractionMode::ForcedTool)
            .unwrap();
        assert_eq!(extracted.value, alice_profile());

        // `name` is required: its null is not a stand-in, and the raw output
        // is the text the model sent, not the normalized value.
        let raw = r#"{"name":null,"tags":null}"#;
        let failure = failure_of(Extractor::new().parse::<Profile>(
            ChatResponseBuilder::new().text(raw).build(),
            ExtractionMode::Native,
        ));
        assert_eq!(failure.error().raw_output(), Some(raw));
    }

    // --- Undeclared keys ---

    /// A response the adapter reported approximating: its output is checked
    /// for undeclared keys.
    fn approximate_text(text: &str) -> ChatResponse {
        ChatResponseBuilder::new()
            .text(text)
            .warning(approximated(RequestField::ResponseFormat))
            .usage(10, 5)
            .build()
    }

    #[derive(Debug, PartialEq, serde::Deserialize, schemars::JsonSchema)]
    struct Contact {
        email: Option<String>,
        #[serde(default)]
        phone: Option<String>,
    }

    #[derive(Debug, PartialEq, serde::Deserialize, schemars::JsonSchema)]
    struct Customer {
        name: String,
        contact: Option<Contact>,
    }

    #[tokio::test]
    async fn a_misnamed_key_is_rejected_on_a_pass_that_is_not_exact() {
        let provider = MockProvider::with_response(approximate_text(
            r#"{"name": "Ada", "contact": {"e_mail": "ada@example.com"}}"#,
        ));
        let request = request().with_option(ExtractionMode::Native);
        let failure = failure(provider.extract::<Customer>(&request).await.unwrap_err());
        let ExtractError::Parse {
            message, path, raw, ..
        } = failure.error()
        else {
            panic!("a parse failure expected: {failure:?}");
        };
        assert_eq!(path.as_deref(), Some("contact.e_mail"));
        assert_eq!(
            message,
            "unknown field `e_mail`, expected one of `email`, `phone`"
        );
        assert!(raw.contains("e_mail"));
        assert_eq!(failure.metadata().undeclared_key_rejections, 1);
        assert_eq!(
            failure.metadata().enforcement,
            SchemaEnforcement::Approximate
        );
    }

    #[tokio::test]
    async fn an_undeclared_key_is_retried_and_counted() {
        let provider = MockProvider::build(|b| {
            b.response(approximate_text(r#"{"name": "Ada", "e_mail": "x"}"#))
                .response(approximate_text(r#"{"name": "Ada", "contact": null}"#))
        });
        let request = request().with_option(ExtractionMode::Native);
        let extracted = Extractor::new()
            .retries(1)
            .extract::<_, Customer>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value.name, "Ada");
        assert_eq!(extracted.metadata.passes, 2);
        assert_eq!(extracted.metadata.undeclared_key_rejections, 1);
        let feedback = retry_feedback(&provider);
        assert!(
            feedback.contains("it did not match the schema at `e_mail`: unknown field `e_mail`"),
            "{feedback}"
        );
    }

    #[tokio::test]
    async fn a_key_nested_at_the_wrong_level_is_rejected() {
        let provider = MockProvider::with_response(approximate_text(
            r#"{"name": "Ada", "email": "ada@example.com"}"#,
        ));
        let request = request().with_option(ExtractionMode::Native);
        let failure = failure(provider.extract::<Customer>(&request).await.unwrap_err());
        assert!(
            matches!(failure.error(), ExtractError::Parse { path: Some(path), .. } if path == "email"),
            "{failure:?}"
        );
    }

    #[derive(Debug, PartialEq, serde::Deserialize, schemars::JsonSchema)]
    #[serde(untagged)]
    enum Reach {
        Email { email: Option<String> },
        Phone { phone: Option<String> },
    }

    #[derive(Debug, PartialEq, serde::Deserialize, schemars::JsonSchema)]
    struct Lead {
        reach: Reach,
    }

    /// serde alone would read `{"e_mail": ..}` as `Email { email: None }`.
    #[tokio::test]
    async fn untagged_enums_with_optional_branches_are_checked_against_every_branch() {
        let request = request().with_option(ExtractionMode::Native);
        let provider =
            MockProvider::with_response(approximate_text(r#"{"reach": {"e_mail": "x"}}"#));
        let failure = failure(provider.extract::<Lead>(&request).await.unwrap_err());
        assert!(
            matches!(failure.error(), ExtractError::Parse { path: Some(path), .. } if path == "reach.e_mail"),
            "{failure:?}"
        );
        let provider =
            MockProvider::with_response(approximate_text(r#"{"reach": {"phone": "1"}}"#));
        // A key some branch declares passes. Which branch serde picks is
        // serde's: the first one that deserializes, here `Email`, which
        // ignores `phone` (the documented limit of the union rule).
        let extracted = provider.extract::<Lead>(&request).await.unwrap();
        assert_eq!(extracted.value.reach, Reach::Email { email: None });
    }

    #[derive(Debug, serde::Deserialize, schemars::JsonSchema)]
    struct Tagged {
        labels: std::collections::BTreeMap<String, String>,
        #[serde(flatten)]
        extra: std::collections::BTreeMap<String, serde_json::Value>,
    }

    #[tokio::test]
    async fn maps_and_flattened_maps_take_any_key() {
        let provider = MockProvider::with_response(approximate_text(
            r#"{"labels": {"any": "x"}, "unlisted": 1}"#,
        ));
        let request = request().with_option(ExtractionMode::Native);
        let extracted = provider.extract::<Tagged>(&request).await.unwrap();
        assert_eq!(extracted.value.labels["any"], "x");
        assert_eq!(extracted.value.extra["unlisted"], 1);
        assert_eq!(extracted.metadata.undeclared_key_rejections, 0);
    }

    #[tokio::test]
    async fn undeclared_keys_can_be_allowed_and_exact_passes_are_not_checked() {
        let output = r#"{"name": "Ada", "e_mail": "x"}"#;
        let request = request().with_option(ExtractionMode::Native);
        let provider = MockProvider::with_response(approximate_text(output));
        let extracted = Extractor::new()
            .allow_undeclared_keys(true)
            .extract::<_, Customer>(&provider, &request)
            .await
            .unwrap();
        assert_eq!(extracted.value.name, "Ada");

        // No degradation: the provider constrained decoding to the schema.
        let provider = MockProvider::with_text(output);
        let extracted = provider.extract::<Customer>(&request).await.unwrap();
        assert_eq!(extracted.metadata.enforcement, SchemaEnforcement::Exact);
    }

    #[test]
    fn parse_rejects_undeclared_keys_unless_allowed() {
        let response = || {
            ChatResponse::new(vec![crate::ContentBlock::text(
                r#"{"name": "Ada", "x": 1}"#,
            )])
        };
        let err = Extractor::new()
            .parse::<Customer>(response(), ExtractionMode::Native)
            .unwrap_err();
        let failure = failure(err);
        assert_eq!(failure.metadata().undeclared_key_rejections, 1);
        assert!(
            Extractor::new()
                .allow_undeclared_keys(true)
                .parse::<Customer>(response(), ExtractionMode::Native)
                .is_ok()
        );
    }

    #[tokio::test]
    async fn streamed_extraction_rejects_undeclared_keys() {
        let provider = MockStreamingProvider::from_responses([approximate_text(
            r#"{"name": "Ada", "e_mail": "x"}"#,
        )]);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = provider.extract_stream::<Customer>(&request).await.unwrap();
        while stream.next().await.is_some() {}
        let failure = failure(stream.finish().unwrap_err());
        assert_eq!(failure.metadata().undeclared_key_rejections, 1);
    }

    // --- Enforcement in the caller's terms, the floor and the plan ---

    /// A provider whose validation is a dry run that reports `warnings`
    /// (`Encoding` coverage), or a plain one (`Capabilities`).
    struct Validating {
        inner: MockProvider,
        warnings: Vec<Warning>,
        coverage: crate::ValidationCoverage,
    }

    impl crate::ProviderIdentity for Validating {
        fn provider_name(&self) -> &str {
            "mock"
        }

        fn reads_namespace(&self, namespace: &str) -> bool {
            namespace == "mock"
        }
    }

    impl ChatProvider for Validating {
        type Stream = <MockProvider as ChatProvider>::Stream;

        async fn chat(&self, request: &ChatRequest) -> Result<ChatResponse> {
            self.inner.chat(request).await
        }

        async fn chat_stream(&self, request: &ChatRequest) -> Result<Self::Stream> {
            self.inner.chat_stream(request).await
        }

        fn validate_chat_request(&self, _: &ChatRequest) -> Result<crate::ChatValidation> {
            let mut validation = crate::Validation::new(self.coverage);
            for warning in &self.warnings {
                validation = validation.with_warning(warning.clone());
            }
            Ok(validation)
        }

        fn chat_capability(&self, model: &str, capability: ChatCapability) -> CapabilitySupport {
            self.inner.chat_capability(model, capability)
        }
    }

    fn not_constrained_tool() -> Warning {
        Warning::new(
            WarningKind::Unsupported,
            Subject::Field {
                field: RequestField::Tools,
            },
            "tool `submit_structured_output` is not strict-representable: the subschema at \
             `/properties/contact` is a map; it was sent without `strict`",
        )
    }

    #[tokio::test]
    async fn degradations_name_the_callers_type() {
        let provider = MockProvider::with_response(
            ChatResponseBuilder::new()
                .tool_call(
                    "call_1",
                    EXTRACTION_TOOL,
                    json!({"name": "Ada", "contact": null}),
                )
                .warning(not_constrained_tool())
                .build(),
        );
        let request = request().with_option(ExtractionMode::ForcedTool);
        let extracted = provider.extract::<Customer>(&request).await.unwrap();
        assert_eq!(
            extracted.metadata.degradations[0].message,
            "the schema of `Customer` is not strict-representable: the subschema at \
             `Customer.contact` is a map; it was sent without `strict`"
        );
        // The response keeps the adapter's own words.
        assert_eq!(extracted.response.warnings[0], not_constrained_tool());
    }

    #[cfg(feature = "warning-events")]
    #[test]
    fn enforcement_is_a_warning_event_once_per_type_and_model() {
        let (_, events) = crate::warning::capture::captured(async {
            for _ in 0..2 {
                let provider = MockProvider::with_response(approximate_text(
                    r#"{"name": "Ada", "contact": null}"#,
                ))
                .with_provider_name("enforcement-events");
                let request = ChatRequest::new("enforcement-model")
                    .user("Ada")
                    .with_option(ExtractionMode::Native);
                provider.extract::<Customer>(&request).await.unwrap();
            }
        });
        let ours: Vec<_> = events
            .iter()
            .filter(|e| e.code.as_deref() == Some("extraction.approximate.Customer"))
            .collect();
        assert_eq!(ours.len(), 2, "{events:?}");
        assert_eq!(ours[0].level, tracing::Level::WARN);
        assert_eq!(ours[1].level, tracing::Level::DEBUG);
        assert!(
            ours[0].message.ends_with(
                "; `Customer` is extracted from `enforcement-model` approximately (`native` mode)"
            ),
            "{}",
            ours[0].message
        );
    }

    #[tokio::test]
    async fn a_floor_fails_before_sending_under_encoding_coverage() {
        let provider = Validating {
            inner: MockProvider::with_text(r#"{"name": "Ada", "contact": null}"#),
            warnings: vec![not_constrained_tool()],
            coverage: crate::ValidationCoverage::Encoding,
        };
        let request = request().with_option(ExtractionMode::ForcedTool);
        let err = Extractor::new()
            .min_enforcement(SchemaEnforcement::Approximate)
            .extract::<_, Customer>(&provider, &request)
            .await
            .unwrap_err();
        assert_eq!(provider.inner.call_count(), 0);
        let failure = failure(err);
        let ExtractError::EnforcementBelow {
            required,
            achieved,
            reason,
        } = failure.error()
        else {
            panic!("a floor failure expected: {failure:?}");
        };
        assert_eq!(
            (*required, *achieved),
            (
                SchemaEnforcement::Approximate,
                SchemaEnforcement::NotEnforced
            )
        );
        assert!(reason.contains("`Customer.contact` is a map"), "{reason}");
        assert!(failure.response().is_none());
        assert_eq!(failure.metadata().passes, 0);

        // A floor the plan meets lets the call through.
        let extracted = Extractor::new()
            .min_enforcement(SchemaEnforcement::NotEnforced)
            .extract::<_, Customer>(&provider, &request)
            .await;
        assert!(extracted.is_err(), "the mock answers text, not a tool call");
        assert_eq!(provider.inner.call_count(), 1);
    }

    #[tokio::test]
    async fn a_floor_is_checked_after_the_pass_under_capabilities_coverage() {
        let provider = Validating {
            inner: MockProvider::with_response(approximate_text(
                r#"{"name": "Ada", "contact": null}"#,
            )),
            warnings: Vec::new(),
            coverage: crate::ValidationCoverage::Capabilities,
        };
        let request = request().with_option(ExtractionMode::Native);
        let err = Extractor::new()
            .min_enforcement(SchemaEnforcement::Exact)
            .extract::<_, Customer>(&provider, &request)
            .await
            .unwrap_err();
        assert_eq!(provider.inner.call_count(), 1);
        let failure = failure(err);
        assert!(
            matches!(
                failure.error(),
                ExtractError::EnforcementBelow {
                    achieved: SchemaEnforcement::Approximate,
                    ..
                }
            ),
            "{failure:?}"
        );
        // A paid call: the response and its usage are kept.
        assert!(failure.response().is_some());
        assert_eq!(
            failure
                .metadata()
                .usage
                .as_ref()
                .and_then(|u| u.output_tokens),
            Some(5)
        );
        assert!(!Error::from(failure).is_retryable());
    }

    #[tokio::test]
    async fn a_floor_applies_to_streams() {
        let provider = MockStreamingProvider::from_responses([approximate_text(
            r#"{"name": "Ada", "contact": null}"#,
        )]);
        let request = request().with_option(ExtractionMode::Native);
        let mut stream = Extractor::new()
            .min_enforcement(SchemaEnforcement::Exact)
            .extract_stream::<_, Customer>(&provider, &request)
            .await
            .unwrap();
        while stream.next().await.is_some() {}
        let failure = failure(stream.finish().unwrap_err());
        assert!(matches!(
            failure.error(),
            ExtractError::EnforcementBelow { .. }
        ));
    }

    #[test]
    fn enforcement_levels_are_ordered() {
        use SchemaEnforcement::{Approximate, Exact, NotEnforced};
        assert!(Exact.satisfies(Exact) && Exact.satisfies(NotEnforced));
        assert!(Approximate.satisfies(Approximate) && !Approximate.satisfies(Exact));
        assert!(NotEnforced.satisfies(NotEnforced) && !NotEnforced.satisfies(Approximate));
        assert_eq!(NotEnforced.to_string(), "not_enforced");
    }

    #[test]
    fn a_plan_reports_mode_enforcement_and_coverage_without_sending() {
        let provider = Validating {
            inner: MockProvider::with_text("unused")
                .with_supported_chat_capabilities(NATIVE_AND_TOOLS),
            warnings: vec![approximated(RequestField::ResponseFormat)],
            coverage: crate::ValidationCoverage::Encoding,
        };
        let plan = Extractor::new()
            .plan::<Customer>(&provider, &request())
            .unwrap();
        assert_eq!(plan.mode, ExtractionMode::Native);
        assert_eq!(plan.enforcement, SchemaEnforcement::Approximate);
        assert_eq!(plan.degradations.len(), 1);
        assert_eq!(plan.coverage, crate::ValidationCoverage::Encoding);
        assert_eq!(plan.auto_fallback, None);
        assert_eq!(provider.inner.call_count(), 0);

        // An `Auto` plan follows a refused response format.
        let provider = MockProvider::with_error(refused(RequestField::ResponseFormat))
            .with_supported_chat_capabilities(NATIVE_AND_TOOLS);
        let plan = Extractor::new().plan::<Customer>(&provider, &request());
        // The default validation checks capabilities only: nothing refuses.
        let plan = plan.unwrap();
        assert_eq!(plan.mode, ExtractionMode::Native);
        assert_eq!(plan.coverage, crate::ValidationCoverage::Capabilities);
    }

    #[test]
    fn metadata_without_enforcement_fields_deserializes() {
        let metadata: ExtractionMetadata =
            serde_json::from_value(json!({"mode": "native", "passes": 1, "repaired": false}))
                .unwrap();
        assert_eq!(metadata.enforcement, SchemaEnforcement::NotEnforced);
        assert!(metadata.degradations.is_empty());

        let mut degraded = ExtractionMetadata::new(ExtractionMode::ForcedTool);
        degraded.record_pass(
            &ChatResponseBuilder::new()
                .warning(strict_dropped(RequestField::Tools))
                .build(),
            "Person",
        );
        let value = serde_json::to_value(&degraded).unwrap();
        assert_eq!(value["enforcement"], "not_enforced");
        assert_eq!(value["degradations"][0]["subject"]["field"], "tools");
        assert_eq!(
            serde_json::from_value::<ExtractionMetadata>(value).unwrap(),
            degraded
        );
    }
}