ai icon indicating copy to clipboard operation
ai copied to clipboard

providerOptions are lost when UIMessages are included in messages passed to core functions

Open carlymr opened this issue 1 year ago • 8 comments

Description

I believe this is the root cause of at least some of the issues seen in https://github.com/vercel/ai/issues/5227 .

The logic in detectPromptType declares a prompt to be of form 'ui-messages' if any of the messages provided have "UI-specific parts." That means that if mixed message types are passed in, they are all treated as UIMessages.

That breaks this pattern, which I would expect to be a fairly common use case:

messages: [
  {
    role: 'system',
    content: systemPrompt,
    providerOptions: {
      anthropic: { cacheControl: { type: 'ephemeral' } },
    },
  },
  ...userMessages, // messages passed in from the UI
];

In that case, the providerOptions field is stripped from all messages in the list by convertToCoreMessages, which reconstructs all the messages including only role, content, and experimentalAttachments.

Here's my simple repro case:

const messages = [
      {
        role: 'user',
        content: 'Please process this message',
        providerOptions: {
          anthropic: { cacheControl: { type: 'ephemeral' } },
        },
      },
    ];

const generateResult = await generateText({
      model: anthropic('claude-3-7-sonnet-20250219');,
      messages: messages,
 });

successfully sends the following to Anthropic:

{
  role: 'user',
  content: [
    {
      type: 'text',
      text: 'Please process this message',
      cache_control: [Object]
    }
  ]
}

However this:

const messages = [
      {
        role: 'user',
        content: 'Please process this message',
        providerOptions: {
          anthropic: { cacheControl: { type: 'ephemeral' } },
        },
      },
      {
        role: 'data',
        content: 'some data',
      },
    ];

 const generateResult = await generateText({
      model: anthropic('claude-3-7-sonnet-20250219');,
      messages: messages,
});

Still sends only the first message, but without the cache_control parameter:

{
  role: 'user',
  content: [ { type: 'text', text: 'Please process this message' } ]
}

I could potentially fix this by calling convertToCoreMessages on the UIMessages before passing them to generateText (/streamText/etc), but there's not any indication that should be necessary.

Potential solutions?

  1. Core AI functions throw an error if the message array passed in contains an array of mixed message types, rather than defaulting to UIMessage processing, so users know they must call convertToCoreMessage on their UIMessages
  2. convertToCoreMessage leaves messages unchanged if they are already in CoreMessage form
  3. convertToCoreMessage retains providerOptions specifically

carlymr avatar Apr 01 '25 00:04 carlymr

Hey @carlymr! You can also pass provider options at the generateText level as a workaround

iteratetograceness avatar Apr 08 '25 06:04 iteratetograceness

I don't think that works for anthropic cacheControl, since that needs to be specified at the message level (since it's specifically setting a cache breakpoint on that message).

carlymr avatar Apr 08 '25 17:04 carlymr

@carlymr ah, thank you for correcting me! We decided to update our docs to better inform users on passing in uniform message types.

For now, you should resolve the issue by using convertToCoreMessages on UIMessage array before appending additional core messages that include the provider options!

iteratetograceness avatar Apr 08 '25 18:04 iteratetograceness

Thank you! I did do that workaround (calling convertToCoreMessage on the UI messages first), and that did resolve my issue.

I'm a little concerned about resolving this with a documentation update for a couple of reasons:

  1. The cacheControl pattern above is likely to be pretty frequent - it's the natural way to cache a large system prompt and then continue to append new user messages as a conversation progresses, which is the most common use case for Anthropic caching.
  2. The failure case was very obscure - it look me the better part of a week to pin down what was happening, because the only thing that breaks is anthropic's caching, which doesn't cause any errors, and it only breaks when there are user messages with UI-specific parts appended (e.g. tool calling), so in my case it actually worked on the first turn of the conversation and then broke afterwards.

At the very least, the documentation on anthropic prompt caching (and any other message-level providerOptions - open AI image detail?) should probably be very explicit on this case in particular, since I don't think it would be clear to a user that passing in mixed message types could silently fail in this way.

carlymr avatar Apr 08 '25 19:04 carlymr

@carlymr I hear you! I opted out of throwing because I didn't want to introduce a breaking change for users. Open to your thoughts though!

At the very least, I will add a warning note in the Anthropic prompt caching + OAI image detail docs!

iteratetograceness avatar Apr 08 '25 20:04 iteratetograceness

The fiddling between UI messages and core messages is quite painful. Ideally, I don't want to think about this distinction. Why can't I add provider options to UI messages so they're automatically converted to core messages? Currently, I need to manage this conversion manually, which adds unnecessary overhead to my workflow.

petrbrzek avatar Apr 11 '25 09:04 petrbrzek

@petrbrzek appreciate your feedback! Will take it into consideration as we prepare for an upcoming major release.

iteratetograceness avatar Apr 14 '25 14:04 iteratetograceness

Identify issue type

Identified as Bug
Confidence: High

Rationale

This reports a concrete silent failure in current SDK behavior: valid message-level provider options are lost during prompt conversion, producing an incorrect provider request. The planned documentation warning and available workaround do not negate the reported behavioral defect.

Evidence

  • Mixed UIMessage and CoreMessage inputs are detected as UI messages and converted, silently dropping message-level providerOptions.
  • The reproduction shows Anthropic cache_control is sent for a core-only prompt but disappears after adding a UI-specific data message.
  • Manually converting UI messages before appending core messages restores the expected cache-control behavior.

lgrammel avatar Jul 22 '26 15:07 lgrammel