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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! 2026-09-26: OpenAI `chat.completion.chunk` SSE payload types and their constructors.
//!
//! Owner: server (OpenAI API layer).
//! Invariants:
//! - Every constructor sets `object` to `chat.completion.chunk` and `system_fingerprint`
//! to `fp_metrale`.
//! - Every constructor except `usage_only_chunk` builds exactly one choice, at index 0;
//! `usage_only_chunk` builds none.
use serde::Serialize;
use super::*;
use crate::reasoning_parser::ReasoningWire;
/// 2026-09-26: One `chat.completion.chunk` SSE payload.
#[derive(Debug, Serialize)]
pub struct ChatCompletionChunk {
pub id: String,
pub object: String,
pub created: u64,
pub model: String,
pub system_fingerprint: Option<String>,
pub choices: Vec<ChunkChoice>,
#[serde(skip_serializing_if = "Option::is_none")]
pub usage: Option<Usage>,
}
#[derive(Debug, Serialize)]
pub struct ChunkChoice {
pub index: usize,
pub delta: ChunkDelta,
pub finish_reason: Option<String>,
pub logprobs: Option<ChoiceLogprobs>,
/// 2026-09-26: Sampled token ids carried by this chunk; omitted from the JSON when empty.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub token_ids: Vec<u32>,
}
#[derive(Debug, Serialize)]
pub struct ChunkDelta {
#[serde(skip_serializing_if = "Option::is_none")]
pub role: Option<String>,
/// 2026-09-26: Reasoning text of this chunk, under one key only, so the text appears
/// once per delta. 2026-10-14: the key follows the model's `ReasoningWire`.
#[serde(flatten)]
pub reasoning: WireReasoning,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tool_calls: Option<Vec<crate::tool_parser::ChunkToolCall>>,
/// 2026-09-26: Refusal sentence, set only by `refusal_chunk`.
#[serde(skip_serializing_if = "Option::is_none")]
pub refusal: Option<String>,
}
impl ChatCompletionChunk {
/// 2026-09-26: Opening chunk of a chat stream: `role: "assistant"`. 2026-10-14:
/// with `content: ""`, as vLLM and OpenAI send it.
pub fn role_chunk(model: &str, id: &str) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: Some("assistant".to_string()),
reasoning: WireReasoning::Omitted,
content: Some(String::new()),
tool_calls: None,
refusal: None,
},
finish_reason: None,
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: Delta chunk carrying only reasoning, under `wire`'s key.
pub fn reasoning_chunk(model: &str, id: &str, wire: ReasoningWire, text: String) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: None,
reasoning: WireReasoning::delta(wire, text),
content: None,
tool_calls: None,
refusal: None,
},
finish_reason: None,
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: Delta chunk carrying only `content`.
pub fn content_chunk(model: &str, id: &str, text: String) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: None,
reasoning: WireReasoning::Omitted,
content: Some(text),
tool_calls: None,
refusal: None,
},
finish_reason: None,
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: First delta of a tool call: `role: "assistant"` plus the call's id,
/// type and name, with empty arguments. `content` is omitted, not null.
pub fn tool_call_start_chunk(
model: &str,
id: &str,
tc: &crate::tool_parser::ToolCall,
tc_index: usize,
) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: Some("assistant".to_string()),
reasoning: WireReasoning::Omitted,
content: None,
tool_calls: Some(vec![crate::tool_parser::ChunkToolCall {
index: tc_index,
id: Some(tc.id.clone()),
call_type: Some(tc.call_type.clone()),
function: crate::tool_parser::ChunkFunction {
name: Some(tc.function.name.clone()),
arguments: String::new(),
},
}]),
refusal: None,
},
finish_reason: None,
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: Delta carrying one fragment of the arguments string of tool call
/// `tc_index`; id, type and name are omitted.
pub fn tool_call_args_fragment(model: &str, id: &str, tc_index: usize, fragment: &str) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: None,
reasoning: WireReasoning::Omitted,
content: None,
tool_calls: Some(vec![crate::tool_parser::ChunkToolCall {
index: tc_index,
id: None,
call_type: None,
function: crate::tool_parser::ChunkFunction {
name: None,
arguments: fragment.to_string(),
},
}]),
refusal: None,
},
finish_reason: None,
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: Final chunk with `finish_reason`, an empty delta, and usage.
pub fn done_chunk(model: &str, id: &str, finish_reason: &str, usage: Usage) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: None,
reasoning: WireReasoning::Omitted,
content: None,
tool_calls: None,
refusal: None,
},
finish_reason: Some(finish_reason.to_string()),
logprobs: None,
token_ids: Vec::new(),
}],
usage: Some(usage),
}
}
/// 2026-09-26: Chunk with `choices: []` and usage. When `stream_options.include_usage`
/// is set, the stream encoder sends it just before `final_chunk_no_usage`.
pub fn usage_only_chunk(model: &str, id: &str, usage: Usage) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: Vec::new(),
usage: Some(usage),
}
}
/// 2026-09-26: Delta chunk carrying only `refusal`. The chat stream sends it at most
/// once, just before the finish chunk(s), when `crate::refusal::detect` flags the
/// streamed text and the response made no tool call.
pub fn refusal_chunk(model: &str, id: &str, refusal: String) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: None,
reasoning: WireReasoning::Omitted,
content: None,
tool_calls: None,
refusal: Some(refusal),
},
finish_reason: None,
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: Final chunk with `finish_reason` and no `usage` key, for streams whose
/// usage went out in `usage_only_chunk`.
pub fn final_chunk_no_usage(model: &str, id: &str, finish_reason: &str) -> Self {
Self {
id: id.to_string(),
object: "chat.completion.chunk".to_string(),
created: unix_timestamp(),
model: model.to_string(),
system_fingerprint: Some("fp_metrale".to_string()),
choices: vec![ChunkChoice {
index: 0,
delta: ChunkDelta {
role: None,
reasoning: WireReasoning::Omitted,
content: None,
tool_calls: None,
refusal: None,
},
finish_reason: Some(finish_reason.to_string()),
logprobs: None,
token_ids: Vec::new(),
}],
usage: None,
}
}
/// 2026-09-26: Set `ids` as the first choice's `token_ids`. No-op when `ids` is empty or
/// the chunk has no choice.
pub(crate) fn with_token_ids(mut self, ids: Vec<u32>) -> Self {
if !ids.is_empty()
&& let Some(choice) = self.choices.first_mut()
{
choice.token_ids = ids;
}
self
}
}