litertlm-sys 0.19.1

Raw FFI bindings to LiteRT-LM C API
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
// Copyright 2025 The ODML Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef THIRD_PARTY_ODML_LITERT_LM_C_ENGINE_H_
#define THIRD_PARTY_ODML_LITERT_LM_C_ENGINE_H_

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#include "api_export.h"
#include "error_reporter.h"

// Version of the LiteRT LM C API declared by these headers.
//
// The C API is versioned independently of the LiteRT LM release, following
// semantic versioning:
//   - MAJOR is incremented for ABI-breaking changes, such as removing a
//     function or changing an existing function signature or struct layout.
//   - MINOR is incremented when functions or types are added in a
//     backward-compatible way.
//   - PATCH is incremented for backward-compatible fixes.
//
// These are plain integer literals, so they can be used in preprocessor
// conditionals, e.g. `#if LITERT_LM_C_API_VERSION_MAJOR >= 2`. They describe
// the headers the caller is compiled against; use `litert_lm_get_api_version`
// to query the library that is actually loaded.
//
// Maintainers: keep these in sync with C_API_VERSION in version.bzl;
// engine_test fails if they differ.
//
// Added in version 1.0.0.
// LINT.IfChange(c_api_version)
#define LITERT_LM_C_API_VERSION_MAJOR 1
#define LITERT_LM_C_API_VERSION_MINOR 0
#define LITERT_LM_C_API_VERSION_PATCH 0
// LINT.ThenChange(../version.bzl:c_api_version)

#ifdef __cplusplus
extern "C" {
#endif

// Returns the version of the *library*, which may differ from the
// LITERT_LM_C_API_VERSION_* macros the caller was compiled against.
//
// The two can differ whenever the library is loaded or replaced independently
// of the application, for example when it is loaded at run time with
// dlopen/LoadLibrary. Compare them before calling any other function:
//
//   int major, minor, patch;
//   LiteRtLmStatusCode status =
//       litert_lm_get_api_version(&major, &minor, &patch);
//   if (status != kLiteRtLmStatusOk ||
//       major != LITERT_LM_C_API_VERSION_MAJOR ||
//       minor < LITERT_LM_C_API_VERSION_MINOR) {
//     // Refuse to proceed: a different major version is ABI-incompatible, and
//     // an older minor version may lack functions declared in these headers.
//   }
//
// The name and signature of this function will not change in any future
// version, including major versions. Libraries older than version 1.0.0 do not
// export it, so a caller that loads the library dynamically and cannot resolve
// this symbol should treat the library as incompatible.
//
// @param out_major On success, receives the major version of the library; not
//   written on failure.
// @param out_minor On success, receives the minor version of the library; not
//   written on failure.
// @param out_patch On success, receives the patch version of the library; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `out_major`, `out_minor`, or `out_patch` is NULL.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_get_api_version(int* out_major, int* out_minor,
                                             int* out_patch);

// Threading model
// ---------------
// NOTE: This section describes the current behavior of the implementation so
// that callers can use the API safely. It is not a commitment to keep these
// constraints; they may be changed in a future version.
//
// * Handles are not thread-safe: calls that use the same handle MUST NOT
//   overlap. When single-threaded execution is enabled (see
//   `litert_lm_engine_settings_set_single_threaded_execution`), this extends to
//   an engine and every session and conversation created from it.
// * Stream callbacks can run on library-owned threads that are shared by all
//   streams of an engine, so a callback should return quickly. When
//   single-threaded execution is enabled, queued work, and with it the
//   callbacks, runs only while a caller drives it, for example inside
//   `litert_lm_conversation_wait_until_done`.
// * A callback MUST NOT call LiteRT LM functions on the engine that runs the
//   stream, or on any session or conversation created from it, with one
//   exception: while handling a chunk that is not final, it may stop the stream
//   with `litert_lm_session_cancel_process` or
//   `litert_lm_conversation_cancel_process`. Hand any other such work off to
//   another thread.
// * After `litert_lm_session_cancel_process` or
//   `litert_lm_conversation_cancel_process`, the session or conversation MUST
//   NOT be reused; only delete it.
// * The last error is thread-local (see error_reporter.h): read it on the
//   thread that received the failing status code. Errors that occur after a
//   stream has started are reported through its chunks instead (see
//   `litert_lm_stream_chunk_get_error`).

// Opaque pointer for the LiteRT LM Engine.
//
// Added in version 0.1.0.
typedef struct LiteRtLmEngine LiteRtLmEngine;

// Opaque pointer for the LiteRT LM Session.
//
// Added in version 0.1.0.
typedef struct LiteRtLmSession LiteRtLmSession;

// Opaque pointer for the LiteRT LM Responses.
//
// Added in version 0.1.0.
typedef struct LiteRtLmResponses LiteRtLmResponses;

// Opaque pointer for the LiteRT LM Engine Settings.
//
// Added in version 0.1.0.
typedef struct LiteRtLmEngineSettings LiteRtLmEngineSettings;

// Opaque pointer for the LiteRT LM Benchmark Info.
//
// Added in version 0.1.0.
typedef struct LiteRtLmBenchmarkInfo LiteRtLmBenchmarkInfo;

// Opaque pointer for the LiteRT LM Repetition Penalty Config.
//
// Added in version 0.1.0.
typedef struct LiteRtLmRepetitionPenaltyConfig LiteRtLmRepetitionPenaltyConfig;

// Opaque pointer for the LiteRT LM Speculative Decoding Config.
// Use `litert_lm_speculative_decoding_config_delete` to free memory.
//
// Added in version 1.0.0.
typedef struct LiteRtLmSpeculativeDecodingConfig
    LiteRtLmSpeculativeDecodingConfig;

// Opaque pointer for the LiteRT LM No Repeat Ngram Config.
//
// Added in version 0.1.0.
typedef struct LiteRtLmNoRepeatNgramConfig LiteRtLmNoRepeatNgramConfig;

// Opaque pointer for the LiteRT LM Suppress Tokens Config.
//
// Added in version 0.1.0.
typedef struct LiteRtLmSuppressTokensConfig LiteRtLmSuppressTokensConfig;

// Opaque pointer for a detokenize result.
// Use `litert_lm_detokenize_result_delete` to free memory.
//
// Added in version 0.1.0.
typedef struct LiteRtLmDetokenizeResult LiteRtLmDetokenizeResult;

// Opaque pointer for a tokenize result.
// Use `litert_lm_tokenize_result_delete` to free memory.
//
// Added in version 0.1.0.
typedef struct LiteRtLmTokenizeResult LiteRtLmTokenizeResult;

// Represents the type of a TokenUnion.
//
// Added in version 0.1.0.
typedef enum {
  kLiteRtLmTokenUnionTypeString = 0,
  kLiteRtLmTokenUnionTypeIds = 1,
} LiteRtLmTokenUnionType;

// Opaque pointer for LiteRT LM Token Union.
// Represents a single start or stop token, which could be either a string or a
// sequence of token ids.
// Use `litert_lm_token_union_delete` to free memory.
//
// Added in version 0.1.0.
typedef struct LiteRtLmTokenUnion LiteRtLmTokenUnion;

// Opaque pointer for LiteRT LM Token Unions.
// Represents a collection of TokenUnion, typically used for model stop
// conditions.
// Use `litert_lm_token_unions_delete` to free memory.
//
// Added in version 0.1.0.
typedef struct LiteRtLmTokenUnions LiteRtLmTokenUnions;

// Opaque pointer for LiteRT LM Input Data.
// Use `litert_lm_input_data_delete` to free memory.
//
// Added in version 0.1.0.
typedef struct LiteRtLmInputData LiteRtLmInputData;

// Opaque pointer for LiteRT LM Session Config.
//
// Added in version 0.1.0.
typedef struct LiteRtLmSessionConfig LiteRtLmSessionConfig;

