Skip to content

Commit 5323cdb

Browse files
committed
concat stream response together
1 parent 721d706 commit 5323cdb

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/instrumentation/@google/genai.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ function recordChatCompletionMessages({
9494
})
9595

9696
// Only take the first response message and append to input messages
97+
// request.contents can be a string or an array of strings
98+
// reponse.candidates is an array of candidates (choices), we only take the first one
9799
const inputMessages = Array.isArray(request.contents) ? request.contents : [request.contents]
98100
const responseMessage = response?.candidates?.[0]?.content
99101
const messages = responseMessage !== undefined ? [...inputMessages, responseMessage] : inputMessages
@@ -130,29 +132,56 @@ function instrumentStream ({ agent, shim, request, response, segment, transactio
130132
agent.metrics.getOrCreateMetric(AI.STREAMING_DISABLED).incrementCallCount()
131133
}
132134

133-
let err
135+
let err = false
136+
let content
137+
let modelVersion
138+
let finishReason
139+
let entireMessage = ''
134140
shim.wrap(response, 'next', function wrapNext(shim, originalNext) {
135141
return async function wrappedNext(...nextArgs) {
136-
let result
142+
let result = {}
137143
try {
138144
result = await originalNext.apply(response, nextArgs)
145+
if (result?.value?.candidates?.[0]?.content) {
146+
modelVersion = result.value.modelVersion
147+
content = result.value.candidates[0].content
148+
entireMessage += content.parts[0].text
149+
}
150+
if (result?.value?.candidates?.[0]?.finishReason) {
151+
finishReason = result.value.candidates[0].finishReason
152+
}
139153
} catch (streamErr) {
140154
err = streamErr
141155
throw err
142156
} finally {
143-
// update segment duration since we want to extend the time it took to
144-
// handle the stream
157+
// update segment duration since we want to extend the
158+
// time it took to handle the stream
145159
segment.touch()
146160

147-
recordChatCompletionMessages({
148-
agent: shim.agent,
149-
shim,
150-
segment,
151-
transaction,
152-
request,
153-
response: result?.value,
154-
err
155-
})
161+
if (result?.done || err) {
162+
// result will be {value: undefined, done: true}
163+
// when the stream is done, so we need to create
164+
// a final response with the entire content
165+
//
166+
// also need to enter this block if there was an
167+
// error, so we can record it
168+
content.parts[0].text = entireMessage
169+
result.value = {
170+
candidates: [
171+
{ content, finishReason }
172+
],
173+
modelVersion
174+
}
175+
recordChatCompletionMessages({
176+
agent: shim.agent,
177+
shim,
178+
segment,
179+
transaction,
180+
request,
181+
response: result.value,
182+
err
183+
})
184+
}
156185
}
157186
return result
158187
}

lib/symbols.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ module.exports = {
2424
wrapped: Symbol('shimWrapped'),
2525
openAiHeaders: Symbol('openAiHeaders'),
2626
openAiApiKey: Symbol('openAiApiKey'),
27-
geminiApiKey: Symbol('geminiApiKey'),
2827
googleGenAiHeaders: Symbol('googleGenAiHeaders'),
2928
parentSegment: Symbol('parentSegment'),
3029
langchainRunId: Symbol('runId'),

0 commit comments

Comments
 (0)