dynamo-llm 1.6.2

Dynamo LLM Library
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
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use serde::{Deserialize, Serialize};
use std::ops::Deref;

pub use super::FinishReason;
pub use super::preprocessor::PreprocessedRequest;
use crate::protocols::TokenIdType;
use dynamo_protocols::types::CompletionUsage;
use dynamo_protocols::types::StopReason;
use dynamo_runtime::error::DynamoError;
use dynamo_runtime::protocols::maybe_error::MaybeError;

pub type TokenType = Option<String>;
pub type LogProbs = Vec<f64>;

/// In-process decoder state captured on a streamed output for a possible migration retry.
///
/// The checkpoint deliberately retains the historical wire representation of
/// `jailed_text`: serializers see only the withheld string, while pending token IDs are
/// skipped and therefore survive only when the value moves through the frontend in memory.
/// Deserializing a legacy string restores the text and starts with no pending token IDs.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)]
#[serde(transparent)]
pub struct DecoderCheckpoint {
    jailed_text: String,
    #[serde(skip)]
    pending_token_ids: Vec<TokenIdType>,
}

impl DecoderCheckpoint {
    /// Captures withheld stop-filter text and token IDs buffered by incremental decoding.
    pub(crate) fn new(jailed_text: String, pending_token_ids: Vec<TokenIdType>) -> Self {
        Self {
            jailed_text,
            pending_token_ids,
        }
    }

    /// Splits the checkpoint into its stop-filter text and incremental-decoder state.
    pub(crate) fn into_parts(self) -> (String, Vec<TokenIdType>) {
        (self.jailed_text, self.pending_token_ids)
    }
}

impl Deref for DecoderCheckpoint {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        &self.jailed_text
    }
}

impl From<String> for DecoderCheckpoint {
    fn from(jailed_text: String) -> Self {
        Self::new(jailed_text, Vec::new())
    }
}

/// Per-position prompt logprob entry reported by an engine adapter.
#[derive(Serialize, Deserialize, utoipa::ToSchema, Debug, Clone, PartialEq)]
pub struct PromptLogprobEntry {
    pub logprob: f32,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub rank: Option<u32>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub decoded_token: Option<String>,
}

/// Per-token map of `token_id -> PromptLogprobEntry`. The first position
/// is `None` (no logprob exists for BOS / the very first prompt token).
pub type PromptLogprobs = Vec<Option<std::collections::HashMap<TokenIdType, PromptLogprobEntry>>>;

/// Output type discriminator for different modalities
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum OutputType {
    #[default]
    Text,
    Image,
    Video,
    Audio,
}

/// Image URL data for responses
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ImageUrlData {
    pub url: String,
}

/// Video URL data for responses
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct VideoUrlData {
    pub url: String,
}

/// Audio URL data for responses
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct AudioUrlData {
    pub url: String,
}