// Represents the type of sampler.
//
// Added in version 0.1.0.
typedef enum {
  // Default fallback/unspecified.
  kLiteRtLmSamplerTypeUnspecified = 0,
  // Probabilistically pick among the top k tokens.
  kLiteRtLmSamplerTypeTopK = 1,
  // Probabilistically pick among the tokens such that the sum is greater
  // than or equal to p tokens after first performing top-k sampling.
  kLiteRtLmSamplerTypeTopP = 2,
  // Pick the token with maximum logit (i.e., argmax).
  kLiteRtLmSamplerTypeGreedy = 3,
} LiteRtLmSamplerType;

// Represents how speculative decoding (e.g. MTP) is used during decode.
//
// The modes match the values of the `--speculative_decoding` flag of the
// LiteRT-LM CLI (`off`, `on` and `adaptive`).
//
// Added in version 1.0.0.
typedef enum {
  // Never speculate.
  kLiteRtLmSpeculativeDecodingModeOff = 0,
  // Speculate on every decode step.
  kLiteRtLmSpeculativeDecodingModeOn = 1,
  // Speculate only while it is measured to be faster than plain decoding.
  // Backends that do not support adaptive speculation speculate on every
  // decode step, like `kLiteRtLmSpeculativeDecodingModeOn`.
  kLiteRtLmSpeculativeDecodingModeAdaptive = 2,
} LiteRtLmSpeculativeDecodingMode;

// Opaque pointer for LiteRT LM Sampler Parameters.
// Use `litert_lm_sampler_params_delete` to free memory.
//
// Added in version 0.1.0.
typedef struct LiteRtLmSamplerParams LiteRtLmSamplerParams;

// Creates LiteRT LM Sampler Parameters with a specific sampler type.
//
// @param type The sampler type to use.
// @param out_params On success, receives the created parameters, owned by the
//   caller; release with `litert_lm_sampler_params_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_params` is NULL or `type` is not a recognized LiteRtLmSamplerType.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the parameters are returned
// through out_params.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_sampler_params_create(
    LiteRtLmSamplerType type, LiteRtLmSamplerParams** out_params);

// Destroys LiteRT LM Sampler Parameters.
//
// @param params The parameters to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_sampler_params_delete(LiteRtLmSamplerParams* params);

// Sets the top-k value.
//
// @param params The sampler parameters to modify.
// @param top_k The top-k value.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `params` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_sampler_params_set_top_k(
    LiteRtLmSamplerParams* params, int32_t top_k);

// Sets the top-p value.
//
// @param params The sampler parameters to modify.
// @param top_p The top-p value.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `params` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_sampler_params_set_top_p(
    LiteRtLmSamplerParams* params, float top_p);

// Sets the temperature.
//
// @param params The sampler parameters to modify.
// @param temperature The sampling temperature.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `params` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_sampler_params_set_temperature(
    LiteRtLmSamplerParams* params, float temperature);

// Sets the seed.
//
// @param params The sampler parameters to modify.
// @param seed The random seed.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `params` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_sampler_params_set_seed(
    LiteRtLmSamplerParams* params, int32_t seed);

// Creates a LiteRT LM Session Config.
//
// @param out_config On success, receives the created config, owned by the
//   caller; release with `litert_lm_session_config_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the config is returned
// through out_config.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_create(
    LiteRtLmSessionConfig** out_config);

// Sets the maximum number of output tokens per decode step for this session.
// For thinking models, both thinking (reasoning) tokens and the final response
// tokens count towards this limit.
// @param config The config to modify.
// @param max_output_tokens The maximum number of tokens to generate (including
// thinking tokens).
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_set_max_output_tokens(
    LiteRtLmSessionConfig* config, int max_output_tokens);

// Sets whether to apply prompt template for this session.
// @param config The config to modify.
// @param apply_prompt_template Whether to apply prompt template.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_set_apply_prompt_template(
    LiteRtLmSessionConfig* config, bool apply_prompt_template);

// Sets whether to enable speculative decoding for this session.
//
// Deprecated: use `litert_lm_session_config_set_speculative_decoding_config`
// instead. Passing true is equivalent to a config whose mode is
// `kLiteRtLmSpeculativeDecodingModeOn` and passing false to
// `kLiteRtLmSpeculativeDecodingModeOff`.
//
// @param config The config to modify.
// @param enable_speculative_decoding Whether to enable speculative decoding.
// If set to true, speculative decoding is enabled for this session. If the
// engine was not initialized with speculative decoding enabled, setting this
// flag to true causes the executor to perform lazy loading of the
// MTP drafter on the first session request. If set to false, speculative
// decoding is explicitly disabled for this session even if the engine was
// initialized with speculative decoding enabled. If this function is not called
// on the config, the session inherits the engine's speculative decoding setting
// by default. This function and
// `litert_lm_session_config_set_speculative_decoding_config` write the same
// mode, so the last call wins.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.2.0.
// Changed in version 1.0.0: returns a status code instead of void.
// Deprecated in version 1.0.0.
LITERT_LM_C_API_EXPORT
LITERT_LM_C_API_DEPRECATED(
    "Use litert_lm_session_config_set_speculative_decoding_config instead.")
LiteRtLmStatusCode litert_lm_session_config_set_enable_speculative_decoding(
    LiteRtLmSessionConfig* config, bool enable_speculative_decoding);

// Creates a LiteRT LM Speculative Decoding Config with every field unset.
//
// A speculative decoding config is set on the engine settings, as the default
// of every session
// (`litert_lm_engine_settings_set_speculative_decoding_config`), and on a
// session config, as an override
// (`litert_lm_session_config_set_speculative_decoding_config`). A field left
// unset on the session inherits the engine's value, and a field left unset on
// the engine uses the runtime default.
//
// @param out_config On success, receives the created config, owned by the
//   caller; release with `litert_lm_speculative_decoding_config_delete`. Set to
//   NULL on failure.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `out_config` is NULL.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_speculative_decoding_config_create(
    LiteRtLmSpeculativeDecodingConfig** out_config);

// Destroys a LiteRT LM Speculative Decoding Config.
// @param config The config to destroy.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
void litert_lm_speculative_decoding_config_delete(
    LiteRtLmSpeculativeDecodingConfig* config);

// Sets the speculative decoding mode. If unset, the session inherits the
// engine's mode, and the engine's mode defaults to
// `kLiteRtLmSpeculativeDecodingModeOff`.
//
// On the engine, `kLiteRtLmSpeculativeDecodingModeOn` and
// `kLiteRtLmSpeculativeDecodingModeAdaptive` load the speculative decoding
// drafter (e.g. MTP) when the engine is created. On a session of an engine
// whose mode is `kLiteRtLmSpeculativeDecodingModeOff`, they load the drafter
// lazily on the first request that needs it (CPU and GPU only; the NPU backend
// decodes without speculation).
//
// @param config The config to modify.
// @param mode The speculative decoding mode.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL or `mode` is invalid. An invalid `mode` leaves the config
//   unchanged.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_speculative_decoding_config_set_mode(
    LiteRtLmSpeculativeDecodingConfig* config,
    LiteRtLmSpeculativeDecodingMode mode);

// Sets the speculative decoding config of this session, which overrides the
// engine's config (see
// `litert_lm_engine_settings_set_speculative_decoding_config`). Fields left
// unset in `speculative_decoding_config` inherit the engine's values. The
// config is copied, so the caller keeps ownership of
// `speculative_decoding_config`. Replaces any config set before, including the
// mode set by the deprecated
// `litert_lm_session_config_set_enable_speculative_decoding`.
// @param config The config to modify.
// @param speculative_decoding_config The speculative decoding config.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` or `speculative_decoding_config` is NULL.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_set_speculative_decoding_config(
    LiteRtLmSessionConfig* config,
    const LiteRtLmSpeculativeDecodingConfig* speculative_decoding_config);

// Sets the sampler parameters for this session config.
// @param config The config to modify.
// @param sampler_params The sampler parameters to use.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` or `sampler_params` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_set_sampler_params(
    LiteRtLmSessionConfig* config, const LiteRtLmSamplerParams* sampler_params);

