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
/*
* vllm.h — the vllm.cpp stable C ABI (libvllm).
*
* ORIGINAL packaging layer — NOT a 1:1 upstream mirror. vLLM ships no C ABI;
* this header is the llama.cpp-style (see llama.h) handle-based C surface over
* the C++ LLMEngine, so downstream FFI consumers (LocalAI via purego/cgo, any
* C/C++ host) can load + drive vllm.cpp without the C++ headers. Recorded as a
* deviation in .agents/porting-inventory.md §9 (C-ABI packaging, alongside the
* vt:: runtime and the cpp-httplib transport).
*
* ── ABI contract ────────────────────────────────────────────────────────────
* - PURE C: this header compiles as C (C11) and as C++. It uses only C types —
* opaque handle typedefs, POD param/result structs, primitive scalars and
* `const char*`. No C++ leaks across the boundary.
* - NO-THROW: every entry point catches all C++ exceptions internally and maps
* them to a `vllm_status`; nothing throws across the ABI. On a non-OK status,
* `vllm_last_error()` returns a human-readable message (thread-local).
* - OWNERSHIP is documented per pointer below. In short: the caller frees
* heap `char*` returned in results (via vllm_string_free / vllm_completion_free)
* and frees engine handles (via vllm_engine_free); `const char*` returns
* (finish_reason, vllm_last_error, vllm_version) point to storage the library
* owns and the caller must NOT free.
* - Scope (M3.5 Task 1): load a model + blocking completion. Streaming callback
* + sampled generation land in Task 2; the shared/static lib packaging + CLI
* + dlopen smoke test in Task 3.
*/
/* `bool` in the streaming callback signature: native in C++, needs <stdbool.h>
* when this header is compiled as C. */
extern "C" VLLM_API
/* ── Status codes ─────────────────────────────────────────────────────────────
* Every fallible entry point returns one of these. VLLM_OK == 0. On any error,
* vllm_last_error() (thread-local) carries the detail. */
typedef enum vllm_status vllm_status;
/* ── Opaque handles ───────────────────────────────────────────────────────────
* The engine handle owns the whole C++ stack (LLMEngine + Scheduler + runner +
* KV cache + processors + tokenizer). Created by vllm_engine_load, destroyed by
* vllm_engine_free. W2 completion submissions are thread-safe and share one
* AsyncLLM scheduler; destruction still requires all request handles freed. */
typedef struct vllm_engine vllm_engine;
/* A non-blocking request returned by vllm_request_submit. The engine MUST
* outlive every request created from it. Free with vllm_request_free. */
typedef struct vllm_request vllm_request;
/* ── Model / load parameters ──────────────────────────────────────────────────
* POD. Populate with vllm_model_params_default() then override. All `const char*`
* fields are borrowed for the duration of the vllm_engine_load call only — the
* library copies what it needs; the caller retains ownership. */
typedef struct vllm_model_params vllm_model_params;
/* ── Custom logits processor (ABI v8) ─────────────────────────────────────────
* A host callback the sampler invokes ONCE PER DECODE STEP for the request,
* BEFORE sampling, to inspect the tokens generated so far and modify the logits.
* It runs at vLLM's non-argmax-invariant logits-processor stage — after
* allowed_token_ids / bad_words / min_tokens / logit_bias, before penalties —
* mirroring SamplingParams.logits_processors (and satisfying SGLang's
* custom_logit_processor capability with the same callback).
* - token_ids / n_token_ids: the request's generated output token ids so far
* (n_token_ids == 0 on the first decode step). BORROWED — valid only for the
* duration of the call; copy them if you need to retain them.
* - logits / vocab_size: a MUTABLE view of THIS request's logits row, a
* contiguous float[vocab_size]. Edit it in place (add a bias, mask tokens to
* -inf, force a token to +inf, ...); the edited row is what the sampler then
* samples from. Greedy decoding takes the argmax of the edited row.
* - user_data: the opaque pointer registered in
* vllm_sampling_params.logits_processor_user_data, round-tripped unchanged.
* The callback is C code and MUST NOT throw across the ABI boundary. */
typedef void ;
/* ── Sampling parameters ──────────────────────────────────────────────────────
* POD mirror of the T0 fields of vllm::SamplingParams. Populate with
* vllm_sampling_params_default() then override; a zero-initialized struct is NOT
* valid (repetition_penalty must be > 0). temperature <= 0 selects greedy.
* `stop` is borrowed for the duration of the vllm_complete call; the library
* copies the strings. */
typedef struct vllm_sampling_params vllm_sampling_params;
/* ── Completion result ────────────────────────────────────────────────────────
* Filled by vllm_complete. OWNERSHIP:
* - text: heap-allocated, NUL-terminated. The CALLER owns it and must free it
* via vllm_completion_free(out) (or vllm_string_free(out->text)). Set to
* NULL on any non-OK status.
* - finish_reason: borrowed pointer into library-owned static storage
* ("stop" / "length" / "abort" / ...). The caller must NOT free it and must
* not use it after the string literal's program lifetime (it is static, so
* it is always valid). NULL if the request did not finish.
* - prompt_tokens / completion_tokens: token counts (prompt vs generated). */
typedef struct vllm_completion vllm_completion;
/* ── Defaults ─────────────────────────────────────────────────────────────────
* Return structs pre-filled with the upstream SamplingParams / sane load
* defaults. Use these as the base and override fields, so future struct growth
* stays source-compatible. */
VLLM_API vllm_model_params ;
VLLM_API vllm_sampling_params ;
/* ── Lifecycle ────────────────────────────────────────────────────────────────
* vllm_engine_load: build the full engine stack from `params->model_path`.
* On success returns VLLM_OK and stores a handle in *out (caller frees via
* vllm_engine_free). On failure returns a VLLM_ERR_* code, sets vllm_last_error(),
* and leaves *out == NULL. `params` and `out` must be non-NULL. */
VLLM_API vllm_status ;
/* Destroy an engine handle and everything it owns. NULL is a no-op. */
VLLM_API void ;
/* ── Completion (blocking) ────────────────────────────────────────────────────
* Run a single blocking completion for `prompt` with `params`, filling *out.
* Returns VLLM_OK on success (out->text is a heap string the caller frees), or a
* VLLM_ERR_* code (out is zeroed, out->text == NULL, vllm_last_error() set).
* `engine`, `prompt`, `params` and `out` must be non-NULL. */
VLLM_API vllm_status ;
/* ── Streaming completion (M3.5 Task 2) ───────────────────────────────────────
* vllm_token_callback: invoked once per engine-step delta for the streaming
* request, then once more with finished == true to carry the finish.
* - delta_text: the incremental text produced since the previous call, as a
* NUL-terminated, well-formed UTF-8 C string. It is BORROWED — valid ONLY
* for the duration of the call; copy it if you need to retain it. The final
* finished == true call may carry an empty delta_text ("").
* - finished: true on the terminal call (the request ended: EOS / stop /
* length / abort). The callback is not invoked again for this request.
* - user_data: the opaque pointer passed to vllm_complete_stream, round-tripped
* unchanged (e.g. an accumulator the callback appends to).
* RETURN false to STOP generation early: the library aborts the in-flight
* request (tears it down so the engine stays usable) and returns VLLM_OK. Return
* true to keep generating. The callback is C code and MUST NOT throw across the
* ABI (any C++ exception it raises is caught and mapped to a status). */
typedef bool ;
/* Run a single streaming completion for `prompt` with `params`, invoking `cb`
* per delta (see vllm_token_callback). BLOCKING: drives the engine loop to a
* natural finish, an early stop (cb returned false), or an error before
* returning. Returns VLLM_OK on success (including an early stop), or a
* VLLM_ERR_* code (vllm_last_error() set). `engine`, `prompt`, `params` and `cb`
* must be non-NULL; `user_data` may be NULL. Sampled generation (temperature > 0
* with a seed) is supported and deterministic for a fixed seed. */
VLLM_API vllm_status ;
/* ── Non-blocking streaming requests (async-serving W2) ─────────────────────
* Submit returns after validation/enqueue. A library-owned delivery thread
* invokes `cb` for each RequestOutput delta while the shared AsyncLLM engine
* continues batching other requests. `out` receives an owned request handle.
* The callback/user_data borrow follows vllm_complete_stream's contract.
* The engine must outlive the request handle. */
VLLM_API vllm_status ;
/* Abort an in-flight request. Idempotent; its delivery thread exits after the
* terminal abort output is consumed. */
VLLM_API vllm_status ;
/* Wait for callback delivery to finish and return its terminal status. On an
* error, vllm_last_error() on the WAITING thread receives the request error.
* Do not call wait/free from that request's own callback. */
VLLM_API vllm_status ;
/* Non-blocking completion probe (false for NULL). */
VLLM_API bool ;
/* Request-owned diagnostic string. Empty while the request is still running;
* after vllm_request_done returns true (or wait returns), valid until
* vllm_request_free. Never NULL. */
VLLM_API const char* ;
/* Cancel if needed, join the delivery thread, and destroy the handle. NULL is
* a no-op. The parent engine must still be alive. Must not be called from the
* request's own callback; use cancel there and free from another thread. */
VLLM_API void ;
/* ── Chat completions (ABI v3) ────────────────────────────────────────────────
* OpenAI-style chat entry points over the SAME engine handle. The heavy
* lifting runs ENGINE-SIDE, exactly like the bundled OpenAI server:
* - request_json is one OpenAI /v1/chat/completions request object
* (messages, tools, tool_choice, temperature, top_p, max_tokens, stop,
* ...). The `model` and `stream` fields are ignored (the handle's model
* serves; streaming is selected by the entry point).
* - The chat template is applied by the engine's renderer. It is resolved
* at vllm_engine_load: <model_dir>/tokenizer_config.json `chat_template`,
* or the GGUF `tokenizer.chat_template` metadata for a .gguf model; when
* neither exists, a plain "<role>: <content>" join is used.
* - tools + tool_choice lower to the engine's structural-tag DECODE
* constraint: `auto` is LAZY (the ENGINE decides when a tool engages —
* text is unconstrained until the model emits the tool trigger, then the
* call is grammar-constrained); `required`/named force a call; `none`
* disables. Tool-call output is parsed engine-side (streaming-stateful
* Hermes-style parser) into structured tool_calls deltas.
*
* vllm_chat: BLOCKING non-streaming completion. On VLLM_OK, *out_response_json
* is a heap NUL-terminated ChatCompletionResponse JSON object (choices with
* message.content / message.tool_calls, finish_reason "stop"/"length"/
* "tool_calls", usage) that the CALLER frees via vllm_string_free. On error,
* *out_response_json is NULL and vllm_last_error() is set (a malformed
* request_json maps to VLLM_ERR_INVALID_ARGUMENT). */
VLLM_API vllm_status ;
/* vllm_chat_stream: BLOCKING streaming completion. `cb` receives ONE OpenAI
* chat.completion.chunk JSON object per invocation in delta_text (no SSE
* framing): first the role chunk, then content and/or tool_calls delta chunks
* as the engine-side parser emits them, then the finish chunk; after the last
* chunk the callback is invoked once more with finished == true and an empty
* delta. Returning false from the callback aborts the in-flight request
* (VLLM_OK is still returned). The borrow/threading contract of
* vllm_token_callback applies unchanged. */
VLLM_API vllm_status ;
/* ── Memory helpers ───────────────────────────────────────────────────────────
* Free a heap string returned by the library. NULL is a no-op. */
VLLM_API void ;
/* Free the owned members of a completion (out->text) and zero the struct. The
* `out` struct itself is caller-provided storage and is not freed. NULL is a
* no-op. */
VLLM_API void ;
/* ── Diagnostics / versioning ─────────────────────────────────────────────────
* The last error on the CURRENT thread, as a NUL-terminated string owned by the
* library (thread-local). Never NULL (empty string if no error). Valid until the
* next C API call on the same thread; the caller must NOT free it. */
VLLM_API const char* ;
/* The library version string ("MAJOR.MINOR.PATCH[+cuda]"), static storage; do
* not free. */
VLLM_API const char* ;
/* The ABI version the library was built with (compare against VLLM_ABI_VERSION). */
VLLM_API int32_t ;
} /* extern "C" */
/* VLLM_H_ */