/// Content part for multimodal outputs (internal representation)
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentPart {
    Text { text: String },
    ImageUrl { image_url: ImageUrlData },
    VideoUrl { video_url: VideoUrlData },
    AudioUrl { audio_url: AudioUrlData },
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct TopLogprob {
    pub rank: u32,
    pub token_id: TokenIdType,
    pub token: TokenType,
    pub logprob: f64,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bytes: Option<Vec<u8>>,
}
pub type TopLogprobs = Vec<Vec<TopLogprob>>; // num_tokens x top_logprobs

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct BackendOutput {
    /// New token_ids generated from the LLM Engine
    pub token_ids: Vec<TokenIdType>,

    /// Unlike [`LLMEngineOutput::tokens`], this is a vector of tokens, not an optional.
    /// The size of this vector should be the same as the size of `token_ids`.
    pub tokens: Vec<TokenType>,

    /// Decoded text from the list tokens.
    pub text: Option<String>,

    /// Optional cumulative log probabilities
    pub cum_log_probs: Option<f64>,

    /// Optional log probabilities
    pub log_probs: Option<LogProbs>,

    pub top_logprobs: Option<TopLogprobs>,

    // TODO: Enrich this with more information as can apply our first-level postprocessing
    // logic and return more detailed information
    pub finish_reason: Option<FinishReason>,

    /// The stop string or token that triggered the stop condition.
    /// This is set when finish_reason is Stop and identifies what triggered it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<StopReason>,

    // Model Deployment Card checksum
    //pub mdcsum: String,

    // Index field for batch requests to match OpenAI format
    pub index: Option<u32>,

    // Token usage information
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completion_usage: Option<CompletionUsage>,

    /// Disaggregated execution parameters (for prefill/decode separation).
    /// Engine-owned payload — backends pack their own KV-transfer format
    /// here (vLLM `kv_transfer_params`, SGLang bootstrap triple, TRT-LLM
    /// encoded `LlmDisaggregatedParams`). Dynamo does NOT inject framework
    /// metadata into this field — use `worker_trace_link` instead.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub disaggregated_params: Option<serde_json::Value>,

    /// Multimodal encoder handoff payload (object-only by contract).
    /// Set by Encode workers on their terminal chunk; consumed by the
    /// frontend and threaded onto the downstream PreprocessedRequest.
    /// Engine-opaque; framework does not inspect.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub encoder_result: Option<serde_json::Value>,

    /// Framework-owned link to the prefill worker's span. Propagated
    /// alongside the engine's `disaggregated_params` so the decode worker
    /// can record an OTel `Link` on its `engine.generate` span. Engines
    /// should NOT read or write this field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub worker_trace_link: Option<crate::protocols::common::preprocessor::TraceLink>,

    /// Opaque engine data passed through from the backend worker to the response.
    /// Dynamo does not inspect this field; it is serialized as-is into `nvext.engine_data`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub engine_data: Option<serde_json::Value>,

    /// Router-computed data handed back to the frontend (e.g. per-request timing from
    /// a standalone router) so it joins this request's trace/metrics. Dynamo-internal,
    /// consumed by the frontend and not surfaced to clients. See [`RoutingData`](crate::protocols::common::timing::RoutingData).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub routing_data: Option<crate::protocols::common::timing::RoutingData>,

    /// Frontend-only decoder checkpoint for a possible migration retry. Its serialized
    /// representation remains the historical optional `jailed_text` string; pending token
    /// IDs are in-memory-only and let the retry reconstruct incremental decoding state.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jailed_text: Option<DecoderCheckpoint>,
}

/// The LLM engine and backnd with manage it's own state, specifically translating how a
/// given request/slot is managed on that particular backend.
///
/// For nvLLM's purpose, it has a single tracable request_id as part of it's context that
/// has propaged through the service pipeline to the backend.
///
/// This is the minimal raw output from the LLM engine. The Backend may then apply multiple
/// levels of post-processing before the BackendOutput is returns
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
pub struct LLMEngineOutput {
    // new token_ids
    pub token_ids: Vec<TokenIdType>,

    /// If the LLM Engine performs the detokenization, then this will have a Some of the detokenized
    /// text/tokens. If this value is None, then the Backend is responsible for detokenization.
    pub tokens: Option<Vec<TokenType>>,

    // decoded text -
    pub text: Option<String>,

    /// Output type discriminator (text, image, video, audio)
    #[serde(default)]
    pub output_type: OutputType,

    /// Multimodal content parts (for non-text outputs)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_parts: Option<Vec<ContentPart>>,

    /// cumulative log probabilities
    pub cum_log_probs: Option<f64>,

    /// Optional log probabilities
    pub log_probs: Option<LogProbs>,

    pub top_logprobs: Option<TopLogprobs>,

    // TODO: Enrich this with more information as can apply our first-level postprocessing
    // logic and return more detailed information
    pub finish_reason: Option<FinishReason>,

    /// The stop string or token that triggered the stop condition.
    /// This is set when finish_reason is Stop and identifies what triggered it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stop_reason: Option<StopReason>,

    // Index field for batch requests to match OpenAI format
    pub index: Option<u32>,

    /// Disaggregated execution parameters (for prefill/decode separation)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub disaggregated_params: Option<serde_json::Value>,

    /// Multimodal encoder handoff payload (object-only by contract).
    /// Set by Encode workers on their terminal chunk via
    /// `LLMEngineOutput::encode_terminal`; the post-processor copies it
    /// through to `BackendOutput.encoder_result`. Engine-opaque payload;
    /// framework does not inspect or mutate.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub encoder_result: Option<serde_json::Value>,

    /// Framework-owned link to the prefill worker's span (cross-process
    /// trace linking on disagg requests). Set by the framework on the
    /// prefill terminal chunk; consumed by the decode adapter. Engines
    /// should NOT read or write this field.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub worker_trace_link: Option<crate::protocols::common::preprocessor::TraceLink>,

    /// Additional arguments for extensibility
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub extra_args: Option<serde_json::Value>,

    // Token usage information
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub completion_usage: Option<CompletionUsage>,

    /// Opaque engine data passed through from the backend worker to the response.
    /// Dynamo does not inspect this field; it is serialized as-is into `nvext.engine_data`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub engine_data: Option<serde_json::Value>,

    /// Router-computed data handed back to the frontend (e.g. standalone-router timing).
    /// Dynamo-internal; consumed by the frontend. See [`RoutingData`](crate::protocols::common::timing::RoutingData).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub routing_data: Option<crate::protocols::common::timing::RoutingData>,

    /// Frontend-only decoder checkpoint for a possible migration retry. Engines never set
    /// it; `Backend` populates it after worker deserialization. The legacy optional-string
    /// wire shape is retained, while pending token IDs are kept in memory only.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub jailed_text: Option<DecoderCheckpoint>,
}

impl LLMEngineOutput {
    pub fn cancelled() -> Self {
        LLMEngineOutput {
            token_ids: vec![],
            tokens: None,
            text: None,
            output_type: OutputType::default(),
            content_parts: None,
            cum_log_probs: None,
            log_probs: None,
            top_logprobs: None,
            finish_reason: Some(FinishReason::Cancelled),
            stop_reason: None,
            index: None,
            disaggregated_params: None,
            encoder_result: None,
            worker_trace_link: None,
            extra_args: None,
            completion_usage: None,
            engine_data: None,
            routing_data: None,
            jailed_text: None,
        }
    }

    pub fn stop() -> Self {
        LLMEngineOutput {
            token_ids: vec![],
            tokens: None,
            text: None,
            output_type: OutputType::default(),
            content_parts: None,
            cum_log_probs: None,
            log_probs: None,
            finish_reason: Some(FinishReason::Stop),
            stop_reason: None,
            top_logprobs: None,
            index: None,
            disaggregated_params: None,
            encoder_result: None,
            worker_trace_link: None,
            extra_args: None,
            completion_usage: None,
            engine_data: None,
            routing_data: None,
            jailed_text: None,
        }
    }

    pub fn length() -> Self {
        LLMEngineOutput {
            token_ids: vec![],
            tokens: None,
            text: None,
            output_type: OutputType::default(),
            content_parts: None,
            cum_log_probs: None,
            log_probs: None,
            top_logprobs: None,
            finish_reason: Some(FinishReason::Length),
            stop_reason: None,
            index: None,
            disaggregated_params: None,
            encoder_result: None,
            worker_trace_link: None,
            extra_args: None,
            completion_usage: None,
            engine_data: None,
            routing_data: None,
            jailed_text: None,
        }
    }

    pub fn error(err_msg: String) -> Self {
        LLMEngineOutput {
            token_ids: vec![],
            tokens: None,
            text: None,
            output_type: OutputType::default(),
            content_parts: None,
            cum_log_probs: None,
            log_probs: None,
            top_logprobs: None,
            finish_reason: Some(FinishReason::Error(err_msg)),
            stop_reason: None,
            index: None,
            disaggregated_params: None,
            encoder_result: None,
            worker_trace_link: None,
            extra_args: None,
            completion_usage: None,
            engine_data: None,
            routing_data: None,
            jailed_text: None,
        }
    }

    /// Terminal chunk for an Encode-mode stream. The `encoder_result`
    /// payload is the engine-opaque handoff dict the downstream
    /// Prefill/Aggregated worker will receive on its
    /// `PreprocessedRequest.encoder_result`.
    ///
    /// Signature takes `serde_json::Map<String, serde_json::Value>` (not
    /// bare `Value`) so the object-only Wire Shape invariant is
    /// type-enforced and the constructor stays infallible -- there is no
    /// way to pass an array or scalar through this API. The constructor
    /// wraps the `Map` in `Value::Object(...)` internally.
    ///
    /// `index: Some(0)` matches the Python helper `encoder_terminal_chunk`
    /// so Rust and Python producers emit byte-identical terminals for the
    /// same `encoder_result`.
    pub fn encode_terminal(encoder_result: serde_json::Map<String, serde_json::Value>) -> Self {
        LLMEngineOutput {
            token_ids: vec![],
            tokens: None,
            text: None,
            output_type: OutputType::default(),
            content_parts: None,
            cum_log_probs: None,
            log_probs: None,
            top_logprobs: None,
            finish_reason: Some(FinishReason::Stop),
            stop_reason: None,
            index: Some(0),
            disaggregated_params: None,
            encoder_result: Some(serde_json::Value::Object(encoder_result)),
            worker_trace_link: None,
            extra_args: None,
            completion_usage: None,
            engine_data: None,
            routing_data: None,
            jailed_text: None,
        }
    }
}

pub(crate) fn prompt_logprobs_from_engine_data(
    engine_data: Option<&serde_json::Value>,
) -> anyhow::Result<Option<PromptLogprobs>> {
    let Some(value) = engine_data.and_then(|data| data.get("prompt_logprobs")) else {
        return Ok(None);
    };
    serde_json::from_value(value.clone())
        .map(Some)
        .map_err(|error| anyhow::anyhow!("invalid prompt_logprobs payload: {error}"))
}

impl MaybeError for LLMEngineOutput {
    fn from_err(err: impl std::error::Error + 'static) -> Self {
        LLMEngineOutput::error(err.to_string())
    }

    fn err(&self) -> Option<DynamoError> {
        if let Some(FinishReason::Error(err_msg)) = &self.finish_reason {
            Some(DynamoError::msg(err_msg.clone()))
        } else {
            None
        }
    }
}

/// Raw output from embedding engines containing base64-encoded float32 vectors.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct EmbeddingsEngineOutput {
    /// One standard-base64 string per input embedding.
    pub embeddings: Vec<String>,

    /// Token usage information
    pub prompt_tokens: u32,
    pub total_tokens: u32,
}

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

    fn checkpoint_output(checkpoint: Option<DecoderCheckpoint>) -> BackendOutput {
        BackendOutput {
            token_ids: vec![],
            tokens: vec![],
            text: None,
            cum_log_probs: None,
            log_probs: None,
            top_logprobs: None,
            finish_reason: None,
            stop_reason: None,
            index: None,
            completion_usage: None,
            disaggregated_params: None,
            encoder_result: None,
            worker_trace_link: None,
            engine_data: None,
            routing_data: None,
            jailed_text: checkpoint,
        }
    }

    /// Decoder checkpoints retain the legacy optional-string wire shape while keeping
    /// pending token IDs strictly in process.
    #[test]
    fn decoder_checkpoint_preserves_legacy_wire_shape() {
        let absent = serde_json::to_value(checkpoint_output(None)).unwrap();
        assert!(absent.get("jailed_text").is_none());

        let mut null = absent.clone();
        null.as_object_mut()
            .unwrap()
            .insert("jailed_text".to_string(), serde_json::Value::Null);
        let decoded_null: BackendOutput = serde_json::from_value(null).unwrap();
        assert_eq!(decoded_null.jailed_text, None);

        let mut legacy = absent;
        legacy.as_object_mut().unwrap().insert(
            "jailed_text".to_string(),
            serde_json::Value::String("ST".to_string()),
        );
        let decoded_legacy: BackendOutput = serde_json::from_value(legacy).unwrap();
        let checkpoint = decoded_legacy.jailed_text.unwrap();
        assert_eq!(&*checkpoint, "ST");
        assert!(checkpoint.into_parts().1.is_empty());

        let populated = serde_json::to_value(checkpoint_output(Some(DecoderCheckpoint::new(
            "ST".to_string(),
            vec![195],
        ))))
        .unwrap();
        assert_eq!(populated.get("jailed_text").unwrap(), "ST");
        assert!(
            !populated.to_string().contains("195"),
            "pending decoder IDs must not leak onto the wire"
        );
    }

    #[test]
    fn test_maybe_error() {
        let output = LLMEngineOutput::stop();
        assert!(output.err().is_none());
        assert!(output.is_ok());
        assert!(!output.is_err());

        let output = LLMEngineOutput::error("Test error".to_string());
        assert!(format!("{}", output.err().unwrap()).contains("Test error"));
        assert!(!output.is_ok());
        assert!(output.is_err());
    }

    /// `encode_terminal` produces an Encode-mode terminal chunk with the
    /// exact field shape required by the Wire Shape contract:
    /// empty token_ids, FinishReason::Stop, index = Some(0), and the
    /// encoder_result wrapped as `Value::Object(_)` (object-only by type).
    #[test]
    fn encode_terminal_pins_terminal_chunk_shape() {
        let payload = serde_json::json!({
            "embedding_handle": {"uri": "nixl://encoder/0", "shape": [1, 1024]},
        });
        let map = payload.as_object().unwrap().clone();
        let chunk = LLMEngineOutput::encode_terminal(map);

        assert!(chunk.token_ids.is_empty(), "encode terminal has no tokens");
        assert_eq!(chunk.finish_reason, Some(FinishReason::Stop));
        assert_eq!(chunk.index, Some(0));
        let result = chunk
            .encoder_result
            .as_ref()
            .expect("encoder_result must be set");
        assert!(result.is_object(), "encoder_result must be a JSON object");
        assert_eq!(result, &payload);
        // Sibling Option fields stay None on the producer terminal.
        assert!(chunk.disaggregated_params.is_none());
        assert!(chunk.worker_trace_link.is_none());
        assert!(chunk.completion_usage.is_none());
    }
}