// Destroys a LiteRT LM Session Config.
// @param config The config to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_session_config_delete(LiteRtLmSessionConfig* config);

// Sets the path to the LoRA weights file.
// @param config The config to modify.
// @param lora_path The path to the text LoRA weights file.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `config`
//   or `lora_path` is NULL or `lora_path` is empty; otherwise the code of the
//   error encountered while opening the file.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns LiteRtLmStatusCode and failures return a
// canonical LiteRtLmStatusCode instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_set_lora_path(
    LiteRtLmSessionConfig* config, const char* lora_path);

// Sets the path to the Audio LoRA weights file.
// @param config The config to modify.
// @param audio_lora_path The path to the audio LoRA weights file.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `config`
//   or `audio_lora_path` is NULL or `audio_lora_path` is empty; otherwise the
//   code of the error encountered while opening the file.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns LiteRtLmStatusCode and failures return a
// canonical LiteRtLmStatusCode instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_config_set_audio_lora_path(
    LiteRtLmSessionConfig* config, const char* audio_lora_path);

// Creates a LiteRT LM Repetition Penalty Config with default values
// (`repetition_penalty` = 1.0f, `presence_penalty` = 0.0f,
// `frequency_penalty` = 0.0f, `window_size` = 0, which means all history with
// no penalties active).
//
// When multiple penalties are configured and active, the order of application
// to output logits during decoding is:
// 1. Multiplicative penalty (`repetition_penalty`)
// 2. Subtractive penalties (`presence_penalty` and `frequency_penalty`)
//
// @param out_config On success, receives the created config, owned by the
//   caller; release with `litert_lm_repetition_penalty_config_delete`. Set to
//   NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the config is returned
// through out_config.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_repetition_penalty_config_create(
    LiteRtLmRepetitionPenaltyConfig** out_config);

// Destroys a LiteRT LM Repetition Penalty Config.
// @param config The config to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_repetition_penalty_config_delete(
    LiteRtLmRepetitionPenaltyConfig* config);

// Sets the multiplicative repetition penalty for the repetition penalty config.
// @param config The config to modify.
// @param repetition_penalty A multiplicative penalty applied to a token's logit
// if that token has appeared at least once inside the generated window history
// (e.g., 1.0 = no penalty, 1.2 = moderate penalty). Positive logits are divided
// by this parameter, and negative logits are multiplied (HuggingFace style).
// The parameter must be >= 1.0f; values less than 1.0f are automatically
// clamped to 1.0f during execution.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_repetition_penalty_config_set_repetition_penalty(
    LiteRtLmRepetitionPenaltyConfig* config, float repetition_penalty);

// Sets the subtractive presence penalty for the repetition penalty config.
// @param config The config to modify.
// @param presence_penalty A scalar subtracted from a token's logit if that
// token has appeared at least once inside the generated window history.
// Positive values discourage repetition, while negative values reward repeating
// tokens (OpenAI style). Defaults to 0.0f.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_repetition_penalty_config_set_presence_penalty(
    LiteRtLmRepetitionPenaltyConfig* config, float presence_penalty);

// Sets the subtractive frequency penalty for the repetition penalty config.
// @param config The config to modify.
// @param frequency_penalty A scalar subtracted from a token's logit, scaled
// linearly by the number of times that token has previously appeared inside the
// generated window history. Positive values discourage repetition, while
// negative values reward repeating tokens (OpenAI style). Defaults to 0.0f.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_repetition_penalty_config_set_frequency_penalty(
    LiteRtLmRepetitionPenaltyConfig* config, float frequency_penalty);

// Sets the window size for the repetition penalty config.
// @param config The config to modify.
// @param window_size The maximum number of recent tokens in generation history
// to consider when computing penalization. Tokens generated prior to this
// window are forgotten. A value of 0 means tracking all infinite generation
// history. Must be >= 0; negative values are clamped to 0 during execution.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_repetition_penalty_config_set_window_size(
    LiteRtLmRepetitionPenaltyConfig* config, int window_size);

// Creates a LiteRT LM No Repeat Ngram Config with default values
// (`no_repeat_ngram_size` = 0, `window_size` = 0, which means no repeat ngram
// banning is disabled).
//
// When `no_repeat_ngram_size` is set greater than 0, any sequence of tokens (an
// ngram of that exact length) generated during decoding or present inside the
// window history can only occur at most once. If generating a candidate token
// would complete a repeating ngram, that candidate token's logit is set to
// -inf.
//
// @param out_config On success, receives the created config, owned by the
//   caller; release with `litert_lm_no_repeat_ngram_config_delete`. Set to
//   NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the config is returned
// through out_config.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_no_repeat_ngram_config_create(
    LiteRtLmNoRepeatNgramConfig** out_config);

// Destroys a LiteRT LM No Repeat Ngram Config.
// @param config The config to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_no_repeat_ngram_config_delete(
    LiteRtLmNoRepeatNgramConfig* config);

// Sets the no repeat ngram size for the no repeat ngram config.
// @param config The config to modify.
// @param no_repeat_ngram_size The size of ngrams (consecutive token sequences)
// that are banned from repeating within the generation history window. If set
// > 0, when generating the next token would complete an already observed
// `no_repeat_ngram_size` sequence, the logit of the candidate token is set to
// -inf. If set <= 0, no repeat ngram banning is disabled. Negative values are
// automatically clamped to 0 during execution.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_no_repeat_ngram_config_set_no_repeat_ngram_size(
    LiteRtLmNoRepeatNgramConfig* config, int no_repeat_ngram_size);

// Sets the window size for the no repeat ngram config.
// @param config The config to modify.
// @param window_size The maximum number of recent tokens in generation history
// to consider when checking for repeating ngrams. Tokens generated prior to
// this window are forgotten. A value of 0 means tracking all infinite
// generation history. Must be >= 0; negative values are clamped to 0. If
// `window_size` is greater than 0 but less than `no_repeat_ngram_size`, it is
// automatically clamped to `no_repeat_ngram_size` so that the ngrams can fit
// and be tracked.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_no_repeat_ngram_config_set_window_size(
    LiteRtLmNoRepeatNgramConfig* config, int window_size);

// Creates a LiteRT LM Suppress Tokens Config with default values (an empty set
// of suppressed tokens, which means token suppression is disabled).
//
// When `suppress_tokens` is configured with one or more token IDs, the logits
// corresponding to those exact token IDs will be set directly to -inf during
// generation. This guarantees that those tokens can never be sampled by the
// model.
//
// @param out_config On success, receives the created config, owned by the
//   caller; release with `litert_lm_suppress_tokens_config_delete`. Set to
//   NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_config` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the config is returned
// through out_config.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_suppress_tokens_config_create(
    LiteRtLmSuppressTokensConfig** out_config);

// Destroys a LiteRT LM Suppress Tokens Config.
// @param config The config to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_suppress_tokens_config_delete(
    LiteRtLmSuppressTokensConfig* config);

// Sets the list of token IDs to suppress for the suppress tokens config.
// @param config The config to modify.
// @param suppress_tokens An array of integer token IDs that should be banned
// from generation. During every decode step, each listed token ID's candidate
// logit will be forced to -inf. If `num_tokens` is 0, any previously set
// suppressed tokens are cleared and token suppression is disabled; in that case
// `suppress_tokens` may be NULL.
// @param num_tokens The number of token IDs in the `suppress_tokens` array.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `config` is NULL, or if `suppress_tokens` is NULL while `num_tokens` is
//   non-zero (the config is left unchanged).
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_suppress_tokens_config_set_suppress_tokens(
    LiteRtLmSuppressTokensConfig* config, const int* suppress_tokens,
    size_t num_tokens);

