Next.js + Vercel AI SDK + Langfuse: How to get observationId
(responseId) for langfuse scores?
#5415
Replies: 4 comments 1 reply
-
I'm having the same issue as OP. We should be able to tie this into an observation ID that we want a score for. Eg. The observation/span ID from ai.streamText.doStream |
Beta Was this translation helpful? Give feedback.
-
One possible work-around that I have right now is creating a trace.generation object, taking it's ID and passing that to dataStream.writeMessageAnnotation. Extracting that ID from messages, storing that in my global storage and then using that trace.generation ID in my feedback component so that a score gets attached to it. It's not a clean work-around, but you'll be able to have feedback on a trace.generation which happens right before the span. So for example, doing it as I described you'd end up with something like this - which is not ideal - but it's better than nothing.
Code would look something like:
Hope that might be a little helpful for your use case. |
Beta Was this translation helpful? Give feedback.
-
HI @Josh-Zirena, I did some more research and analysed the logs. This is what I came up so far: I've played with the // instrumentation.ts
registerOTel({
serviceName: 'your-app',
idGenerator: {
// default: random 16 chars
generateTraceId: () => uuidv4(),
// default: random 16 chars
generateSpanId: () => uuidv4(),
},
traceExporter: new LangfuseExporter({
debug: true
})
}) If you then send a message to the API the logs with debugging enabled in [1] name: 'ai.streamText.doStream',
[1] _spanContext: {
[1] traceId: '875566be-5491-40ad-a842-648dcc279578',
[1] spanId: 'bde24164-3729-4627-b5a5-3e4a00c7641f',
[1] traceFlags: 1,
[1] traceState: undefined
[1] },
[1] parentSpanId: undefined, This ids and (some others visible in the debug logs) are then used here
This id The problem, as you can see in the screenshot) is that the generation is no child of the span anymore: Span [cd-chat-message-75]
Generation [ai.streamText.doStream] instead of - Span [cd-chat-message-75]
- Generation [ai.streamText.doStream] Which is what I still would expect to have based on the given langfuse data model (but it's not a requirement according to the docs). Besides that the real and still existing problem is how to access the Using this exposed a different import { trace, context, type Span } from '@opentelemetry/api'
// e.g. in `onFinish` of `streamText`
const activeSpan: Span | undefined = trace.getSpan(context.active())
const activeSpanContext = activeSpan?.spanContext()
console.log('activeSpanContext', activeSpanContext) This is what I get [1] activeSpanContext {
[1] traceId: '09f31b10-4ecc-4384-92df-4339ea164a73',
[1] spanId: 'ccbf35e2-0a48-4db9-9c6e-08a417355278',
[1] traceFlags: 1,
[1] traceState: undefined
[1] } Which corresponds to the following debug output of name: 'executing api route (app) /(chat)/api/chat/route',
[1] _spanContext: {
[1] traceId: '09f31b10-4ecc-4384-92df-4339ea164a73',
[1] spanId: 'ccbf35e2-0a48-4db9-9c6e-08a417355278',
[1] traceFlags: 1,
[1] traceState: undefined
[1] },
[1] parentSpanId: undefined, Maybe I am on the right track but there's something I do not see. 🤔 Further more Gemini pointed out that it is difficult to access all active spans within the application code aka the request were are working with, see also open-telemetry/opentelemetry-js-api#167 and https://stackoverflow.com/questions/72973140/get-all-child-spans-associated-with-opentelemetry-trace/73058566 ... Btw: your solution is similar to mine, but I send the manually created |
Beta Was this translation helpful? Give feedback.
-
I think I got something to work. See https://github.com/orgs/langfuse/discussions/6210#discussioncomment-14312688. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Dear vercel/ai-Community,
I need your help please, as I do not know how to obtain the
observationId
and attach it to ascore
in langfuse.Note: originally asked in the langfuse community, but they sent me here with the following hint: "(...) you'd need the SDK to expose the span ids of each of the requests." (src)
Context: this is how my
experimental_telemetry
looks like:The docs state
But how and where do I get the observationId from, taking into account that I use
streamText
(https://sdk.vercel.ai/docs/reference/ai-sdk-core/stream-text)Thank you very much for your time and help!
Beta Was this translation helpful? Give feedback.
All reactions