@@ -1447,23 +1447,35 @@ export class ChatOpenAI<
1447
1447
protected _convertOpenAIResponsesMessageToBaseMessage (
1448
1448
response : OpenAIResponsesCreateStream
1449
1449
) : BaseMessage {
1450
+ if ( response . error ) {
1451
+ // TODO: might be incorrect
1452
+ throw wrapOpenAIClientError ( response . error ) ;
1453
+ }
1454
+
1455
+ const content : MessageContent = [ ] ;
1450
1456
const tool_calls : ToolCall [ ] = [ ] ;
1451
1457
const invalid_tool_calls : InvalidToolCall [ ] = [ ] ;
1458
+ const additional_kwargs : {
1459
+ [ key : string ] : unknown ;
1460
+ reasoning ?: unknown ;
1461
+ tool_outputs ?: unknown [ ] ;
1462
+ } = { } ;
1452
1463
1453
- let content : MessageContent = [ ] ;
1454
1464
for ( const item of response . output ) {
1455
1465
if ( item . type === "message" ) {
1456
1466
// TODO: how to handle refusals?
1457
- content = item . content . map ( ( part ) => {
1458
- if ( part . type === "output_text" ) {
1459
- return {
1460
- type : "text" ,
1461
- text : part . text ,
1462
- annotations : part . annotations ,
1463
- } ;
1464
- }
1465
- return part ;
1466
- } ) ;
1467
+ content . push (
1468
+ ...item . content . map ( ( part ) => {
1469
+ if ( part . type === "output_text" ) {
1470
+ return {
1471
+ type : "text" ,
1472
+ text : part . text ,
1473
+ annotations : part . annotations ,
1474
+ } ;
1475
+ }
1476
+ return part ;
1477
+ } )
1478
+ ) ;
1467
1479
} else if ( item . type === "function_call" ) {
1468
1480
const fnAdapter = {
1469
1481
function : { name : item . name , arguments : item . arguments } ,
@@ -1484,8 +1496,11 @@ export class ChatOpenAI<
1484
1496
}
1485
1497
invalid_tool_calls . push ( makeInvalidToolCall ( fnAdapter , errMessage ) ) ;
1486
1498
}
1499
+ } else if ( item . type === "reasoning" ) {
1500
+ additional_kwargs . reasoning = item ;
1487
1501
} else {
1488
- throw new Error ( `Unknown item type: ${ item . type } ` ) ;
1502
+ additional_kwargs . tool_outputs ??= [ ] ;
1503
+ additional_kwargs . tool_outputs . push ( item ) ;
1489
1504
}
1490
1505
}
1491
1506
@@ -1494,6 +1509,8 @@ export class ChatOpenAI<
1494
1509
content,
1495
1510
tool_calls,
1496
1511
invalid_tool_calls,
1512
+ usage_metadata : response . usage ,
1513
+ additional_kwargs,
1497
1514
response_metadata : { model_name : response . model , usage : response . usage } ,
1498
1515
} ) ;
1499
1516
}
0 commit comments