// Represents the log severity / level.
//
// Added in version 0.1.0.
typedef enum {
  kLiteRtLmLogSeverityVerbose = 0,
  kLiteRtLmLogSeverityDebug = 1,
  kLiteRtLmLogSeverityInfo = 2,
  kLiteRtLmLogSeverityWarning = 3,
  kLiteRtLmLogSeverityError = 4,
  kLiteRtLmLogSeverityFatal = 5,
  kLiteRtLmLogSeveritySilent = 1000,
} LiteRtLmLogSeverity;
// Sets the minimum log level for the LiteRT LM library.
//
// @param level The minimum severity to log.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `level` is not a declared LiteRtLmLogSeverity value.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_set_min_log_level(LiteRtLmLogSeverity level);

// Represents the type of input data.
//
// Added in version 0.1.0.
typedef enum {
  kLiteRtLmInputDataTypeText = 0,
  kLiteRtLmInputDataTypeImage = 1,
  kLiteRtLmInputDataTypeImageEnd = 2,
  kLiteRtLmInputDataTypeAudio = 3,
  kLiteRtLmInputDataTypeAudioEnd = 4,
} LiteRtLmInputDataType;

// Creates a LiteRT LM Input Data.
//
// @param type The type of the input data.
// @param data The data pointer. For kLiteRtLmInputDataTypeText, it's a UTF-8
// string.
//             For image/audio types, it's a pointer to the raw bytes.
//             The data is copied internally.
// @param size The size of the data in bytes.
// @param out_input_data On success, receives the created input data, owned by
//   the caller; release with `litert_lm_input_data_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_input_data` is NULL, `data` is NULL while `size` is non-zero, or
//   `type` is not a declared LiteRtLmInputDataType value.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the input data is returned
// through out_input_data.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_input_data_create(
    LiteRtLmInputDataType type, const void* data, size_t size,
    LiteRtLmInputData** out_input_data);

// Destroys a LiteRT LM Input Data.
//
// @param input_data The input data to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_input_data_delete(LiteRtLmInputData* input_data);

// Creates LiteRT LM Engine Settings.
//
// @param model_path The path to the model file.
// @param backend_str The backend to use (e.g., "cpu", "gpu").
// @param vision_backend_str The vision backend to use, or NULL if not set.
// @param audio_backend_str The audio backend to use, or NULL if not set.
// @param out_settings On success, receives the created settings, owned by the
//   caller; release with `litert_lm_engine_settings_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_settings` or `model_path` is NULL or a backend string is not
//   recognized; otherwise the code of the error encountered while loading the
//   model assets or building the settings.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the settings are returned
// through out_settings.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_create(
    const char* model_path, const char* backend_str,
    const char* vision_backend_str, const char* audio_backend_str,
    LiteRtLmEngineSettings** out_settings);

// Creates LiteRT LM Engine Settings from a raw file descriptor. The engine
// takes ownership of the file descriptor and will close it when done. If
// `out_settings` is NULL or `fd` is negative, ownership is not taken.
//
// @param fd The file descriptor of the model.
// @param backend_str The backend to use (e.g., "cpu", "gpu").
// @param vision_backend_str The vision backend to use, or NULL if not set.
// @param audio_backend_str The audio backend to use, or NULL if not set.
// @param out_settings On success, receives the created settings, owned by the
//   caller; release with `litert_lm_engine_settings_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `out_settings` is NULL, `fd` is negative or a backend string is not
//   recognized; otherwise the code of the error encountered while loading the
//   model assets or building the settings.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the settings are returned
// through out_settings.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_create_from_raw_file_descriptor(
    int fd, const char* backend_str, const char* vision_backend_str,
    const char* audio_backend_str, LiteRtLmEngineSettings** out_settings);

// Destroys LiteRT LM Engine Settings.
//
// @param settings The settings to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_engine_settings_delete(LiteRtLmEngineSettings* settings);

// Sets the maximum number of tokens for the engine.
//
// @param settings The engine settings.
// @param max_num_tokens The maximum number of tokens.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_max_num_tokens(
    LiteRtLmEngineSettings* settings, int max_num_tokens);

// Sets the number of threads for the CPU backend.
//
// @param settings The engine settings.
// @param num_threads The number of threads.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If the main backend is not CPU, this call has no effect
//   and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_num_threads(
    LiteRtLmEngineSettings* settings, int num_threads);

// Sets the number of threads for the audio CPU backend.
//
// @param settings The engine settings.
// @param num_threads The number of threads.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If no audio backend is configured, this call has no
//   effect and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_audio_num_threads(
    LiteRtLmEngineSettings* settings, int num_threads);

// Sets whether the engine should load different sections of the litertlm file
// in parallel. Defaults to true.
//
// @param settings The engine settings.
// @param parallel_file_section_loading Whether to load in parallel.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_parallel_file_section_loading(
    LiteRtLmEngineSettings* settings, bool parallel_file_section_loading);

// Sets whether to enable single threaded execution.
//
// @param settings The engine settings.
// @param single_threaded_execution Whether to enable single threaded
// execution.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.2.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_single_threaded_execution(
    LiteRtLmEngineSettings* settings, bool single_threaded_execution);

// Sets the maximum number of images for the engine.
//
// This is only used for the legacy implementation of the engine.
//
// @param settings The engine settings.
// @param max_num_images The maximum number of images.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_max_num_images(
    LiteRtLmEngineSettings* settings, int max_num_images);

// Sets the maximum vision tokens generated per image for the engine.
//
// When set, the engine automatically selects vision encoder and adapter
// signatures with capacity up to this limit and configures vision patch
// metadata.
//
// @param settings The engine settings.
// @param max_vision_tokens_per_image The maximum vision tokens per image.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_max_vision_tokens_per_image(
    LiteRtLmEngineSettings* settings, int max_vision_tokens_per_image);

// Sets the cache directory for the engine.
//
// @param settings The engine settings.
// @param cache_dir The cache directory.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` or `cache_dir` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_cache_dir(
    LiteRtLmEngineSettings* settings, const char* cache_dir);

// Sets the LiteRT dispatch library directory for NPU backend.
//
// @param settings The engine settings.
// @param lib_dir The dispatch library directory.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` or `lib_dir` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_litert_dispatch_lib_dir(
    LiteRtLmEngineSettings* settings, const char* lib_dir);

// Represents the activation data type.
//
// Added in version 0.1.0.
typedef enum {
  kLiteRtLmActivationDataTypeFloat32 = 0,
  kLiteRtLmActivationDataTypeFloat16 = 1,
  kLiteRtLmActivationDataTypeInt16 = 2,
  kLiteRtLmActivationDataTypeInt8 = 3,
} LiteRtLmActivationDataType;

// Sets the activation data type.
//
// @param settings The engine settings.
// @param activation_data_type The activation data type.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL or `activation_data_type` is not a declared
//   LiteRtLmActivationDataType value.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_activation_data_type(
    LiteRtLmEngineSettings* settings,
    LiteRtLmActivationDataType activation_data_type);

// Sets the prefill chunk size for the engine. Only applicable for CPU backend
// with dynamic models.
//
// @param settings The engine settings.
// @param prefill_chunk_size The prefill chunk size.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If the main backend is not CPU, this call has no effect
//   and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_prefill_chunk_size(
    LiteRtLmEngineSettings* settings, int prefill_chunk_size);

// Sets whether YNNPACK should delegate supported operations before XNNPACK.
//
// @param settings The engine settings.
// @param enable_ynnpack Whether to enable YNNPACK.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If the main backend is not CPU, this call has no effect
//   and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_enable_ynnpack(
    LiteRtLmEngineSettings* settings, bool enable_ynnpack);

// Enables benchmarking for the engine.
//
// @param settings The engine settings.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_enable_benchmark(
    LiteRtLmEngineSettings* settings);

// Sets the number of prefill tokens for benchmarking.
//
// @param settings The engine settings.
// @param num_prefill_tokens The number of prefill tokens.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_num_prefill_tokens(
    LiteRtLmEngineSettings* settings, int num_prefill_tokens);

// Sets the number of decode tokens for benchmarking.
//
// @param settings The engine settings.
// @param num_decode_tokens The number of decode tokens.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_num_decode_tokens(
    LiteRtLmEngineSettings* settings, int num_decode_tokens);

