Skip to content

Commit 7345fa7

Browse files
committed
Store internal tools in additional_kwargs
1 parent bb3f4c9 commit 7345fa7

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

libs/langchain-openai/src/chat_models.ts

+29-12
Original file line numberDiff line numberDiff line change
@@ -1447,23 +1447,35 @@ export class ChatOpenAI<
14471447
protected _convertOpenAIResponsesMessageToBaseMessage(
14481448
response: OpenAIResponsesCreateStream
14491449
): BaseMessage {
1450+
if (response.error) {
1451+
// TODO: might be incorrect
1452+
throw wrapOpenAIClientError(response.error);
1453+
}
1454+
1455+
const content: MessageContent = [];
14501456
const tool_calls: ToolCall[] = [];
14511457
const invalid_tool_calls: InvalidToolCall[] = [];
1458+
const additional_kwargs: {
1459+
[key: string]: unknown;
1460+
reasoning?: unknown;
1461+
tool_outputs?: unknown[];
1462+
} = {};
14521463

1453-
let content: MessageContent = [];
14541464
for (const item of response.output) {
14551465
if (item.type === "message") {
14561466
// 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+
);
14671479
} else if (item.type === "function_call") {
14681480
const fnAdapter = {
14691481
function: { name: item.name, arguments: item.arguments },
@@ -1484,8 +1496,11 @@ export class ChatOpenAI<
14841496
}
14851497
invalid_tool_calls.push(makeInvalidToolCall(fnAdapter, errMessage));
14861498
}
1499+
} else if (item.type === "reasoning") {
1500+
additional_kwargs.reasoning = item;
14871501
} else {
1488-
throw new Error(`Unknown item type: ${item.type}`);
1502+
additional_kwargs.tool_outputs ??= [];
1503+
additional_kwargs.tool_outputs.push(item);
14891504
}
14901505
}
14911506

@@ -1494,6 +1509,8 @@ export class ChatOpenAI<
14941509
content,
14951510
tool_calls,
14961511
invalid_tool_calls,
1512+
usage_metadata: response.usage,
1513+
additional_kwargs,
14971514
response_metadata: { model_name: response.model, usage: response.usage },
14981515
});
14991516
}

0 commit comments

Comments
 (0)