@@ -1262,7 +1262,7 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
12621262 expect ( result [ 1 ] . content [ 0 ] ) . toEqual ( { type : "text" , text : "Answer" } )
12631263 } )
12641264
1265- test ( "does not filter for non-anthropic providers" , ( ) => {
1265+ test ( "does not filter empty-text parts inside arrays for non-anthropic providers" , ( ) => {
12661266 const openaiModel = {
12671267 ...anthropicModel ,
12681268 providerID : "openai" ,
@@ -1273,8 +1273,10 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
12731273 } ,
12741274 }
12751275
1276+ // Pass 1 (in-array text/reasoning filtering) only runs for Anthropic/Bedrock.
1277+ // The universal guard drops empty-content messages but does NOT reach inside arrays —
1278+ // so a message with content:[{text:""}] is kept (non-empty array) with its parts intact.
12761279 const msgs = [
1277- { role : "assistant" , content : "" } ,
12781280 {
12791281 role : "assistant" ,
12801282 content : [ { type : "text" , text : "" } ] ,
@@ -1283,9 +1285,9 @@ describe("ProviderTransform.message - anthropic empty content filtering", () =>
12831285
12841286 const result = ProviderTransform . message ( msgs , openaiModel , { } )
12851287
1286- expect ( result ) . toHaveLength ( 2 )
1287- expect ( result [ 0 ] . content ) . toBe ( "" )
1288- expect ( result [ 1 ] . content ) . toHaveLength ( 1 )
1288+ expect ( result ) . toHaveLength ( 1 )
1289+ expect ( result [ 0 ] . content ) . toHaveLength ( 1 )
1290+ expect ( result [ 0 ] . content [ 0 ] ) . toMatchObject ( { type : "text" , text : "" } )
12891291 } )
12901292
12911293 test ( "splits anthropic assistant messages when text trails tool calls" , ( ) => {
@@ -3113,3 +3115,327 @@ describe("ProviderTransform.variants", () => {
31133115 } )
31143116 } )
31153117} )
3118+
3119+ describe ( "ProviderTransform.message - universal empty-content guard" , ( ) => {
3120+ const openaiModel = {
3121+ id : "openai/gpt-4o" ,
3122+ providerID : "openai" ,
3123+ api : {
3124+ id : "gpt-4o" ,
3125+ url : "https://api.openai.com" ,
3126+ npm : "@ai-sdk/openai" ,
3127+ } ,
3128+ name : "GPT-4o" ,
3129+ capabilities : {
3130+ temperature : true ,
3131+ reasoning : false ,
3132+ attachment : true ,
3133+ toolcall : true ,
3134+ input : { text : true , audio : false , image : true , video : false , pdf : false } ,
3135+ output : { text : true , audio : false , image : false , video : false , pdf : false } ,
3136+ interleaved : false ,
3137+ } ,
3138+ cost : { input : 0.005 , output : 0.015 , cache : { read : 0.0005 , write : 0.001 } } ,
3139+ limit : { context : 128000 , output : 4096 } ,
3140+ status : "active" ,
3141+ options : { } ,
3142+ headers : { } ,
3143+ } as any
3144+
3145+ const anthropicModel = {
3146+ id : "anthropic/claude-3-5-sonnet" ,
3147+ providerID : "anthropic" ,
3148+ api : {
3149+ id : "claude-3-5-sonnet-20241022" ,
3150+ url : "https://api.anthropic.com" ,
3151+ npm : "@ai-sdk/anthropic" ,
3152+ } ,
3153+ name : "Claude 3.5 Sonnet" ,
3154+ capabilities : {
3155+ temperature : true ,
3156+ reasoning : false ,
3157+ attachment : true ,
3158+ toolcall : true ,
3159+ input : { text : true , audio : false , image : true , video : false , pdf : true } ,
3160+ output : { text : true , audio : false , image : false , video : false , pdf : false } ,
3161+ interleaved : false ,
3162+ } ,
3163+ cost : { input : 0.003 , output : 0.015 , cache : { read : 0.0003 , write : 0.00375 } } ,
3164+ limit : { context : 200000 , output : 8192 } ,
3165+ status : "active" ,
3166+ options : { } ,
3167+ headers : { } ,
3168+ } as any
3169+
3170+ const bedrockModel = {
3171+ id : "amazon-bedrock/anthropic.claude-3-5-sonnet" ,
3172+ providerID : "amazon-bedrock" ,
3173+ api : {
3174+ id : "anthropic.claude-3-5-sonnet-20241022-v2:0" ,
3175+ url : "https://bedrock-runtime.us-east-1.amazonaws.com" ,
3176+ npm : "@ai-sdk/amazon-bedrock" ,
3177+ } ,
3178+ name : "Claude 3.5 Sonnet (Bedrock)" ,
3179+ capabilities : {
3180+ temperature : true ,
3181+ reasoning : false ,
3182+ attachment : true ,
3183+ toolcall : true ,
3184+ input : { text : true , audio : false , image : true , video : false , pdf : true } ,
3185+ output : { text : true , audio : false , image : false , video : false , pdf : false } ,
3186+ interleaved : false ,
3187+ } ,
3188+ cost : { input : 0.003 , output : 0.015 , cache : { read : 0.0003 , write : 0.00375 } } ,
3189+ limit : { context : 200000 , output : 8192 } ,
3190+ status : "active" ,
3191+ options : { } ,
3192+ headers : { } ,
3193+ } as any
3194+
3195+ test ( "drops assistant message with empty string content (non-Anthropic)" , ( ) => {
3196+ const msgs = [
3197+ { role : "user" , content : "Hello" } ,
3198+ { role : "assistant" , content : "" } ,
3199+ { role : "user" , content : "World" } ,
3200+ ] as any [ ]
3201+
3202+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3203+
3204+ expect ( result ) . toHaveLength ( 2 )
3205+ expect ( result [ 0 ] . content ) . toBe ( "Hello" )
3206+ expect ( result [ 1 ] . content ) . toBe ( "World" )
3207+ } )
3208+
3209+ test ( "drops assistant message with empty array content (non-Anthropic)" , ( ) => {
3210+ const msgs = [
3211+ { role : "user" , content : "Hello" } ,
3212+ { role : "assistant" , content : [ ] } ,
3213+ { role : "user" , content : "World" } ,
3214+ ] as any [ ]
3215+
3216+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3217+
3218+ expect ( result ) . toHaveLength ( 2 )
3219+ expect ( result [ 0 ] . content ) . toBe ( "Hello" )
3220+ expect ( result [ 1 ] . content ) . toBe ( "World" )
3221+ } )
3222+
3223+ test ( "drops tool message with empty array content" , ( ) => {
3224+ const msgs = [
3225+ { role : "user" , content : "Hello" } ,
3226+ { role : "tool" , content : [ ] } ,
3227+ { role : "user" , content : "World" } ,
3228+ ] as any [ ]
3229+
3230+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3231+
3232+ expect ( result ) . toHaveLength ( 2 )
3233+ expect ( result [ 0 ] . content ) . toBe ( "Hello" )
3234+ expect ( result [ 1 ] . content ) . toBe ( "World" )
3235+ } )
3236+
3237+ test ( "drops system message with empty string content" , ( ) => {
3238+ const msgs = [
3239+ { role : "system" , content : "" } ,
3240+ { role : "user" , content : "Hello" } ,
3241+ ] as any [ ]
3242+
3243+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3244+
3245+ expect ( result ) . toHaveLength ( 1 )
3246+ expect ( result [ 0 ] . content ) . toBe ( "Hello" )
3247+ } )
3248+
3249+ test ( "drops user message with empty array content" , ( ) => {
3250+ const msgs = [
3251+ { role : "user" , content : [ ] } ,
3252+ { role : "assistant" , content : "Hello" } ,
3253+ ] as any [ ]
3254+
3255+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3256+
3257+ expect ( result ) . toHaveLength ( 1 )
3258+ expect ( result [ 0 ] . content ) . toBe ( "Hello" )
3259+ } )
3260+
3261+ test ( "preserves messages with non-empty string content" , ( ) => {
3262+ const msgs = [
3263+ { role : "user" , content : "Hello" } ,
3264+ { role : "assistant" , content : "World" } ,
3265+ ] as any [ ]
3266+
3267+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3268+
3269+ expect ( result ) . toHaveLength ( 2 )
3270+ expect ( result [ 0 ] . content ) . toBe ( "Hello" )
3271+ expect ( result [ 1 ] . content ) . toBe ( "World" )
3272+ } )
3273+
3274+ test ( "preserves messages with non-empty array content" , ( ) => {
3275+ const msgs = [
3276+ {
3277+ role : "user" ,
3278+ content : [ { type : "text" , text : "Hello" } ] ,
3279+ } ,
3280+ ] as any [ ]
3281+
3282+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3283+
3284+ expect ( result ) . toHaveLength ( 1 )
3285+ expect ( result [ 0 ] . content ) . toHaveLength ( 1 )
3286+ expect ( result [ 0 ] . content [ 0 ] ) . toEqual ( { type : "text" , text : "Hello" } )
3287+ } )
3288+
3289+ test ( "preserves messages with empty-text parts (guard does not reach inside arrays)" , ( ) => {
3290+ const msgs = [
3291+ {
3292+ role : "assistant" ,
3293+ content : [ { type : "text" , text : "" } ] ,
3294+ } ,
3295+ ] as any [ ]
3296+
3297+ // Non-Anthropic provider: Pass 1 does not run, array has 1 part -> not empty at message level
3298+ const result = ProviderTransform . message ( msgs , openaiModel , { } )
3299+
3300+ expect ( result ) . toHaveLength ( 1 )
3301+ expect ( result [ 0 ] . content ) . toHaveLength ( 1 )
3302+ expect ( result [ 0 ] . content [ 0 ] ) . toEqual ( { type : "text" , text : "" } )
3303+ } )
3304+
3305+ test ( "Pass 3 regression: Anthropic tool reorder does not leave empty non-tool half" , ( ) => {
3306+ // When an assistant message is ALL tool-calls with no text, the reorder produces
3307+ // [content:[], content:[tool-calls]]. The guard drops the empty half.
3308+ const msgs = [
3309+ {
3310+ role : "assistant" ,
3311+ content : [
3312+ { type : "tool-call" , toolCallId : "toolu_1" , toolName : "bash" , input : { command : "ls" } } ,
3313+ ] ,
3314+ } ,
3315+ ] as any [ ]
3316+
3317+ const result = ProviderTransform . message ( msgs , anthropicModel , { } ) as any [ ]
3318+
3319+ // The reorder only fires when there are non-tool parts AFTER a tool-call.
3320+ // A message that is only tool-calls is returned unchanged.
3321+ expect ( result ) . toHaveLength ( 1 )
3322+ expect ( result [ 0 ] . content ) . toHaveLength ( 1 )
3323+ expect ( result [ 0 ] . content [ 0 ] . type ) . toBe ( "tool-call" )
3324+ } )
3325+
3326+ test ( "Pass 3 regression: split that produces empty non-tool part is dropped by guard" , ( ) => {
3327+ // text="" before tool-call: after split, the text-only half has content:[{text:""}]
3328+ // which Pass 1 then collapses to [], which the guard drops.
3329+ const msgs = [
3330+ {
3331+ role : "assistant" ,
3332+ content : [
3333+ { type : "text" , text : "" } ,
3334+ { type : "tool-call" , toolCallId : "toolu_1" , toolName : "bash" , input : { command : "ls" } } ,
3335+ ] ,
3336+ } ,
3337+ {
3338+ role : "tool" ,
3339+ content : [
3340+ { type : "tool-result" , toolCallId : "toolu_1" , toolName : "bash" , output : { type : "text" , value : "ok" } } ,
3341+ ] ,
3342+ } ,
3343+ ] as any [ ]
3344+
3345+ const result = ProviderTransform . message ( msgs , anthropicModel , { } ) as any [ ]
3346+
3347+ // Pass 1 strips empty text part; after split the non-tool half is empty -> guard drops it.
3348+ // Only the tool-call half and the tool result remain.
3349+ const toolCallMsgs = result . filter ( ( m : any ) => Array . isArray ( m . content ) && m . content . some ( ( p : any ) => p . type === "tool-call" ) )
3350+ expect ( toolCallMsgs ) . toHaveLength ( 1 )
3351+ expect ( toolCallMsgs [ 0 ] . content ) . toHaveLength ( 1 )
3352+ expect ( toolCallMsgs [ 0 ] . content [ 0 ] . type ) . toBe ( "tool-call" )
3353+ } )
3354+
3355+ test ( "Pass 5 regression: interleaved-only assistant message is dropped by guard" , ( ) => {
3356+ const interleavedModel = {
3357+ ...openaiModel ,
3358+ id : "zai/glm-4.7" ,
3359+ providerID : "zai" ,
3360+ api : {
3361+ id : "glm-4.7" ,
3362+ url : "https://open.bigmodel.cn/api/paas/v4" ,
3363+ npm : "@ai-sdk/openai-compatible" ,
3364+ } ,
3365+ capabilities : {
3366+ ...openaiModel . capabilities ,
3367+ interleaved : { field : "reasoning_content" } ,
3368+ } ,
3369+ }
3370+
3371+ const msgs = [
3372+ { role : "user" , content : "Solve this" } ,
3373+ {
3374+ role : "assistant" ,
3375+ content : [
3376+ { type : "reasoning" , text : "Let me think..." } ,
3377+ ] ,
3378+ } ,
3379+ { role : "user" , content : "Done" } ,
3380+ ] as any [ ]
3381+
3382+ const result = ProviderTransform . message ( msgs , interleavedModel , { } )
3383+
3384+ // Pass 5 strips reasoning parts from content, leaving content:[].
3385+ // Universal guard drops it.
3386+ expect ( result ) . toHaveLength ( 2 )
3387+ expect ( result [ 0 ] . content ) . toBe ( "Solve this" )
3388+ expect ( result [ 1 ] . content ) . toBe ( "Done" )
3389+ } )
3390+
3391+ test ( "cross-provider matrix: empty array content is always dropped" , ( ) => {
3392+ const providers = [
3393+ { ...bedrockModel } ,
3394+ { ...anthropicModel } ,
3395+ { ...openaiModel } ,
3396+ {
3397+ ...openaiModel ,
3398+ id : "google-vertex/gemini-2.5-pro" ,
3399+ providerID : "google-vertex" ,
3400+ api : { id : "gemini-2.5-pro" , url : "https://us-central1-aiplatform.googleapis.com" , npm : "@ai-sdk/google-vertex" } ,
3401+ } ,
3402+ {
3403+ ...openaiModel ,
3404+ id : "mistral/mistral-large" ,
3405+ providerID : "mistral" ,
3406+ api : { id : "mistral-large-latest" , url : "https://api.mistral.ai" , npm : "@ai-sdk/mistral" } ,
3407+ } ,
3408+ {
3409+ ...openaiModel ,
3410+ id : "gateway/anthropic/claude-3-5-sonnet" ,
3411+ providerID : "anthropic" ,
3412+ api : { id : "anthropic/claude-3-5-sonnet" , url : "https://gateway.ai.cloudflare.com" , npm : "@ai-sdk/gateway" } ,
3413+ } ,
3414+ {
3415+ ...openaiModel ,
3416+ id : "openrouter/anthropic/claude-3.5-sonnet" ,
3417+ providerID : "openrouter" ,
3418+ api : { id : "anthropic/claude-3.5-sonnet" , url : "https://openrouter.ai/api/v1" , npm : "@openrouter/ai-sdk-provider" } ,
3419+ } ,
3420+ {
3421+ ...openaiModel ,
3422+ id : "zai/glm-4.7" ,
3423+ providerID : "zai" ,
3424+ api : { id : "glm-4.7" , url : "https://open.bigmodel.cn/api/paas/v4" , npm : "@ai-sdk/openai-compatible" } ,
3425+ } ,
3426+ ]
3427+
3428+ for ( const model of providers ) {
3429+ const msgs = [
3430+ { role : "user" , content : "Hello" } ,
3431+ { role : "assistant" , content : [ ] } ,
3432+ { role : "user" , content : "World" } ,
3433+ ] as any [ ]
3434+
3435+ const result = ProviderTransform . message ( msgs , model as any , { } )
3436+
3437+ const emptyContentMsgs = result . filter ( ( m ) => Array . isArray ( m . content ) && m . content . length === 0 )
3438+ expect ( emptyContentMsgs ) . toHaveLength ( 0 )
3439+ }
3440+ } )
3441+ } )
0 commit comments