@@ -221,11 +221,11 @@ function buildMessagesFromItems(items: ResponsesInputItem[]): OpenAIMessage[] {
221221 }
222222 case "function_call_output" :
223223 case "custom_tool_call_output" : {
224- const fco = item as { call_id : string ; output ?: string } ;
224+ const fco = item as { call_id : string ; output ?: unknown } ;
225225 out . push ( {
226226 role : "tool" ,
227227 tool_call_id : fco . call_id ,
228- content : fco . output ?? "" ,
228+ content : toolOutputToChat ( fco . output ) ,
229229 } ) ;
230230 pendingReasoning = "" ;
231231 continue ;
@@ -302,6 +302,78 @@ function extractReasoningText(item: ResponsesInputItem): string {
302302 . join ( "\n" ) ;
303303}
304304
305+ /**
306+ * Normalize a `function_call_output` / `custom_tool_call_output` `output` into
307+ * a Chat tool-message `content` value.
308+ *
309+ * The Responses spec allows the output to be a content-part array (OpenAI's
310+ * own docs show `{type:"input_image", image_url}` inside a tool result so the
311+ * model can look at a screenshot the tool returned); upstream Chat dialects
312+ * carry tool images as `image_url` parts on `role:"tool"` — and the downstream
313+ * Chat→Anthropic translator maps those onto Anthropic `tool_result` image
314+ * blocks. Clients sending read_file image results in the array form had every
315+ * image silently dropped when only strings were accepted here.
316+ *
317+ * Non-array, non-string outputs (structured JSON objects) stringify — matches
318+ * OpenAI Chat, where tool-message content is stringly typed.
319+ */
320+ function toolOutputToChat ( output : unknown ) : string | OpenAIContentPart [ ] {
321+ if ( typeof output === "string" ) return output ;
322+ if ( Array . isArray ( output ) ) {
323+ const chatParts = outputPartsToChat ( output ) ;
324+ if ( chatParts . every ( ( p ) => p . type === "text" ) ) return chatParts . map ( ( p ) => p . text ?? "" ) . join ( "" ) ;
325+ return chatParts ;
326+ }
327+ if ( output === undefined || output === null ) return "" ;
328+ try {
329+ return JSON . stringify ( output ) ;
330+ } catch {
331+ return String ( output ) ;
332+ }
333+ }
334+
335+ /**
336+ * Convert content-part objects appearing inside a tool output array to Chat
337+ * content parts. Accepts the Responses dialect (`input_image` with
338+ * `image_url: string | {url}`, `input_text`, `input_video`, `input_file`)
339+ * and the already-Chat-shaped parts some clients emit verbatim. Anything
340+ * unknown degrades to a text part carrying its JSON — never dropped, so the
341+ * model can still see what the tool returned.
342+ */
343+ function outputPartsToChat ( parts : unknown [ ] ) : OpenAIContentPart [ ] {
344+ const out : OpenAIContentPart [ ] = [ ] ;
345+ for ( const part of parts ) {
346+ if ( part === null || typeof part !== "object" ) {
347+ out . push ( { type : "text" , text : String ( part ) } ) ;
348+ continue ;
349+ }
350+ const p = part as ResponsesContentPart ;
351+ if ( ( p . type === "input_text" || p . type === "output_text" || p . type === "text" ) && typeof p . text === "string" ) {
352+ out . push ( { type : "text" , text : p . text } ) ;
353+ } else if ( p . type === "input_image" || p . type === "image_url" ) {
354+ const url = typeof p . image_url === "string" ? p . image_url : p . image_url ?. url ;
355+ if ( ! url ) continue ;
356+ out . push ( { type : "image_url" , image_url : { url } } ) ;
357+ } else if ( p . type === "input_video" || p . type === "video_url" ) {
358+ const url = typeof p . video_url === "string"
359+ ? p . video_url
360+ : p . video_url ?. url ?? ( typeof p . data === "string" ? p . data : undefined ) ;
361+ if ( ! url ) continue ;
362+ out . push ( { type : "video_url" , video_url : { url } } ) ;
363+ } else if ( p . type === "input_file" || p . type === "file" ) {
364+ const fileData = p . file_data ?? p . file ?. file_data ;
365+ if ( ! fileData ) continue ;
366+ out . push ( {
367+ type : "file" ,
368+ file : { filename : p . filename ?? p . file ?. filename , file_data : fileData } ,
369+ } ) ;
370+ } else {
371+ out . push ( { type : "text" , text : JSON . stringify ( p ) } ) ;
372+ }
373+ }
374+ return out ;
375+ }
376+
305377/** Convert Responses content parts to a Chat `content` value (string or parts[]). */
306378function contentPartsToChat (
307379 content : ResponsesContentPart [ ] | string | undefined ,
0 commit comments