File tree Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Expand file tree Collapse file tree 2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -377,23 +377,35 @@ function convertAIMessageToConverseMessage(msg: AIMessage): BedrockMessage {
377
377
} ) ;
378
378
} else if ( Array . isArray ( msg . content ) ) {
379
379
const concatenatedBlocks = concatenateLangchainReasoningBlocks ( msg . content ) ;
380
- const contentBlocks : ContentBlock [ ] = concatenatedBlocks . map ( ( block ) => {
380
+ const contentBlocks : ContentBlock [ ] = [ ] ;
381
+ concatenatedBlocks . forEach ( ( block ) => {
381
382
if ( block . type === "text" && block . text !== "" ) {
382
- return {
383
- text : block . text ,
384
- } ;
383
+ // Merge whitespace/newlines with previous text blocks to avoid validation errors.
384
+ const cleanedText = block . text ?. replace ( / \n / g, "" ) . trim ( ) ;
385
+ if ( cleanedText === "" ) {
386
+ if ( contentBlocks . length > 0 ) {
387
+ const mergedTextContent = `${
388
+ contentBlocks [ contentBlocks . length - 1 ] . text
389
+ } ${ block . text } `;
390
+ contentBlocks [ contentBlocks . length - 1 ] . text = mergedTextContent ;
391
+ }
392
+ } else {
393
+ contentBlocks . push ( {
394
+ text : block . text ,
395
+ } ) ;
396
+ }
385
397
} else if ( block . type === "reasoning_content" ) {
386
- return {
398
+ contentBlocks . push ( {
387
399
reasoningContent : langchainReasoningBlockToBedrockReasoningBlock (
388
400
block as MessageContentReasoningBlock
389
401
) ,
390
- } ;
402
+ } ) ;
391
403
} else if ( isDefaultCachePoint ( block ) ) {
392
- return {
404
+ contentBlocks . push ( {
393
405
cachePoint : {
394
406
type : "default" ,
395
407
} ,
396
- } ;
408
+ } ) ;
397
409
} else {
398
410
const blockValues = Object . fromEntries (
399
411
Object . entries ( block ) . filter ( ( [ key ] ) => key !== "type" )
Original file line number Diff line number Diff line change @@ -416,7 +416,7 @@ test("Streaming supports empty string chunks", async () => {
416
416
{
417
417
contentBlockIndex : 0 ,
418
418
delta : {
419
- text : "Hello " ,
419
+ text : "Hello" ,
420
420
} ,
421
421
} ,
422
422
{
@@ -425,6 +425,12 @@ test("Streaming supports empty string chunks", async () => {
425
425
text : "" ,
426
426
} ,
427
427
} ,
428
+ {
429
+ contentBlockIndex : 0 ,
430
+ delta : {
431
+ text : " " ,
432
+ } ,
433
+ } ,
428
434
{
429
435
contentBlockIndex : 0 ,
430
436
delta : {
You can’t perform that action at this time.
0 commit comments