// Sets whether to enable speculative decoding.
//
// Deprecated: use `litert_lm_engine_settings_set_speculative_decoding_config`
// instead. Passing true is equivalent to a config whose mode is
// `kLiteRtLmSpeculativeDecodingModeOn` and passing false to
// `kLiteRtLmSpeculativeDecodingModeOff`. This function and
// `litert_lm_engine_settings_set_speculative_decoding_config` write the same
// mode, so the last call wins.
//
// @param settings The engine settings.
// @param enable_speculative_decoding Whether to enable speculative decoding.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
// Deprecated in version 1.0.0.
LITERT_LM_C_API_EXPORT
LITERT_LM_C_API_DEPRECATED(
    "Use litert_lm_engine_settings_set_speculative_decoding_config instead.")
LiteRtLmStatusCode litert_lm_engine_settings_set_enable_speculative_decoding(
    LiteRtLmEngineSettings* settings, bool enable_speculative_decoding);

// Sets the default speculative decoding config of the engine's sessions (see
// `litert_lm_speculative_decoding_config_create`). Each session can override
// any field with `litert_lm_session_config_set_speculative_decoding_config`.
// The config is copied, so the caller keeps ownership of
// `speculative_decoding_config`. Replaces any config set before, including the
// mode set by the deprecated
// `litert_lm_engine_settings_set_enable_speculative_decoding`.
// @param settings The engine settings.
// @param speculative_decoding_config The speculative decoding config.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` or `speculative_decoding_config` is NULL.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_speculative_decoding_config(
    LiteRtLmEngineSettings* settings,
    const LiteRtLmSpeculativeDecodingConfig* speculative_decoding_config);

// Sets the number of decode steps per sync for the GPU backend.
// Note: This setting is currently only supported for the Artisan GPU
// backend (Artisan).
//
// @param settings The engine settings.
// @param num_decode_steps_per_sync The number of decode steps per sync.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If the main backend is not the Artisan GPU backend,
//   this call has no effect and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_gpu_decode_steps_per_sync(
    LiteRtLmEngineSettings* settings, int num_decode_steps_per_sync);

// Sets whether to wait for weight uploads for the GPU backend.
// Note: This setting is currently only supported for the Artisan GPU backend.
//
// @param settings The engine settings.
// @param wait_for_weight_uploads Whether to wait for weight uploads.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If the main backend is not the Artisan GPU backend,
//   this call has no effect and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_gpu_wait_for_weight_uploads(
    LiteRtLmEngineSettings* settings, bool wait_for_weight_uploads);

// Sets whether to use ringbuffers for local attention KV cache.
//
// When enabled for supported models, a ringbuffer stores only necessary KV
// cache memory for local attention layers, minimizing memory usage. When
// disabled, memory is allocated for the full context length, enabling instant
// rewinding at the cost of higher memory usage.
//
// Note: This feature is backend-agnostic in interface design, but currently
// only supported by the GPU Artisan backend. Enabling it on unsupported models
// or backends will be ignored with a warning.
//
// @param settings The engine settings.
// @param use_ringbuffers_local_attention Whether to use ringbuffers for local
// attention.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If the main backend is not the Artisan GPU backend,
//   this call has no effect and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode
litert_lm_engine_settings_set_use_ringbuffers_local_attention(
    LiteRtLmEngineSettings* settings, bool use_ringbuffers_local_attention);

// Sets the LoRA rank for the engine.
//
// @param settings The engine settings.
// @param lora_rank The LoRA rank.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_lora_rank(
    LiteRtLmEngineSettings* settings, int lora_rank);

// Sets the supported LoRA ranks for the engine.
//
// @param settings The engine settings.
// @param lora_ranks An array of supported LoRA ranks.
// @param num_ranks The number of ranks in the array.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `settings` or `lora_ranks` is NULL or `num_ranks` is 0.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_supported_lora_ranks(
    LiteRtLmEngineSettings* settings, const int* lora_ranks, size_t num_ranks);

// Sets the Audio LoRA rank for the engine.
//
// @param settings The engine settings.
// @param lora_rank The Audio LoRA rank.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. If no audio backend is configured, this call has no
//   effect and returns kLiteRtLmStatusOk.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_audio_lora_rank(
    LiteRtLmEngineSettings* settings, int lora_rank);

// Sets the supported Audio LoRA ranks for the engine.
//
// @param settings The engine settings.
// @param lora_ranks An array of supported Audio LoRA ranks.
// @param num_ranks The number of ranks in the array.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `settings` or `lora_ranks` is NULL or `num_ranks` is 0;
//   kLiteRtLmStatusFailedPrecondition if no audio executor is configured.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_supported_audio_lora_ranks(
    LiteRtLmEngineSettings* settings, const int* lora_ranks, size_t num_ranks);

// Sets whether to enable Metal residency set on GPU.
//
// When enabled on Apple platforms (macOS and iOS with Metal GPU backend), this
// uses Apple's MTLResidencySet API to ensure model weights and allocations
// remain resident in GPU memory, preventing memory swapping and reducing
// allocation overhead.
//
// This setting is only supported on Apple platforms (macOS / iOS) with the GPU
// backend. On other platforms (e.g. Linux, Android, Windows) or non-GPU
// backends, this setting has no effect and is safely ignored.
//
// @param settings The engine settings.
// @param enable_metal_residency_set Whether to enable Metal residency set.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `settings` is NULL. The setting is still recorded (and kLiteRtLmStatusOk
//   returned) on platforms or backends where it has no effect.
//
// Added in version 0.2.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_settings_set_gpu_enable_metal_residency_set(
    LiteRtLmEngineSettings* settings, bool enable_metal_residency_set);

// Creates a LiteRT LM Engine from the given settings.
//
// @param settings The engine settings.
// @param out_engine On success, receives the created engine, owned by the
//   caller; release with `litert_lm_engine_delete`. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `settings` or `out_engine` is NULL; otherwise the code of the error
//   encountered while creating the engine (e.g. the model cannot be loaded).
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the engine is returned
// through out_engine.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_create(
    const LiteRtLmEngineSettings* settings, LiteRtLmEngine** out_engine);

// Ownership and lifetime
// ----------------------
// An engine owns the resources shared by every session and conversation
// created from it. Handles are NOT reference counted across the C boundary
// and are NOT reparented.
//
// The caller MUST destroy every session and conversation created from an
// engine before destroying the engine itself. Destroying an engine with
// live children, or using a child after its engine has been destroyed, is
// undefined behavior.
//
// Handles are not thread-safe; see "Threading model" at the top of this file.

// Destroys a LiteRT LM Engine.
//
// Every session and conversation created from `engine` MUST be destroyed
// before calling this function; see "Ownership and lifetime" above.
//
// @param engine The engine to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_engine_delete(LiteRtLmEngine* engine);

// Creates a LiteRT LM Session.
//
// @param engine The engine to create the session from.
// @param config The session config of the session. If NULL, use the default
// session config.
// @param out_session On success, receives the created session, owned by the
//   caller; release with `litert_lm_session_delete`. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `engine`
//   or `out_session` is NULL; otherwise the code of the error encountered
//   while creating the session.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the session is returned
// through out_session.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_create_session(
    LiteRtLmEngine* engine, LiteRtLmSessionConfig* config,
    LiteRtLmSession** out_session);

// Destroys a LiteRT LM Session.
//
// @param session The session to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_session_delete(LiteRtLmSession* session);

// Clones a LiteRT LM Session, copying its state so the new session can be used
// independently.
//
// @param session The session to clone.
// @param out_session On success, receives the cloned session, owned by the
//   caller; release with `litert_lm_session_delete`. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   or `out_session` is NULL.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_clone(LiteRtLmSession* session,
                                           LiteRtLmSession** out_session);

// Cancels the current processing in the session.
//
// This function only requests cancellation and does not wait for it: the
// stream callback can still run after this function returns. The session MUST
// NOT be reused afterwards; only delete it. See "Threading model" at the top of
// this file.
//
// @param session The session to cancel processing on.
// @return kLiteRtLmStatusOk on success, or kLiteRtLmStatusInvalidArgument if
//   `session` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code instead of void.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_cancel_process(LiteRtLmSession* session);

