@@ -636,27 +636,36 @@ const buildMessage = async (msg: llm.ChatMessage, cacheKey: any) => {
636636 break ;
637637 }
638638
639- if ( typeof msg . content === 'string' ) {
640- oaiMsg . content = msg . content ;
641- } else if ( Array . isArray ( msg . content ) ) {
642- oaiMsg . content = ( await Promise . all (
643- msg . content . map ( async ( c ) => {
644- if ( typeof c === 'string' ) {
645- return { type : 'text' , text : c } ;
646- } else if (
647- // typescript type guard for determining ChatAudio vs ChatImage
648- ( ( c : llm . ChatAudio | llm . ChatImage ) : c is llm . ChatImage => {
649- return ( c as llm . ChatImage ) . image !== undefined ;
650- } ) ( c )
651- ) {
652- return await buildImageContent ( c , cacheKey ) ;
653- } else {
654- throw new Error ( 'ChatAudio is not supported' ) ;
655- }
656- } ) ,
657- ) ) as OpenAI . ChatCompletionContentPart [ ] ;
658- } else if ( msg . content === undefined ) {
659- oaiMsg . content = '' ;
639+ if ( msg . role === llm . ChatRole . TOOL ) {
640+ try {
641+ const toolCallOutput = JSON . stringify ( msg . content ) ;
642+ oaiMsg . content = toolCallOutput ;
643+ } catch ( e ) {
644+ throw Error ( `Tool call output is not JSON serializable: ${ e } ` ) ;
645+ }
646+ } else {
647+ if ( typeof msg . content === 'string' ) {
648+ oaiMsg . content = msg . content ;
649+ } else if ( Array . isArray ( msg . content ) ) {
650+ oaiMsg . content = ( await Promise . all (
651+ msg . content . map ( async ( c ) => {
652+ if ( typeof c === 'string' ) {
653+ return { type : 'text' , text : c } ;
654+ } else if (
655+ // typescript type guard for determining ChatAudio vs ChatImage
656+ ( ( c : llm . ChatAudio | llm . ChatImage ) : c is llm . ChatImage => {
657+ return ( c as llm . ChatImage ) . image !== undefined ;
658+ } ) ( c )
659+ ) {
660+ return await buildImageContent ( c , cacheKey ) ;
661+ } else {
662+ throw new Error ( 'ChatAudio is not supported' ) ;
663+ }
664+ } ) ,
665+ ) ) as OpenAI . ChatCompletionContentPart [ ] ;
666+ } else if ( msg . content === undefined ) {
667+ oaiMsg . content = '' ;
668+ }
660669 }
661670
662671 // make sure to provide when function has been called inside the context
0 commit comments