ai-sdk-provider icon indicating copy to clipboard operation
ai-sdk-provider copied to clipboard

Question: How is transform like middle-out used in AI SDK?

Open Kitenite opened this issue 4 months ago • 3 comments

Right now i'm passing in as providerOptions. This feels like it's wrong so would love some clarity here. Thanks! https://openrouter.ai/docs/features/message-transforms

 const result = streamText({
        model,
        headers,
        messages: [
            {
                role: 'system',
                content: systemPrompt,
                providerOptions: {openrouter: { transforms: ['middle-out'] },} ,
            },
            ...messages,
        ],
        maxTokens,
        maxSteps,

Kitenite avatar Aug 10 '25 04:08 Kitenite

This feels like it's wrong

[Update]: Oh, I see this is inside the messages block. Do the providerOptions need to be there? My brain places them outside this block, as a sibling to model and headers (transforms could also get placed into headers, right?)

Okay after checking that my brain is not wrong (it often is): The providerOptions should indeed be on the same level as model and headers, so your example should read:

 const result = streamText({
        model,
        headers,
        messages: [
            {
                role: 'system',
                content: systemPrompt,
            },
            ...messages,
        ],
        providerOptions: {openrouter: { transforms: ['middle-out'] },} ,
        maxTokens,
        maxSteps,
})

See here -> https://github.com/OpenRouterTeam/ai-sdk-provider#passing-extra-body-to-openrouter

fry69 avatar Aug 10 '25 05:08 fry69

I'm not sure. some providerOptions can be passed in messages such as caching. Is there a way to confirm from OR side if this option was enabled?

Kitenite avatar Aug 10 '25 05:08 Kitenite

It looks like the recommended way is to use OpenTelemetry tracing to check if the request/payload matches what you expect with AI SDK, see -> https://ai-sdk.dev/docs/ai-sdk-core/telemetry

fry69 avatar Aug 10 '25 05:08 fry69