// Saves the current state of the session to a checkpoint with the given label.
//
// @param session The session to save checkpoint for.
// @param label Label for the checkpoint.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   or `label` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_save_checkpoint(LiteRtLmSession* session,
                                                     const char* label);

// Rewinds the session to the given checkpoint label.
//
// @param session The session to rewind.
// @param label Label of the checkpoint to rewind to.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   or `label` is NULL; otherwise the runtime error (e.g. if no checkpoint with
//   `label` exists).
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_rewind_to_checkpoint(
    LiteRtLmSession* session, const char* label);

// Rewinds the session to a specific step number.
//
// @param session The session to rewind.
// @param step The step number to rewind to.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   is NULL; otherwise the runtime error (e.g. if `step` is invalid).
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_rewind_to_step(LiteRtLmSession* session,
                                                    int step);

// Adds the input prompt/query to the model for starting the prefilling
// process. This is a blocking call and the function will return when the
// prefill process is done.
//
// @param session The session to use.
// @param inputs An array of InputData structs representing the multimodal
//   input.
// @param num_inputs The number of InputData structs in the array.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   or `inputs` is NULL or `num_inputs` is 0.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_run_prefill(
    LiteRtLmSession* session, const LiteRtLmInputData* const* inputs,
    size_t num_inputs);

// Starts the decoding process for the model to predict the response based
// on the input prompt/query added after using litert_lm_session_run_prefill.
// This is a blocking call and the function will return when the decoding
// process is done.
//
// @param session The session to use.
// @param out_responses On success, receives the responses, owned by the
//   caller; release with `litert_lm_responses_delete`. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `session` or `out_responses` is NULL; otherwise the runtime error.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_responses.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_run_decode(
    LiteRtLmSession* session, LiteRtLmResponses** out_responses);

// Scores the target text after the prefill process is done.
//
// @param session The session to use.
// @param target_text An array of target text strings to score.
// @param num_targets The number of strings in the target_text array.
// @param store_token_lengths Whether to store the token lengths of the target
//   texts in the responses.
// @param out_responses On success, receives the responses, owned by the
//   caller; release with `litert_lm_responses_delete`. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `session`, `target_text` or `out_responses` is NULL or `num_targets` is 0;
//   otherwise the runtime error.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_responses.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_run_text_scoring(
    LiteRtLmSession* session, const char** target_text, size_t num_targets,
    bool store_token_lengths, LiteRtLmResponses** out_responses);

// Generates content from the input prompt.
//
// @param session The session to use for generation.
// @param inputs An array of LiteRtLmInputData structs representing the
// multimodal
//   input.
// @param num_inputs The number of LiteRtLmInputData structs in the array.
// @param out_responses On success, receives the responses, owned by the
//   caller; release with `litert_lm_responses_delete`. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `session` or `out_responses` is NULL; otherwise the code of the error
//   encountered while copying the inputs or generating content.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_responses.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_generate_content(
    LiteRtLmSession* session, const LiteRtLmInputData* const* inputs,
    size_t num_inputs, LiteRtLmResponses** out_responses);

// Destroys a LiteRT LM Responses object.
//
// @param responses The responses to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_responses_delete(LiteRtLmResponses* responses);

// Returns the number of response candidates.
//
// The number of candidates is the number of response texts or, if there are
// none (e.g. for text scoring), the number of scores or token lengths. Valid
// candidate indices for the `litert_lm_responses_*_at` functions are
// `[0, *out_num_candidates)`, although individual values may still be absent
// at a valid index (see the `has_*_at` predicates).
//
// @param responses The responses object.
// @param out_num_candidates On success, receives the number of candidates; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_num_candidates` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_num_candidates.
// Changed in version 1.0.0: `*out_num_candidates` is a size_t
// instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_get_num_candidates(
    const LiteRtLmResponses* responses, size_t* out_num_candidates);

// Returns the response text at a given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_text On success, receives the response text. The string is owned
//   by the `responses` object and is valid only for its lifetime. Set to NULL
//   on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_text` is NULL; kLiteRtLmStatusOutOfRange if `index` is
//   out of bounds of the candidates; kLiteRtLmStatusNotFound if there is no
//   response text at `index`.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the text is returned
// through out_text.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_get_response_text_at(
    const LiteRtLmResponses* responses, size_t index, const char** out_text);

// Returns whether the response contains a score at the given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_has_score On success, receives true if a score is available at
//   `index`, false otherwise; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_has_score` is NULL; kLiteRtLmStatusOutOfRange if
//   `index` is out of bounds of the candidates.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_has_score.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_has_score_at(
    const LiteRtLmResponses* responses, size_t index, bool* out_has_score);

// Returns the score at a given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_score On success, receives the score; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_score` is NULL; kLiteRtLmStatusOutOfRange if `index`
//   is out of bounds of the candidates; kLiteRtLmStatusNotFound if no score is
//   available at `index`.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the score is returned
// through out_score.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_get_score_at(
    const LiteRtLmResponses* responses, size_t index, float* out_score);

// Returns whether the response contains a token length at the given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_has_token_length On success, receives true if a token length is
//   available at `index`, false otherwise; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_has_token_length` is NULL; kLiteRtLmStatusOutOfRange
//   if `index` is out of bounds of the candidates.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_has_token_length.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_has_token_length_at(
    const LiteRtLmResponses* responses, size_t index,
    bool* out_has_token_length);

// Returns the token length at a given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_token_length On success, receives the token length; not written
//   on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_token_length` is NULL; kLiteRtLmStatusOutOfRange if
//   `index` is out of bounds of the candidates; kLiteRtLmStatusNotFound if no
//   token length is available at `index`.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the token length is
// returned through out_token_length.
// Changed in version 1.0.0: `index` and `*out_token_length` are
// size_t instead of int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_get_token_length_at(
    const LiteRtLmResponses* responses, size_t index, size_t* out_token_length);

// Returns whether the response contains token scores at the given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_has_token_scores On success, receives true if token scores are
//   available at `index`, false otherwise; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_has_token_scores` is NULL; kLiteRtLmStatusOutOfRange
//   if `index` is out of bounds of the candidates.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_has_token_scores.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_has_token_scores_at(
    const LiteRtLmResponses* responses, size_t index,
    bool* out_has_token_scores);

// Returns the number of tokens for which scores are present at a given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_num_token_scores On success, receives the number of token scores;
//   not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_num_token_scores` is NULL; kLiteRtLmStatusOutOfRange
//   if `index` is out of bounds of the candidates; kLiteRtLmStatusNotFound if
//   no token scores are available at `index`.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_num_token_scores.
// Changed in version 1.0.0: `index` and `*out_num_token_scores`
// are size_t instead of int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_get_num_token_scores_at(
    const LiteRtLmResponses* responses, size_t index,
    size_t* out_num_token_scores);

// Returns the token scores at a given index.
//
// @param responses The responses object.
// @param index The index of the response.
// @param out_token_scores On success, receives a pointer to the internal array
//   of token scores, whose length is given by
//   `litert_lm_responses_get_num_token_scores_at`. The array is owned by the
//   `responses` object and is valid only for its lifetime. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `responses` or `out_token_scores` is NULL; kLiteRtLmStatusOutOfRange if
//   `index` is out of bounds of the candidates; kLiteRtLmStatusNotFound if no
//   token scores are available at `index`.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the array is returned
// through out_token_scores.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_responses_get_token_scores_at(
    const LiteRtLmResponses* responses, size_t index,
    const float** out_token_scores);

// Retrieves the benchmark information from the session.
//
// @param session The session to get the benchmark info from.
// @param out_benchmark_info On success, receives the benchmark info, owned by
//   the caller; release with `litert_lm_benchmark_info_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `session` or `out_benchmark_info` is NULL; otherwise the runtime error
//   (e.g. if benchmarking is not enabled).
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_benchmark_info.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_get_benchmark_info(
    LiteRtLmSession* session, LiteRtLmBenchmarkInfo** out_benchmark_info);

// Destroys a LiteRT LM Benchmark Info object.
//
// @param benchmark_info The benchmark info to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_benchmark_info_delete(LiteRtLmBenchmarkInfo* benchmark_info);

// Returns the time to the first token in seconds.
//
// Note that the first time to token doesn't include the time for
// initialization. It is the sum of the prefill time for the first turn and
// the time spent for decoding the first token.
//
// @param benchmark_info The benchmark info object.
// @param out_seconds On success, receives the time to the first token in
//   seconds; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_seconds` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the value is returned
// through out_seconds.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_time_to_first_token(
    const LiteRtLmBenchmarkInfo* benchmark_info, double* out_seconds);

// Returns the total initialization time in seconds.
//
// @param benchmark_info The benchmark info object.
// @param out_seconds On success, receives the total initialization time in
//   seconds; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_seconds` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the value is returned
// through out_seconds.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_total_init_time_in_second(
    const LiteRtLmBenchmarkInfo* benchmark_info, double* out_seconds);

// Returns the number of prefill turns.
//
// @param benchmark_info The benchmark info object.
// @param out_num_turns On success, receives the number of prefill turns; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_num_turns` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_num_turns.
// Changed in version 1.0.0: `*out_num_turns` is a size_t instead of
// an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_num_prefill_turns(
    const LiteRtLmBenchmarkInfo* benchmark_info, size_t* out_num_turns);

// Returns the number of decode turns.
//
// @param benchmark_info The benchmark info object.
// @param out_num_turns On success, receives the number of decode turns; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_num_turns` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_num_turns.
// Changed in version 1.0.0: `*out_num_turns` is a size_t instead of
// an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_num_decode_turns(
    const LiteRtLmBenchmarkInfo* benchmark_info, size_t* out_num_turns);

// Returns the prefill token count at a given turn index.
//
// @param benchmark_info The benchmark info object.
// @param index The index of the prefill turn.
// @param out_token_count On success, receives the prefill token count; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_token_count` is NULL; kLiteRtLmStatusOutOfRange
//   if `index` is out of bounds of the prefill turns.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_token_count.
// Changed in version 1.0.0: `index` and `*out_token_count` are
// size_t instead of int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_prefill_token_count_at(
    const LiteRtLmBenchmarkInfo* benchmark_info, size_t index,
    size_t* out_token_count);

// Returns the decode token count at a given turn index.
//
// @param benchmark_info The benchmark info object.
// @param index The index of the decode turn.
// @param out_token_count On success, receives the decode token count; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_token_count` is NULL; kLiteRtLmStatusOutOfRange
//   if `index` is out of bounds of the decode turns.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_token_count.
// Changed in version 1.0.0: `index` and `*out_token_count` are
// size_t instead of int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_decode_token_count_at(
    const LiteRtLmBenchmarkInfo* benchmark_info, size_t index,
    size_t* out_token_count);

// Returns the prefill tokens per second at a given turn index.
//
// @param benchmark_info The benchmark info object.
// @param index The index of the prefill turn.
// @param out_tokens_per_sec On success, receives the prefill tokens per
//   second (0 if the turn has no measurable duration); not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_tokens_per_sec` is NULL;
//   kLiteRtLmStatusOutOfRange if `index` is out of bounds of the prefill turns.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the value is returned
// through out_tokens_per_sec.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_prefill_tokens_per_sec_at(
    const LiteRtLmBenchmarkInfo* benchmark_info, size_t index,
    double* out_tokens_per_sec);

// Returns the decode tokens per second at a given turn index.
//
// @param benchmark_info The benchmark info object.
// @param index The index of the decode turn.
// @param out_tokens_per_sec On success, receives the decode tokens per second
//   (0 if the turn has no measurable duration); not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_tokens_per_sec` is NULL;
//   kLiteRtLmStatusOutOfRange if `index` is out of bounds of the decode turns.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the value is returned
// through out_tokens_per_sec.
// Changed in version 1.0.0: `index` is a size_t instead of an int.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_decode_tokens_per_sec_at(
    const LiteRtLmBenchmarkInfo* benchmark_info, size_t index,
    double* out_tokens_per_sec);

// Returns the max number of tokens, i.e. the KV cache size, that the engine
// used.
//
// It can differ from the value requested with
// `litert_lm_engine_settings_set_max_num_tokens`, e.g. for a model whose KV
// cache size is fixed when it is compiled, or when no value was requested.
//
// @param benchmark_info The benchmark info object.
// @param out_max_num_tokens On success, receives the max number of tokens, or
//   0 if the engine didn't report it (e.g. before the first prefill); not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `benchmark_info` or `out_max_num_tokens` is NULL.
//
// Added in version 1.0.0.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_benchmark_info_get_max_num_tokens(
    const LiteRtLmBenchmarkInfo* benchmark_info, int* out_max_num_tokens);

// Opaque pointer for LiteRT LM Stream Chunk.
// This object represents a single chunk of data returned during streaming.
// It is owned by the library and is only valid for the duration of the
// callback.
//
// Added in version 0.1.0.
typedef struct LiteRtLmStreamChunk LiteRtLmStreamChunk;

// Gets the text content of the chunk.
//
// @param chunk The stream chunk.
// @param out_text On success, receives the text content of the chunk, or NULL
//   if the chunk has no text content (e.g. if it is an error or final
//   metadata-only chunk). The string is owned by the chunk and is only valid
//   as long as the chunk is valid. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success (including when the chunk has no text),
//   or another LiteRtLmStatusCode on failure (see error_reporter.h).
//   kLiteRtLmStatusInvalidArgument if `chunk` or `out_text` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the text is returned
// through out_text.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_stream_chunk_get_text(
    const LiteRtLmStreamChunk* chunk, const char** out_text);

// Returns whether this is the final chunk of the stream.
//
// @param chunk The stream chunk.
// @param out_is_final On success, receives true if this is the final chunk of
//   the stream, false otherwise; not written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `chunk`
//   or `out_is_final` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_is_final.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_stream_chunk_is_final(
    const LiteRtLmStreamChunk* chunk, bool* out_is_final);

// Gets the error message associated with this chunk, if any.
//
// @param chunk The stream chunk.
// @param out_error On success, receives the error message of the chunk, or
//   NULL if the chunk carries no error. The string is owned by the chunk and
//   is only valid as long as the chunk is valid. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success (including when the chunk carries no
//   error), or another LiteRtLmStatusCode on failure (see error_reporter.h).
//   kLiteRtLmStatusInvalidArgument if `chunk` or `out_error` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the message is returned
// through out_error.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_stream_chunk_get_error(
    const LiteRtLmStreamChunk* chunk, const char** out_error);

// Callback for streaming responses.
// `callback_data` is a pointer to user-defined data passed to the stream
// function. `chunk` is a pointer to the stream chunk object. It's only valid
// for the duration of the call.
//
// See "Threading model" at the top of this file for where the callback runs
// and which functions it must not call.
//
// Added in version 0.1.0.
typedef void (*LiteRtLmStreamCallback)(void* callback_data,
                                       const LiteRtLmStreamChunk* chunk);

// Starts the decoding process for the model to predict the response based
// on the input prompt/query added after using litert_lm_session_run_prefill.
// This is a non-blocking call that will stream responses via a callback.
//
// @param session The session to use.
// @param callback The callback function to receive response chunks.
// @param callback_data A pointer to user data that will be passed to the
// callback.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   or `callback` is NULL; otherwise the code of the error that prevented the
//   stream from starting. Errors that occur after the stream has started are
//   reported through the callback.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_run_decode_async(
    LiteRtLmSession* session, LiteRtLmStreamCallback callback,
    void* callback_data);

