Skip to content

wrapTraced works with Promise and non-Promise callbacks #1022

@nikitatome

Description

@nikitatome

The core issue is unrelated to the ai-sdk integration. The customer is attempting to use the wrapTraced with a non-Promise and wrapTraced assumes that the callback is a Promise. There's existing code that tries to handle this, but the AsyncFlush args makes this hard to clean up.


Previously:

I'm using AI SDK v5 and am trying to use wrapTraced around my streamText function. However, I'm using the createUIMessageStream functionality as well, and it appears that the wrapTraced is dropping the toUIMessageStream function, so I'm unable to use it. Our code looks similar to the following simplified example:

  const stream = createUIMessageStream({
    originalMessages,
    execute: async ({ writer }) => {
      const result = tracer.wrap(
        'streamText',
        {
          tags: {
            threadId,
          },
        },
        wrapTraced(streamText, {
          name: 'streamText',
          event: {
            input: {
              metadata: {
                threadId,
                modelName,
              },
            },
          },
        })
      )({...});

      // Transform UI message stream to restore IDs for frontend
      const uiStream = result.toUIMessageStream();
    },
  });

  pipeUIMessageStreamToResponse({
    response: res,
    stream,
  });

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions