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
//! Streaming types for AI generation
use super::{FinishReason, Usage};
use crate::error::Result;
use futures::Stream;
use pin_project::pin_project;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::pin::Pin;
use std::task::{Context, Poll};
#[cfg(feature = "tracing")]
use tracing::Span;
#[cfg(feature = "tracing")]
use crate::tracing as gen_ai_tracing;
/// Accumulated tool call during streaming
#[cfg(feature = "tracing")]
#[derive(Debug, Clone)]
struct AccumulatedToolCall {
id: String,
name: String,
arguments: String,
}
/// A stream of generation events
#[pin_project]
pub struct GenerateStream {
#[pin]
inner: Pin<Box<dyn Stream<Item = Result<StreamEvent>> + Send>>,
/// Optional span for tracing - usage is recorded when Finish event is received
#[cfg(feature = "tracing")]
span: Option<Span>,
/// Accumulated text content for tracing
#[cfg(feature = "tracing")]
accumulated_text: String,
/// Accumulated tool calls for tracing
#[cfg(feature = "tracing")]
accumulated_tool_calls: Vec<AccumulatedToolCall>,
}
impl GenerateStream {
/// Create a new stream from a boxed stream
pub fn new(stream: Pin<Box<dyn Stream<Item = Result<StreamEvent>> + Send>>) -> Self {
Self {
inner: stream,
#[cfg(feature = "tracing")]
span: None,
#[cfg(feature = "tracing")]
accumulated_text: String::new(),
#[cfg(feature = "tracing")]
accumulated_tool_calls: Vec::new(),
}
}
/// Create a new stream with an associated tracing span
///
/// When the stream emits a `Finish` event, token usage and response content
/// will be recorded on the span automatically.
#[cfg(feature = "tracing")]
pub fn with_span(
stream: Pin<Box<dyn Stream<Item = Result<StreamEvent>> + Send>>,
span: Span,
) -> Self {
Self {
inner: stream,
span: Some(span),
accumulated_text: String::new(),
accumulated_tool_calls: Vec::new(),
}
}
}
impl Stream for GenerateStream {
type Item = Result<StreamEvent>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.project();
let poll_result = this.inner.poll_next(cx);
#[cfg(feature = "tracing")]
if let Poll::Ready(Some(Ok(ref event))) = poll_result {
// Accumulate content for tracing
match event {
StreamEvent::TextDelta { delta, .. } => {
this.accumulated_text.push_str(delta);
}
StreamEvent::ToolCallStart { id, name } => {
this.accumulated_tool_calls.push(AccumulatedToolCall {
id: id.clone(),
name: name.clone(),
arguments: String::new(),
});
}
StreamEvent::ToolCallDelta { id, delta } => {
if let Some(tc) = this
.accumulated_tool_calls
.iter_mut()
.find(|tc| &tc.id == id)
{
tc.arguments.push_str(delta);
}
}
StreamEvent::ToolCallEnd {
id,
name,
arguments,
..
} => {
// Update the tool call with final name and arguments
if let Some(tc) = this
.accumulated_tool_calls
.iter_mut()
.find(|tc| &tc.id == id)
{
tc.name = name.clone();
tc.arguments = arguments.to_string();
} else {
// Tool call wasn't started, add it now
this.accumulated_tool_calls.push(AccumulatedToolCall {
id: id.clone(),
name: name.clone(),
arguments: arguments.to_string(),
});
}
}
StreamEvent::Finish { usage, reason } => {
// Record usage and completion on span
if let Some(span) = this.span {
let _guard = span.enter();
span.record("gen_ai.usage.input_tokens", usage.prompt_tokens as i64);
span.record("gen_ai.usage.output_tokens", usage.completion_tokens as i64);
// Non-standard: Cache token metrics (not part of OTel GenAI semantic conventions)
if let Some(cache_read) = usage.cache_read_tokens() {
span.record("gen_ai.usage.cache_read_input_tokens", cache_read as i64);
}
if let Some(cache_write) = usage.cache_write_tokens() {
span.record(
"gen_ai.usage.cache_write_input_tokens",
cache_write as i64,
);
}
// finish_reasons is an array per OTel spec
let finish_reason = format!("{:?}", reason.unified);
let finish_reasons_json =
serde_json::to_string(&vec![&finish_reason]).unwrap_or_default();
span.record(
"gen_ai.response.finish_reasons",
finish_reasons_json.as_str(),
);
// Record response content as span attribute
let tool_calls: Vec<gen_ai_tracing::ToolCallInfo> = this
.accumulated_tool_calls
.iter()
.map(|tc| gen_ai_tracing::ToolCallInfo {
id: tc.id.clone(),
name: tc.name.clone(),
arguments: tc.arguments.clone(),
})
.collect();
gen_ai_tracing::record_streamed_response(
this.accumulated_text,
&tool_calls,
&finish_reason,
);
}
}
_ => {}
}
}
poll_result
}
}
/// Events emitted during streaming generation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum StreamEvent {
/// Stream started
Start {
/// Unique ID for this generation
id: String,
},
/// Text content delta
TextDelta {
/// Generation ID
id: String,
/// Text delta to append
delta: String,
},
/// Reasoning content delta (extended thinking for Anthropic, reasoning for OpenAI)
ReasoningDelta {
/// Generation ID
id: String,
/// Reasoning delta to append
delta: String,
},
/// Tool call started
ToolCallStart {
/// Tool call ID
id: String,
/// Function name
name: String,
},
/// Tool call arguments delta
ToolCallDelta {
/// Tool call ID
id: String,
/// Arguments delta (partial JSON)
delta: String,
},
/// Tool call completed
ToolCallEnd {
/// Tool call ID
id: String,
/// Complete function name
name: String,
/// Complete arguments as JSON
arguments: Value,
/// Opaque provider-specific metadata (e.g., Gemini thought_signature)
#[serde(skip_serializing_if = "Option::is_none")]
metadata: Option<Value>,
},
/// Generation finished
Finish {
/// Token usage
usage: Usage,
/// Why it finished
reason: FinishReason,
},
/// Error occurred
Error {
/// Error message
message: String,
},
}
impl StreamEvent {
/// Create a start event
pub fn start(id: impl Into<String>) -> Self {
Self::Start { id: id.into() }
}
/// Create a text delta event
pub fn text_delta(id: impl Into<String>, delta: impl Into<String>) -> Self {
Self::TextDelta {
id: id.into(),
delta: delta.into(),
}
}
/// Create a reasoning delta event (extended thinking for Anthropic, reasoning for OpenAI)
pub fn reasoning_delta(id: impl Into<String>, delta: impl Into<String>) -> Self {
Self::ReasoningDelta {
id: id.into(),
delta: delta.into(),
}
}
/// Create a tool call start event
pub fn tool_call_start(id: impl Into<String>, name: impl Into<String>) -> Self {
Self::ToolCallStart {
id: id.into(),
name: name.into(),
}
}
/// Create a tool call delta event
pub fn tool_call_delta(id: impl Into<String>, delta: impl Into<String>) -> Self {
Self::ToolCallDelta {
id: id.into(),
delta: delta.into(),
}
}
/// Create a tool call end event
pub fn tool_call_end(id: impl Into<String>, name: impl Into<String>, arguments: Value) -> Self {
Self::ToolCallEnd {
id: id.into(),
name: name.into(),
arguments,
metadata: None,
}
}
/// Create a tool call end event with metadata
pub fn tool_call_end_with_metadata(
id: impl Into<String>,
name: impl Into<String>,
arguments: Value,
metadata: Option<Value>,
) -> Self {
Self::ToolCallEnd {
id: id.into(),
name: name.into(),
arguments,
metadata,
}
}
/// Create a finish event
pub fn finish(usage: Usage, reason: FinishReason) -> Self {
Self::Finish { usage, reason }
}
/// Create an error event
pub fn error(message: impl Into<String>) -> Self {
Self::Error {
message: message.into(),
}
}
}