// Generates content from the input prompt and streams the response via a
// callback. This is a non-blocking call that will invoke the callback from a
// background thread for each chunk (see "Threading model" at the top of this
// file).
//
// @param session The session to use for generation.
// @param inputs An array of LiteRtLmInputData structs representing the
// multimodal
//   input.
// @param num_inputs The number of LiteRtLmInputData structs in the array.
// @param callback The callback function to receive response chunks.
// @param callback_data A pointer to user data that will be passed to the
// callback.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `session`
//   or `callback` is NULL; otherwise the code of the error that prevented the
//   stream from starting. Errors that occur after the stream has started are
//   reported through the callback.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_session_generate_content_stream(
    LiteRtLmSession* session, const LiteRtLmInputData* const* inputs,
    size_t num_inputs, LiteRtLmStreamCallback callback, void* callback_data);

// Tokenizes text using the engine's tokenizer.
//
// @param engine The engine instance.
// @param text The UTF-8 string to tokenize.
// @param out_result On success, receives the tokenize result, owned by the
//   caller; release with `litert_lm_tokenize_result_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `engine`, `text` or `out_result` is NULL; otherwise the code of the
//   tokenizer error.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_result.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_tokenize(
    LiteRtLmEngine* engine, const char* text,
    LiteRtLmTokenizeResult** out_result);

// Destroys a LiteRT LM Tokenize Result.
//
// @param result The tokenize result to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_tokenize_result_delete(LiteRtLmTokenizeResult* result);

// Returns the token ids from a tokenize result.
//
// @param result The tokenize result.
// @param out_tokens On success, receives a pointer to the internal array of
//   token ids, whose length is given by
//   `litert_lm_tokenize_result_get_num_tokens`. The pointer is valid only for
//   the lifetime of the `result` object, and may be NULL if the result holds
//   no tokens. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `result`
//   or `out_tokens` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the array is returned
// through out_tokens.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_tokenize_result_get_tokens(
    const LiteRtLmTokenizeResult* result, const int** out_tokens);

// Returns the number of token ids from a tokenize result.
//
// @param result The tokenize result.
// @param out_num_tokens On success, receives the number of token ids; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `result`
//   or `out_num_tokens` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_num_tokens.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_tokenize_result_get_num_tokens(
    const LiteRtLmTokenizeResult* result, size_t* out_num_tokens);

// Detokenizes token ids using the engine's tokenizer.
//
// @param engine The engine instance.
// @param tokens An array of token ids to detokenize.
// @param num_tokens The number of token ids in the array.
// @param out_result On success, receives the detokenize result, owned by the
//   caller; release with `litert_lm_detokenize_result_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `engine`, `tokens` or `out_result` is NULL; otherwise the code of the
//   tokenizer error.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_result.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_detokenize(
    LiteRtLmEngine* engine, const int* tokens, size_t num_tokens,
    LiteRtLmDetokenizeResult** out_result);

// Destroys a LiteRT LM Detokenize Result.
//
// @param result The detokenize result to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_detokenize_result_delete(LiteRtLmDetokenizeResult* result);

// Returns the string from a detokenize result.
//
// @param result The detokenize result.
// @param out_text On success, receives the detokenized UTF-8 string. The
//   string is owned by the `result` object and is valid only for its lifetime.
//   Set to NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `result`
//   or `out_text` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the string is returned
// through out_text.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_detokenize_result_get_string(
    const LiteRtLmDetokenizeResult* result, const char** out_text);

// Destroys a LiteRT LM Token Union that the caller owns, such as one returned
// by `litert_lm_engine_get_start_token`. Do not pass a token union borrowed
// from a collection by `litert_lm_token_unions_get_token_at`.
//
// @param token_union The token union to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_token_union_delete(LiteRtLmTokenUnion* token_union);

// Returns the type of the token union.
//
// @param token_union The token union.
// @param out_type On success, receives the type of the token union; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `token_union` or `out_type` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the type is returned
// through out_type.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_token_union_get_type(
    const LiteRtLmTokenUnion* token_union, LiteRtLmTokenUnionType* out_type);

// Returns the string value from a token union.
//
// @param token_union The token union.
// @param out_string On success, receives the string value. The string is owned
//   by the `token_union` object and is valid only for its lifetime. Set to
//   NULL on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if
//   `token_union` or `out_string` is NULL, or if the type of `token_union` is
//   not kLiteRtLmTokenUnionTypeString.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the string is returned
// through out_string.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_token_union_get_string(
    const LiteRtLmTokenUnion* token_union, const char** out_string);

// Returns the token ids from a token union.
//
// @param token_union The token union.
// @param out_tokens On success, receives a pointer to the internal array of
//   token ids. The received pointer is valid only for the lifetime of the
//   `token_union` object. Set to NULL on failure.
// @param out_num_tokens On success, receives the number of token ids; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if any
//   argument is NULL or the type of `token_union` is not
//   kLiteRtLmTokenUnionTypeIds.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: failures return a canonical LiteRtLmStatusCode
// instead of -1.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_token_union_get_ids(
    const LiteRtLmTokenUnion* token_union, const int** out_tokens,
    size_t* out_num_tokens);

// Destroys a LiteRT LM Token Unions object.
//
// @param tokens The token unions object to destroy.
//
// Added in version 0.1.0.
LITERT_LM_C_API_EXPORT
void litert_lm_token_unions_delete(LiteRtLmTokenUnions* tokens);

// Returns the number of token unions in the collection.
//
// @param tokens The token unions object.
// @param out_num_tokens On success, receives the number of token unions; not
//   written on failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `tokens`
//   or `out_num_tokens` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the count is returned
// through out_num_tokens.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_token_unions_get_num_tokens(
    const LiteRtLmTokenUnions* tokens, size_t* out_num_tokens);

// Returns the token union at a given index in a collection.
//
// @param tokens The token unions collection.
// @param index The index of the token union.
// @param out_token On success, receives the token union at `index`. The
//   returned pointer is owned by `tokens` and is invalidated when it is
//   deleted; do not pass it to `litert_lm_token_union_delete`. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success, or another LiteRtLmStatusCode on
//   failure (see error_reporter.h). kLiteRtLmStatusInvalidArgument if `tokens`
//   or `out_token` is NULL; kLiteRtLmStatusOutOfRange if `index` is out of
//   bounds.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_token and is borrowed from `tokens` instead of being a copy
// that the caller deletes.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_token_unions_get_token_at(
    const LiteRtLmTokenUnions* tokens, size_t index,
    const LiteRtLmTokenUnion** out_token);

// Returns the configured start token (BOS), if any.
//
// @param engine The engine instance.
// @param out_token On success, receives the start token, owned by the caller;
//   release with `litert_lm_token_union_delete`. Receives NULL on success if
//   the engine has no start token configured. Set to NULL on failure.
// @return kLiteRtLmStatusOk on success (including when no start token is
//   configured), or another LiteRtLmStatusCode on failure (see
//   error_reporter.h). kLiteRtLmStatusInvalidArgument if `engine` or
//   `out_token` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_token.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_get_start_token(
    LiteRtLmEngine* engine, LiteRtLmTokenUnion** out_token);

// Returns the configured stop tokens (EOS).
//
// @param engine The engine instance.
// @param out_tokens On success, receives the stop tokens collection, owned by
//   the caller; release with `litert_lm_token_unions_delete`. Receives NULL on
//   success if the engine has no stop tokens configured. Set to NULL on
//   failure.
// @return kLiteRtLmStatusOk on success (including when no stop tokens are
//   configured), or another LiteRtLmStatusCode on failure (see
//   error_reporter.h). kLiteRtLmStatusInvalidArgument if `engine` or
//   `out_tokens` is NULL.
//
// Added in version 0.1.0.
// Changed in version 1.0.0: returns a status code; the result is returned
// through out_tokens.
LITERT_LM_C_API_EXPORT
LiteRtLmStatusCode litert_lm_engine_get_stop_tokens(
    LiteRtLmEngine* engine, LiteRtLmTokenUnions** out_tokens);

#ifdef __cplusplus
}  // extern "C"
#endif

#endif  // THIRD_PARTY_ODML_LITERT_LM_C_ENGINE_H_