@langchain/openai 1.0.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/azure/chat_models/completions.d.cts +1 -1
  3. package/dist/azure/chat_models/index.cjs +1 -1
  4. package/dist/azure/chat_models/index.d.cts +2 -2
  5. package/dist/azure/chat_models/index.js +1 -1
  6. package/dist/azure/chat_models/responses.cjs +1 -1
  7. package/dist/azure/chat_models/responses.d.cts +1 -1
  8. package/dist/azure/chat_models/responses.js +1 -1
  9. package/dist/azure/embeddings.cjs +1 -1
  10. package/dist/azure/embeddings.js +1 -1
  11. package/dist/chat_models/base.cjs +24 -3
  12. package/dist/chat_models/base.cjs.map +1 -1
  13. package/dist/chat_models/base.d.cts +20 -1
  14. package/dist/chat_models/base.d.cts.map +1 -1
  15. package/dist/chat_models/base.d.ts +23 -4
  16. package/dist/chat_models/base.d.ts.map +1 -1
  17. package/dist/chat_models/base.js +24 -3
  18. package/dist/chat_models/base.js.map +1 -1
  19. package/dist/chat_models/completions.cjs +36 -106
  20. package/dist/chat_models/completions.cjs.map +1 -1
  21. package/dist/chat_models/completions.d.cts +18 -7
  22. package/dist/chat_models/completions.d.cts.map +1 -1
  23. package/dist/chat_models/completions.d.ts +17 -6
  24. package/dist/chat_models/completions.d.ts.map +1 -1
  25. package/dist/chat_models/completions.js +37 -107
  26. package/dist/chat_models/completions.js.map +1 -1
  27. package/dist/chat_models/index.cjs +1 -1
  28. package/dist/chat_models/index.cjs.map +1 -1
  29. package/dist/chat_models/index.d.cts +3 -3
  30. package/dist/chat_models/index.d.ts +4 -4
  31. package/dist/chat_models/index.js +1 -1
  32. package/dist/chat_models/index.js.map +1 -1
  33. package/dist/chat_models/profiles.cjs +567 -0
  34. package/dist/chat_models/profiles.cjs.map +1 -0
  35. package/dist/chat_models/profiles.js +566 -0
  36. package/dist/chat_models/profiles.js.map +1 -0
  37. package/dist/chat_models/responses.cjs +14 -375
  38. package/dist/chat_models/responses.cjs.map +1 -1
  39. package/dist/chat_models/responses.d.cts +2 -17
  40. package/dist/chat_models/responses.d.cts.map +1 -1
  41. package/dist/chat_models/responses.d.ts +1 -16
  42. package/dist/chat_models/responses.d.ts.map +1 -1
  43. package/dist/chat_models/responses.js +15 -376
  44. package/dist/chat_models/responses.js.map +1 -1
  45. package/dist/converters/completions.cjs +605 -0
  46. package/dist/converters/completions.cjs.map +1 -0
  47. package/dist/converters/completions.d.cts +319 -0
  48. package/dist/converters/completions.d.cts.map +1 -0
  49. package/dist/converters/completions.d.ts +319 -0
  50. package/dist/converters/completions.d.ts.map +1 -0
  51. package/dist/converters/completions.js +599 -0
  52. package/dist/converters/completions.js.map +1 -0
  53. package/dist/converters/index.cjs +2 -0
  54. package/dist/converters/index.js +2 -0
  55. package/dist/converters/responses.cjs +895 -0
  56. package/dist/converters/responses.cjs.map +1 -0
  57. package/dist/converters/responses.d.cts +310 -0
  58. package/dist/converters/responses.d.cts.map +1 -0
  59. package/dist/converters/responses.d.ts +310 -0
  60. package/dist/converters/responses.d.ts.map +1 -0
  61. package/dist/converters/responses.js +889 -0
  62. package/dist/converters/responses.js.map +1 -0
  63. package/dist/embeddings.cjs +1 -1
  64. package/dist/embeddings.js +1 -1
  65. package/dist/index.cjs +24 -11
  66. package/dist/index.d.cts +4 -3
  67. package/dist/index.d.ts +4 -3
  68. package/dist/index.js +7 -5
  69. package/dist/llms.cjs +2 -2
  70. package/dist/llms.d.cts +1 -1
  71. package/dist/llms.js +2 -2
  72. package/dist/types.d.cts +2 -2
  73. package/dist/types.d.ts +2 -2
  74. package/dist/utils/errors.cjs +1 -1
  75. package/dist/utils/errors.cjs.map +1 -1
  76. package/dist/utils/errors.js +1 -1
  77. package/dist/utils/errors.js.map +1 -1
  78. package/dist/utils/misc.js +2 -2
  79. package/dist/utils/misc.js.map +1 -1
  80. package/dist/utils/output.cjs +0 -12
  81. package/dist/utils/output.cjs.map +1 -1
  82. package/dist/utils/output.js +1 -12
  83. package/dist/utils/output.js.map +1 -1
  84. package/dist/utils/prompts.cjs +2 -2
  85. package/dist/utils/prompts.cjs.map +1 -1
  86. package/dist/utils/prompts.d.cts +1 -0
  87. package/dist/utils/prompts.d.cts.map +1 -1
  88. package/dist/utils/prompts.d.ts +1 -0
  89. package/dist/utils/prompts.d.ts.map +1 -1
  90. package/dist/utils/prompts.js +2 -2
  91. package/dist/utils/prompts.js.map +1 -1
  92. package/dist/utils/tools.cjs +1 -1
  93. package/dist/utils/tools.js +1 -1
  94. package/package.json +9 -6
  95. package/dist/utils/message_inputs.cjs +0 -139
  96. package/dist/utils/message_inputs.cjs.map +0 -1
  97. package/dist/utils/message_inputs.d.cts +0 -9
  98. package/dist/utils/message_inputs.d.cts.map +0 -1
  99. package/dist/utils/message_inputs.d.ts +0 -9
  100. package/dist/utils/message_inputs.d.ts.map +0 -1
  101. package/dist/utils/message_inputs.js +0 -137
  102. package/dist/utils/message_inputs.js.map +0 -1
  103. package/dist/utils/standard.cjs +0 -315
  104. package/dist/utils/standard.cjs.map +0 -1
  105. package/dist/utils/standard.js +0 -313
  106. package/dist/utils/standard.js.map +0 -1
@@ -0,0 +1,895 @@
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
2
+ const require_tools = require('../utils/tools.cjs');
3
+ const require_misc = require('../utils/misc.cjs');
4
+ const require_completions = require('./completions.cjs');
5
+ const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
6
+ const __langchain_core_outputs = require_rolldown_runtime.__toESM(require("@langchain/core/outputs"));
7
+ const __langchain_core_output_parsers_openai_tools = require_rolldown_runtime.__toESM(require("@langchain/core/output_parsers/openai_tools"));
8
+
9
+ //#region src/converters/responses.ts
10
+ const _FUNCTION_CALL_IDS_MAP_KEY = "__openai_function_call_ids__";
11
+ /**
12
+ * Converts OpenAI Responses API usage statistics to LangChain's UsageMetadata format.
13
+ *
14
+ * This converter transforms token usage information from OpenAI's Responses API into
15
+ * the standardized UsageMetadata format used throughout LangChain. It handles both
16
+ * basic token counts and detailed token breakdowns including cached tokens and
17
+ * reasoning tokens.
18
+ *
19
+ * @param usage - The usage statistics object from OpenAI's Responses API containing
20
+ * token counts and optional detailed breakdowns.
21
+ *
22
+ * @returns A UsageMetadata object containing:
23
+ * - `input_tokens`: Total number of tokens in the input/prompt (defaults to 0 if not provided)
24
+ * - `output_tokens`: Total number of tokens in the model's output (defaults to 0 if not provided)
25
+ * - `total_tokens`: Combined total of input and output tokens (defaults to 0 if not provided)
26
+ * - `input_token_details`: Object containing detailed input token information:
27
+ * - `cache_read`: Number of tokens read from cache (only included if available)
28
+ * - `output_token_details`: Object containing detailed output token information:
29
+ * - `reasoning`: Number of tokens used for reasoning (only included if available)
30
+ *
31
+ * @example
32
+ * ```typescript
33
+ * const usage = {
34
+ * input_tokens: 100,
35
+ * output_tokens: 50,
36
+ * total_tokens: 150,
37
+ * input_tokens_details: { cached_tokens: 20 },
38
+ * output_tokens_details: { reasoning_tokens: 10 }
39
+ * };
40
+ *
41
+ * const metadata = convertResponsesUsageToUsageMetadata(usage);
42
+ * // Returns:
43
+ * // {
44
+ * // input_tokens: 100,
45
+ * // output_tokens: 50,
46
+ * // total_tokens: 150,
47
+ * // input_token_details: { cache_read: 20 },
48
+ * // output_token_details: { reasoning: 10 }
49
+ * // }
50
+ * ```
51
+ *
52
+ * @remarks
53
+ * - The function safely handles undefined or null values by using optional chaining
54
+ * and nullish coalescing operators
55
+ * - Detailed token information (cache_read, reasoning) is only included in the result
56
+ * if the corresponding values are present in the input
57
+ * - Token counts default to 0 if not provided in the usage object
58
+ * - This converter is specifically designed for OpenAI's Responses API format and
59
+ * may differ from other OpenAI API endpoints
60
+ */
61
+ const convertResponsesUsageToUsageMetadata = (usage) => {
62
+ const inputTokenDetails = { ...usage?.input_tokens_details?.cached_tokens != null && { cache_read: usage?.input_tokens_details?.cached_tokens } };
63
+ const outputTokenDetails = { ...usage?.output_tokens_details?.reasoning_tokens != null && { reasoning: usage?.output_tokens_details?.reasoning_tokens } };
64
+ return {
65
+ input_tokens: usage?.input_tokens ?? 0,
66
+ output_tokens: usage?.output_tokens ?? 0,
67
+ total_tokens: usage?.total_tokens ?? 0,
68
+ input_token_details: inputTokenDetails,
69
+ output_token_details: outputTokenDetails
70
+ };
71
+ };
72
+ /**
73
+ * Converts an OpenAI Responses API response to a LangChain AIMessage.
74
+ *
75
+ * This converter processes the output from OpenAI's Responses API (both `create` and `parse` methods)
76
+ * and transforms it into a LangChain AIMessage object with all relevant metadata, tool calls, and content.
77
+ *
78
+ * @param response - The response object from OpenAI's Responses API. Can be either:
79
+ * - ResponsesCreateInvoke: Result from `responses.create()`
80
+ * - ResponsesParseInvoke: Result from `responses.parse()`
81
+ *
82
+ * @returns An AIMessage containing:
83
+ * - `id`: The message ID from the response output
84
+ * - `content`: Array of message content blocks (text, images, etc.)
85
+ * - `tool_calls`: Array of successfully parsed tool calls
86
+ * - `invalid_tool_calls`: Array of tool calls that failed to parse
87
+ * - `usage_metadata`: Token usage information converted to LangChain format
88
+ * - `additional_kwargs`: Extra data including:
89
+ * - `refusal`: Refusal text if the model refused to respond
90
+ * - `reasoning`: Reasoning output for reasoning models
91
+ * - `tool_outputs`: Results from built-in tools (web search, file search, etc.)
92
+ * - `parsed`: Parsed structured output when using json_schema format
93
+ * - Function call ID mappings for tracking
94
+ * - `response_metadata`: Metadata about the response including model, timestamps, status, etc.
95
+ *
96
+ * @throws Error if the response contains an error object. The error message and code are extracted
97
+ * from the response.error field.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const response = await client.responses.create({
102
+ * model: "gpt-4",
103
+ * input: [{ type: "message", content: "Hello" }]
104
+ * });
105
+ * const message = convertResponsesMessageToAIMessage(response);
106
+ * console.log(message.content); // Message content
107
+ * console.log(message.tool_calls); // Any tool calls made
108
+ * ```
109
+ *
110
+ * @remarks
111
+ * The converter handles multiple output item types:
112
+ * - `message`: Text and structured content from the model
113
+ * - `function_call`: Tool/function calls that need to be executed
114
+ * - `reasoning`: Reasoning traces from reasoning models (o1, o3, etc.)
115
+ * - `custom_tool_call`: Custom tool invocations
116
+ * - Built-in tool outputs: web_search, file_search, code_interpreter, etc.
117
+ *
118
+ * Tool calls are parsed and validated. Invalid tool calls (malformed JSON, etc.) are captured
119
+ * in the `invalid_tool_calls` array rather than throwing errors.
120
+ */
121
+ const convertResponsesMessageToAIMessage = (response) => {
122
+ if (response.error) {
123
+ const error = new Error(response.error.message);
124
+ error.name = response.error.code;
125
+ throw error;
126
+ }
127
+ let messageId;
128
+ const content = [];
129
+ const tool_calls = [];
130
+ const invalid_tool_calls = [];
131
+ const response_metadata = {
132
+ model_provider: "openai",
133
+ model: response.model,
134
+ created_at: response.created_at,
135
+ id: response.id,
136
+ incomplete_details: response.incomplete_details,
137
+ metadata: response.metadata,
138
+ object: response.object,
139
+ status: response.status,
140
+ user: response.user,
141
+ service_tier: response.service_tier,
142
+ model_name: response.model
143
+ };
144
+ const additional_kwargs = {};
145
+ for (const item of response.output) if (item.type === "message") {
146
+ messageId = item.id;
147
+ content.push(...item.content.flatMap((part) => {
148
+ if (part.type === "output_text") {
149
+ if ("parsed" in part && part.parsed != null) additional_kwargs.parsed = part.parsed;
150
+ return {
151
+ type: "text",
152
+ text: part.text,
153
+ annotations: part.annotations
154
+ };
155
+ }
156
+ if (part.type === "refusal") {
157
+ additional_kwargs.refusal = part.refusal;
158
+ return [];
159
+ }
160
+ return part;
161
+ }));
162
+ } else if (item.type === "function_call") {
163
+ const fnAdapter = {
164
+ function: {
165
+ name: item.name,
166
+ arguments: item.arguments
167
+ },
168
+ id: item.call_id
169
+ };
170
+ try {
171
+ tool_calls.push((0, __langchain_core_output_parsers_openai_tools.parseToolCall)(fnAdapter, { returnId: true }));
172
+ } catch (e) {
173
+ let errMessage;
174
+ if (typeof e === "object" && e != null && "message" in e && typeof e.message === "string") errMessage = e.message;
175
+ invalid_tool_calls.push((0, __langchain_core_output_parsers_openai_tools.makeInvalidToolCall)(fnAdapter, errMessage));
176
+ }
177
+ additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] ??= {};
178
+ if (item.id) additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][item.call_id] = item.id;
179
+ } else if (item.type === "reasoning") additional_kwargs.reasoning = item;
180
+ else if (item.type === "custom_tool_call") {
181
+ const parsed = require_tools.parseCustomToolCall(item);
182
+ if (parsed) tool_calls.push(parsed);
183
+ else invalid_tool_calls.push((0, __langchain_core_output_parsers_openai_tools.makeInvalidToolCall)(item, "Malformed custom tool call"));
184
+ } else {
185
+ additional_kwargs.tool_outputs ??= [];
186
+ additional_kwargs.tool_outputs.push(item);
187
+ }
188
+ return new __langchain_core_messages.AIMessage({
189
+ id: messageId,
190
+ content,
191
+ tool_calls,
192
+ invalid_tool_calls,
193
+ usage_metadata: convertResponsesUsageToUsageMetadata(response.usage),
194
+ additional_kwargs,
195
+ response_metadata
196
+ });
197
+ };
198
+ /**
199
+ * Converts a LangChain ChatOpenAI reasoning summary to an OpenAI Responses API reasoning item.
200
+ *
201
+ * This converter transforms reasoning summaries that have been accumulated during streaming
202
+ * (where summary parts may arrive in multiple chunks with the same index) into the final
203
+ * consolidated format expected by OpenAI's Responses API. It combines summary parts that
204
+ * share the same index and removes the index field from the final output.
205
+ *
206
+ * @param reasoning - A ChatOpenAI reasoning summary object containing:
207
+ * - `id`: The reasoning item ID
208
+ * - `type`: The type of reasoning (typically "reasoning")
209
+ * - `summary`: Array of summary parts, each with:
210
+ * - `text`: The summary text content
211
+ * - `type`: The summary type (e.g., "summary_text")
212
+ * - `index`: The index used to group related summary parts during streaming
213
+ *
214
+ * @returns An OpenAI Responses API ResponseReasoningItem with:
215
+ * - All properties from the input reasoning object
216
+ * - `summary`: Consolidated array of summary objects with:
217
+ * - `text`: Combined text from all parts with the same index
218
+ * - `type`: The summary type
219
+ * - No `index` field (removed after consolidation)
220
+ *
221
+ * @example
222
+ * ```typescript
223
+ * // Input: Reasoning summary with multiple parts at the same index
224
+ * const reasoning = {
225
+ * id: "reasoning_123",
226
+ * type: "reasoning",
227
+ * summary: [
228
+ * { text: "First ", type: "summary_text", index: 0 },
229
+ * { text: "part", type: "summary_text", index: 0 },
230
+ * { text: "Second part", type: "summary_text", index: 1 }
231
+ * ]
232
+ * };
233
+ *
234
+ * const result = convertReasoningSummaryToResponsesReasoningItem(reasoning);
235
+ * // Returns:
236
+ * // {
237
+ * // id: "reasoning_123",
238
+ * // type: "reasoning",
239
+ * // summary: [
240
+ * // { text: "First part", type: "summary_text" },
241
+ * // { text: "Second part", type: "summary_text" }
242
+ * // ]
243
+ * // }
244
+ * ```
245
+ *
246
+ * @remarks
247
+ * - This converter is primarily used when reconstructing complete reasoning items from
248
+ * streaming chunks, where summary parts may arrive incrementally with index markers
249
+ * - Summary parts with the same index are concatenated in the order they appear
250
+ * - If the reasoning summary contains only one part, no reduction is performed
251
+ * - The index field is used internally during streaming to track which summary parts
252
+ * belong together, but is removed from the final output as it's not part of the
253
+ * OpenAI Responses API schema
254
+ * - This is the inverse operation of the streaming accumulation that happens in
255
+ * `convertResponsesDeltaToChatGenerationChunk`
256
+ */
257
+ const convertReasoningSummaryToResponsesReasoningItem = (reasoning) => {
258
+ const summary = (reasoning.summary.length > 1 ? reasoning.summary.reduce((acc, curr) => {
259
+ const last = acc[acc.length - 1];
260
+ if (last.index === curr.index) last.text += curr.text;
261
+ else acc.push(curr);
262
+ return acc;
263
+ }, [{ ...reasoning.summary[0] }]) : reasoning.summary).map((s) => Object.fromEntries(Object.entries(s).filter(([k]) => k !== "index")));
264
+ return {
265
+ ...reasoning,
266
+ summary
267
+ };
268
+ };
269
+ /**
270
+ * Converts OpenAI Responses API stream events to LangChain ChatGenerationChunk objects.
271
+ *
272
+ * This converter processes streaming events from OpenAI's Responses API and transforms them
273
+ * into LangChain ChatGenerationChunk objects that can be used in streaming chat applications.
274
+ * It handles various event types including text deltas, tool calls, reasoning, and metadata updates.
275
+ *
276
+ * @param event - A streaming event from OpenAI's Responses API
277
+ *
278
+ * @returns A ChatGenerationChunk containing:
279
+ * - `text`: Concatenated text content from all text parts in the event
280
+ * - `message`: An AIMessageChunk with:
281
+ * - `id`: Message ID (set when a message output item is added)
282
+ * - `content`: Array of content blocks (text with optional annotations)
283
+ * - `tool_call_chunks`: Incremental tool call data (name, args, id)
284
+ * - `usage_metadata`: Token usage information (only in completion events)
285
+ * - `additional_kwargs`: Extra data including:
286
+ * - `refusal`: Refusal text if the model refused to respond
287
+ * - `reasoning`: Reasoning output for reasoning models (id, type, summary)
288
+ * - `tool_outputs`: Results from built-in tools (web search, file search, etc.)
289
+ * - `parsed`: Parsed structured output when using json_schema format
290
+ * - Function call ID mappings for tracking
291
+ * - `response_metadata`: Metadata about the response (model, id, etc.)
292
+ * - `generationInfo`: Additional generation information (e.g., tool output status)
293
+ *
294
+ * Returns `null` for events that don't produce meaningful chunks:
295
+ * - Partial image generation events (to avoid storing all partial images in history)
296
+ * - Unrecognized event types
297
+ *
298
+ * @example
299
+ * ```typescript
300
+ * const stream = await client.responses.create({
301
+ * model: "gpt-4",
302
+ * input: [{ type: "message", content: "Hello" }],
303
+ * stream: true
304
+ * });
305
+ *
306
+ * for await (const event of stream) {
307
+ * const chunk = convertResponsesDeltaToChatGenerationChunk(event);
308
+ * if (chunk) {
309
+ * console.log(chunk.text); // Incremental text
310
+ * console.log(chunk.message.tool_call_chunks); // Tool call updates
311
+ * }
312
+ * }
313
+ * ```
314
+ *
315
+ * @remarks
316
+ * - Text content is accumulated in an array with index tracking for proper ordering
317
+ * - Tool call chunks include incremental arguments that need to be concatenated by the consumer
318
+ * - Reasoning summaries are built incrementally across multiple events
319
+ * - Function call IDs are tracked in `additional_kwargs` to map call_id to item id
320
+ * - The `text` field is provided for legacy compatibility with `onLLMNewToken` callbacks
321
+ * - Usage metadata is only available in `response.completed` events
322
+ * - Partial images are intentionally ignored to prevent memory bloat in conversation history
323
+ */
324
+ const convertResponsesDeltaToChatGenerationChunk = (event) => {
325
+ const content = [];
326
+ let generationInfo = {};
327
+ let usage_metadata;
328
+ const tool_call_chunks = [];
329
+ const response_metadata = { model_provider: "openai" };
330
+ const additional_kwargs = {};
331
+ let id;
332
+ if (event.type === "response.output_text.delta") content.push({
333
+ type: "text",
334
+ text: event.delta,
335
+ index: event.content_index
336
+ });
337
+ else if (event.type === "response.output_text.annotation.added") content.push({
338
+ type: "text",
339
+ text: "",
340
+ annotations: [event.annotation],
341
+ index: event.content_index
342
+ });
343
+ else if (event.type === "response.output_item.added" && event.item.type === "message") id = event.item.id;
344
+ else if (event.type === "response.output_item.added" && event.item.type === "function_call") {
345
+ tool_call_chunks.push({
346
+ type: "tool_call_chunk",
347
+ name: event.item.name,
348
+ args: event.item.arguments,
349
+ id: event.item.call_id,
350
+ index: event.output_index
351
+ });
352
+ additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = { [event.item.call_id]: event.item.id };
353
+ } else if (event.type === "response.output_item.done" && [
354
+ "web_search_call",
355
+ "file_search_call",
356
+ "computer_call",
357
+ "code_interpreter_call",
358
+ "mcp_call",
359
+ "mcp_list_tools",
360
+ "mcp_approval_request",
361
+ "image_generation_call",
362
+ "custom_tool_call"
363
+ ].includes(event.item.type)) additional_kwargs.tool_outputs = [event.item];
364
+ else if (event.type === "response.created") {
365
+ response_metadata.id = event.response.id;
366
+ response_metadata.model_name = event.response.model;
367
+ response_metadata.model = event.response.model;
368
+ } else if (event.type === "response.completed") {
369
+ const msg = convertResponsesMessageToAIMessage(event.response);
370
+ usage_metadata = convertResponsesUsageToUsageMetadata(event.response.usage);
371
+ if (event.response.text?.format?.type === "json_schema") additional_kwargs.parsed ??= JSON.parse(msg.text);
372
+ for (const [key, value] of Object.entries(event.response)) if (key !== "id") response_metadata[key] = value;
373
+ } else if (event.type === "response.function_call_arguments.delta" || event.type === "response.custom_tool_call_input.delta") tool_call_chunks.push({
374
+ type: "tool_call_chunk",
375
+ args: event.delta,
376
+ index: event.output_index
377
+ });
378
+ else if (event.type === "response.web_search_call.completed" || event.type === "response.file_search_call.completed") generationInfo = { tool_outputs: {
379
+ id: event.item_id,
380
+ type: event.type.replace("response.", "").replace(".completed", ""),
381
+ status: "completed"
382
+ } };
383
+ else if (event.type === "response.refusal.done") additional_kwargs.refusal = event.refusal;
384
+ else if (event.type === "response.output_item.added" && "item" in event && event.item.type === "reasoning") {
385
+ const summary = event.item.summary ? event.item.summary.map((s, index) => ({
386
+ ...s,
387
+ index
388
+ })) : void 0;
389
+ additional_kwargs.reasoning = {
390
+ id: event.item.id,
391
+ type: event.item.type,
392
+ ...summary ? { summary } : {}
393
+ };
394
+ } else if (event.type === "response.reasoning_summary_part.added") additional_kwargs.reasoning = {
395
+ type: "reasoning",
396
+ summary: [{
397
+ ...event.part,
398
+ index: event.summary_index
399
+ }]
400
+ };
401
+ else if (event.type === "response.reasoning_summary_text.delta") additional_kwargs.reasoning = {
402
+ type: "reasoning",
403
+ summary: [{
404
+ text: event.delta,
405
+ type: "summary_text",
406
+ index: event.summary_index
407
+ }]
408
+ };
409
+ else if (event.type === "response.image_generation_call.partial_image") return null;
410
+ else return null;
411
+ return new __langchain_core_outputs.ChatGenerationChunk({
412
+ text: content.map((part) => part.text).join(""),
413
+ message: new __langchain_core_messages.AIMessageChunk({
414
+ id,
415
+ content,
416
+ tool_call_chunks,
417
+ usage_metadata,
418
+ additional_kwargs,
419
+ response_metadata
420
+ }),
421
+ generationInfo
422
+ });
423
+ };
424
+ /**
425
+ * Converts a single LangChain BaseMessage to OpenAI Responses API input format.
426
+ *
427
+ * This converter transforms a LangChain message into one or more ResponseInputItem objects
428
+ * that can be used with OpenAI's Responses API. It handles complex message structures including
429
+ * tool calls, reasoning blocks, multimodal content, and various content block types.
430
+ *
431
+ * @param message - The LangChain BaseMessage to convert. Can be any message type including
432
+ * HumanMessage, AIMessage, SystemMessage, ToolMessage, etc.
433
+ *
434
+ * @returns An array of ResponseInputItem objects.
435
+ *
436
+ * @example
437
+ * Basic text message conversion:
438
+ * ```typescript
439
+ * const message = new HumanMessage("Hello, how are you?");
440
+ * const items = convertStandardContentMessageToResponsesInput(message);
441
+ * // Returns: [{ type: "message", role: "user", content: [{ type: "input_text", text: "Hello, how are you?" }] }]
442
+ * ```
443
+ *
444
+ * @example
445
+ * AI message with tool calls:
446
+ * ```typescript
447
+ * const message = new AIMessage({
448
+ * content: "I'll check the weather for you.",
449
+ * tool_calls: [{
450
+ * id: "call_123",
451
+ * name: "get_weather",
452
+ * args: { location: "San Francisco" }
453
+ * }]
454
+ * });
455
+ * const items = convertStandardContentMessageToResponsesInput(message);
456
+ * // Returns:
457
+ * // [
458
+ * // { type: "message", role: "assistant", content: [{ type: "input_text", text: "I'll check the weather for you." }] },
459
+ * // { type: "function_call", call_id: "call_123", name: "get_weather", arguments: '{"location":"San Francisco"}' }
460
+ * // ]
461
+ * ```
462
+ */
463
+ const convertStandardContentMessageToResponsesInput = (message) => {
464
+ const isResponsesMessage = __langchain_core_messages.AIMessage.isInstance(message) && message.response_metadata?.model_provider === "openai";
465
+ function* iterateItems() {
466
+ const messageRole = require_misc.iife(() => {
467
+ try {
468
+ const role = require_misc.messageToOpenAIRole(message);
469
+ if (role === "system" || role === "developer" || role === "assistant" || role === "user") return role;
470
+ return "assistant";
471
+ } catch {
472
+ return "assistant";
473
+ }
474
+ });
475
+ let currentMessage = void 0;
476
+ const functionCallIdsWithBlocks = /* @__PURE__ */ new Set();
477
+ const serverFunctionCallIdsWithBlocks = /* @__PURE__ */ new Set();
478
+ const pendingFunctionChunks = /* @__PURE__ */ new Map();
479
+ const pendingServerFunctionChunks = /* @__PURE__ */ new Map();
480
+ function* flushMessage() {
481
+ if (!currentMessage) return;
482
+ const content = currentMessage.content;
483
+ if (typeof content === "string" && content.length > 0 || Array.isArray(content) && content.length > 0) yield currentMessage;
484
+ currentMessage = void 0;
485
+ }
486
+ const pushMessageContent = (content) => {
487
+ if (!currentMessage) currentMessage = {
488
+ type: "message",
489
+ role: messageRole,
490
+ content: []
491
+ };
492
+ if (typeof currentMessage.content === "string") currentMessage.content = currentMessage.content.length > 0 ? [{
493
+ type: "input_text",
494
+ text: currentMessage.content
495
+ }, ...content] : [...content];
496
+ else currentMessage.content.push(...content);
497
+ };
498
+ const toJsonString = (value) => {
499
+ if (typeof value === "string") return value;
500
+ try {
501
+ return JSON.stringify(value ?? {});
502
+ } catch {
503
+ return "{}";
504
+ }
505
+ };
506
+ const resolveImageItem = (block) => {
507
+ const detail = require_misc.iife(() => {
508
+ const raw = block.metadata?.detail;
509
+ if (raw === "low" || raw === "high" || raw === "auto") return raw;
510
+ return "auto";
511
+ });
512
+ if (block.fileId) return {
513
+ type: "input_image",
514
+ detail,
515
+ file_id: block.fileId
516
+ };
517
+ if (block.url) return {
518
+ type: "input_image",
519
+ detail,
520
+ image_url: block.url
521
+ };
522
+ if (block.data) {
523
+ const base64Data = typeof block.data === "string" ? block.data : Buffer.from(block.data).toString("base64");
524
+ const mimeType = block.mimeType ?? "image/png";
525
+ return {
526
+ type: "input_image",
527
+ detail,
528
+ image_url: `data:${mimeType};base64,${base64Data}`
529
+ };
530
+ }
531
+ return void 0;
532
+ };
533
+ const resolveFileItem = (block) => {
534
+ const filename = block.metadata?.filename ?? block.metadata?.name ?? block.metadata?.title;
535
+ if (block.fileId && typeof filename === "string") return {
536
+ type: "input_file",
537
+ file_id: block.fileId,
538
+ ...filename ? { filename } : {}
539
+ };
540
+ if (block.url && typeof filename === "string") return {
541
+ type: "input_file",
542
+ file_url: block.url,
543
+ ...filename ? { filename } : {}
544
+ };
545
+ if (block.data && typeof filename === "string") {
546
+ const encoded = typeof block.data === "string" ? block.data : Buffer.from(block.data).toString("base64");
547
+ const mimeType = block.mimeType ?? "application/octet-stream";
548
+ return {
549
+ type: "input_file",
550
+ file_data: `data:${mimeType};base64,${encoded}`,
551
+ ...filename ? { filename } : {}
552
+ };
553
+ }
554
+ return void 0;
555
+ };
556
+ const convertReasoningBlock = (block) => {
557
+ const summaryEntries = require_misc.iife(() => {
558
+ if (Array.isArray(block.summary)) {
559
+ const candidate = block.summary;
560
+ const mapped = candidate?.map((item) => item?.text).filter((text) => typeof text === "string") ?? [];
561
+ if (mapped.length > 0) return mapped;
562
+ }
563
+ return block.reasoning ? [block.reasoning] : [];
564
+ });
565
+ const summary = summaryEntries.length > 0 ? summaryEntries.map((text) => ({
566
+ type: "summary_text",
567
+ text
568
+ })) : [{
569
+ type: "summary_text",
570
+ text: ""
571
+ }];
572
+ const reasoningItem = {
573
+ type: "reasoning",
574
+ id: block.id ?? "",
575
+ summary
576
+ };
577
+ if (block.reasoning) reasoningItem.content = [{
578
+ type: "reasoning_text",
579
+ text: block.reasoning
580
+ }];
581
+ return reasoningItem;
582
+ };
583
+ const convertFunctionCall = (block) => ({
584
+ type: "function_call",
585
+ name: block.name ?? "",
586
+ call_id: block.id ?? "",
587
+ arguments: toJsonString(block.args)
588
+ });
589
+ const convertFunctionCallOutput = (block) => {
590
+ const output = toJsonString(block.output);
591
+ const status = block.status === "success" ? "completed" : block.status === "error" ? "incomplete" : void 0;
592
+ return {
593
+ type: "function_call_output",
594
+ call_id: block.toolCallId ?? "",
595
+ output,
596
+ ...status ? { status } : {}
597
+ };
598
+ };
599
+ for (const block of message.contentBlocks) if (block.type === "text") pushMessageContent([{
600
+ type: "input_text",
601
+ text: block.text
602
+ }]);
603
+ else if (block.type === "invalid_tool_call") {} else if (block.type === "reasoning") {
604
+ yield* flushMessage();
605
+ yield convertReasoningBlock(block);
606
+ } else if (block.type === "tool_call") {
607
+ yield* flushMessage();
608
+ const id = block.id ?? "";
609
+ if (id) {
610
+ functionCallIdsWithBlocks.add(id);
611
+ pendingFunctionChunks.delete(id);
612
+ }
613
+ yield convertFunctionCall(block);
614
+ } else if (block.type === "tool_call_chunk") {
615
+ if (block.id) {
616
+ const existing = pendingFunctionChunks.get(block.id) ?? {
617
+ name: block.name,
618
+ args: []
619
+ };
620
+ if (block.name) existing.name = block.name;
621
+ if (block.args) existing.args.push(block.args);
622
+ pendingFunctionChunks.set(block.id, existing);
623
+ }
624
+ } else if (block.type === "server_tool_call") {
625
+ yield* flushMessage();
626
+ const id = block.id ?? "";
627
+ if (id) {
628
+ serverFunctionCallIdsWithBlocks.add(id);
629
+ pendingServerFunctionChunks.delete(id);
630
+ }
631
+ yield convertFunctionCall(block);
632
+ } else if (block.type === "server_tool_call_chunk") {
633
+ if (block.id) {
634
+ const existing = pendingServerFunctionChunks.get(block.id) ?? {
635
+ name: block.name,
636
+ args: []
637
+ };
638
+ if (block.name) existing.name = block.name;
639
+ if (block.args) existing.args.push(block.args);
640
+ pendingServerFunctionChunks.set(block.id, existing);
641
+ }
642
+ } else if (block.type === "server_tool_call_result") {
643
+ yield* flushMessage();
644
+ yield convertFunctionCallOutput(block);
645
+ } else if (block.type === "audio") {} else if (block.type === "file") {
646
+ const fileItem = resolveFileItem(block);
647
+ if (fileItem) pushMessageContent([fileItem]);
648
+ } else if (block.type === "image") {
649
+ const imageItem = resolveImageItem(block);
650
+ if (imageItem) pushMessageContent([imageItem]);
651
+ } else if (block.type === "video") {
652
+ const videoItem = resolveFileItem(block);
653
+ if (videoItem) pushMessageContent([videoItem]);
654
+ } else if (block.type === "text-plain") {
655
+ if (block.text) pushMessageContent([{
656
+ type: "input_text",
657
+ text: block.text
658
+ }]);
659
+ } else if (block.type === "non_standard" && isResponsesMessage) {
660
+ yield* flushMessage();
661
+ yield block.value;
662
+ }
663
+ yield* flushMessage();
664
+ for (const [id, chunk] of pendingFunctionChunks) {
665
+ if (!id || functionCallIdsWithBlocks.has(id)) continue;
666
+ const args = chunk.args.join("");
667
+ if (!chunk.name && !args) continue;
668
+ yield {
669
+ type: "function_call",
670
+ call_id: id,
671
+ name: chunk.name ?? "",
672
+ arguments: args
673
+ };
674
+ }
675
+ for (const [id, chunk] of pendingServerFunctionChunks) {
676
+ if (!id || serverFunctionCallIdsWithBlocks.has(id)) continue;
677
+ const args = chunk.args.join("");
678
+ if (!chunk.name && !args) continue;
679
+ yield {
680
+ type: "function_call",
681
+ call_id: id,
682
+ name: chunk.name ?? "",
683
+ arguments: args
684
+ };
685
+ }
686
+ }
687
+ return Array.from(iterateItems());
688
+ };
689
+ /**
690
+ * - MCP (Model Context Protocol) approval responses
691
+ * - Zero Data Retention (ZDR) mode handling
692
+ *
693
+ * @param params - Conversion parameters
694
+ * @param params.messages - Array of LangChain BaseMessages to convert
695
+ * @param params.zdrEnabled - Whether Zero Data Retention mode is enabled. When true, certain
696
+ * metadata like message IDs and function call IDs are omitted from the output
697
+ * @param params.model - The model name being used. Used to determine if special role mapping
698
+ * is needed (e.g., "system" -> "developer" for reasoning models)
699
+ *
700
+ * @returns Array of ResponsesInputItem objects formatted for the OpenAI Responses API
701
+ *
702
+ * @throws {Error} When a function message is encountered (not supported)
703
+ * @throws {Error} When computer call output format is invalid
704
+ *
705
+ * @example
706
+ * ```typescript
707
+ * const messages = [
708
+ * new HumanMessage("Hello"),
709
+ * new AIMessage({ content: "Hi there!", tool_calls: [...] })
710
+ * ];
711
+ *
712
+ * const input = convertMessagesToResponsesInput({
713
+ * messages,
714
+ * zdrEnabled: false,
715
+ * model: "gpt-4"
716
+ * });
717
+ * ```
718
+ */
719
+ const convertMessagesToResponsesInput = ({ messages, zdrEnabled, model }) => {
720
+ return messages.flatMap((lcMsg) => {
721
+ const responseMetadata = lcMsg.response_metadata;
722
+ if (responseMetadata?.output_version === "v1") return convertStandardContentMessageToResponsesInput(lcMsg);
723
+ const additional_kwargs = lcMsg.additional_kwargs;
724
+ let role = require_misc.messageToOpenAIRole(lcMsg);
725
+ if (role === "system" && require_misc.isReasoningModel(model)) role = "developer";
726
+ if (role === "function") throw new Error("Function messages are not supported in Responses API");
727
+ if (role === "tool") {
728
+ const toolMessage = lcMsg;
729
+ if (additional_kwargs?.type === "computer_call_output") {
730
+ const output = (() => {
731
+ if (typeof toolMessage.content === "string") return {
732
+ type: "computer_screenshot",
733
+ image_url: toolMessage.content
734
+ };
735
+ if (Array.isArray(toolMessage.content)) {
736
+ const oaiScreenshot = toolMessage.content.find((i) => i.type === "computer_screenshot");
737
+ if (oaiScreenshot) return oaiScreenshot;
738
+ const lcImage = toolMessage.content.find((i) => i.type === "image_url");
739
+ if (lcImage) return {
740
+ type: "computer_screenshot",
741
+ image_url: typeof lcImage.image_url === "string" ? lcImage.image_url : lcImage.image_url.url
742
+ };
743
+ }
744
+ throw new Error("Invalid computer call output");
745
+ })();
746
+ return {
747
+ type: "computer_call_output",
748
+ output,
749
+ call_id: toolMessage.tool_call_id
750
+ };
751
+ }
752
+ if (toolMessage.additional_kwargs?.customTool) return {
753
+ type: "custom_tool_call_output",
754
+ call_id: toolMessage.tool_call_id,
755
+ output: toolMessage.content
756
+ };
757
+ return {
758
+ type: "function_call_output",
759
+ call_id: toolMessage.tool_call_id,
760
+ id: toolMessage.id?.startsWith("fc_") ? toolMessage.id : void 0,
761
+ output: typeof toolMessage.content !== "string" ? JSON.stringify(toolMessage.content) : toolMessage.content
762
+ };
763
+ }
764
+ if (role === "assistant") {
765
+ if (!zdrEnabled && responseMetadata?.output != null && Array.isArray(responseMetadata?.output) && responseMetadata?.output.length > 0 && responseMetadata?.output.every((item) => "type" in item)) return responseMetadata?.output;
766
+ const input = [];
767
+ if (additional_kwargs?.reasoning && !zdrEnabled) {
768
+ const reasoningItem = convertReasoningSummaryToResponsesReasoningItem(additional_kwargs.reasoning);
769
+ input.push(reasoningItem);
770
+ }
771
+ let { content } = lcMsg;
772
+ if (additional_kwargs?.refusal) {
773
+ if (typeof content === "string") content = [{
774
+ type: "output_text",
775
+ text: content,
776
+ annotations: []
777
+ }];
778
+ content = [...content, {
779
+ type: "refusal",
780
+ refusal: additional_kwargs.refusal
781
+ }];
782
+ }
783
+ if (typeof content === "string" || content.length > 0) input.push({
784
+ type: "message",
785
+ role: "assistant",
786
+ ...lcMsg.id && !zdrEnabled && lcMsg.id.startsWith("msg_") ? { id: lcMsg.id } : {},
787
+ content: require_misc.iife(() => {
788
+ if (typeof content === "string") return content;
789
+ return content.flatMap((item) => {
790
+ if (item.type === "text") return {
791
+ type: "output_text",
792
+ text: item.text,
793
+ annotations: item.annotations ?? []
794
+ };
795
+ if (item.type === "output_text" || item.type === "refusal") return item;
796
+ return [];
797
+ });
798
+ })
799
+ });
800
+ const functionCallIds = additional_kwargs?.[_FUNCTION_CALL_IDS_MAP_KEY];
801
+ if (__langchain_core_messages.AIMessage.isInstance(lcMsg) && !!lcMsg.tool_calls?.length) input.push(...lcMsg.tool_calls.map((toolCall) => {
802
+ if (require_tools.isCustomToolCall(toolCall)) return {
803
+ type: "custom_tool_call",
804
+ id: toolCall.call_id,
805
+ call_id: toolCall.id ?? "",
806
+ input: toolCall.args.input,
807
+ name: toolCall.name
808
+ };
809
+ return {
810
+ type: "function_call",
811
+ name: toolCall.name,
812
+ arguments: JSON.stringify(toolCall.args),
813
+ call_id: toolCall.id,
814
+ ...!zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}
815
+ };
816
+ }));
817
+ else if (additional_kwargs?.tool_calls) input.push(...additional_kwargs.tool_calls.map((toolCall) => ({
818
+ type: "function_call",
819
+ name: toolCall.function.name,
820
+ call_id: toolCall.id,
821
+ arguments: toolCall.function.arguments,
822
+ ...!zdrEnabled ? { id: functionCallIds?.[toolCall.id] } : {}
823
+ })));
824
+ const toolOutputs = (responseMetadata?.output)?.length ? responseMetadata?.output : additional_kwargs.tool_outputs;
825
+ const fallthroughCallTypes = [
826
+ "computer_call",
827
+ "mcp_call",
828
+ "code_interpreter_call",
829
+ "image_generation_call"
830
+ ];
831
+ if (toolOutputs != null) {
832
+ const castToolOutputs = toolOutputs;
833
+ const fallthroughCalls = castToolOutputs?.filter((item) => fallthroughCallTypes.includes(item.type));
834
+ if (fallthroughCalls.length > 0) input.push(...fallthroughCalls);
835
+ }
836
+ return input;
837
+ }
838
+ if (role === "user" || role === "system" || role === "developer") {
839
+ if (typeof lcMsg.content === "string") return {
840
+ type: "message",
841
+ role,
842
+ content: lcMsg.content
843
+ };
844
+ const messages$1 = [];
845
+ const content = lcMsg.content.flatMap((item) => {
846
+ if (item.type === "mcp_approval_response") messages$1.push({
847
+ type: "mcp_approval_response",
848
+ approval_request_id: item.approval_request_id,
849
+ approve: item.approve
850
+ });
851
+ if ((0, __langchain_core_messages.isDataContentBlock)(item)) return (0, __langchain_core_messages.convertToProviderContentBlock)(item, require_completions.completionsApiContentBlockConverter);
852
+ if (item.type === "text") return {
853
+ type: "input_text",
854
+ text: item.text
855
+ };
856
+ if (item.type === "image_url") {
857
+ const imageUrl = require_misc.iife(() => {
858
+ if (typeof item.image_url === "string") return item.image_url;
859
+ else if (typeof item.image_url === "object" && item.image_url !== null && "url" in item.image_url) return item.image_url.url;
860
+ return void 0;
861
+ });
862
+ const detail = require_misc.iife(() => {
863
+ if (typeof item.image_url === "string") return "auto";
864
+ else if (typeof item.image_url === "object" && item.image_url !== null && "detail" in item.image_url) return item.image_url.detail;
865
+ return void 0;
866
+ });
867
+ return {
868
+ type: "input_image",
869
+ image_url: imageUrl,
870
+ detail
871
+ };
872
+ }
873
+ if (item.type === "input_text" || item.type === "input_image" || item.type === "input_file") return item;
874
+ return [];
875
+ });
876
+ if (content.length > 0) messages$1.push({
877
+ type: "message",
878
+ role,
879
+ content
880
+ });
881
+ return messages$1;
882
+ }
883
+ console.warn(`Unsupported role found when converting to OpenAI Responses API: ${role}`);
884
+ return [];
885
+ });
886
+ };
887
+
888
+ //#endregion
889
+ exports.convertMessagesToResponsesInput = convertMessagesToResponsesInput;
890
+ exports.convertReasoningSummaryToResponsesReasoningItem = convertReasoningSummaryToResponsesReasoningItem;
891
+ exports.convertResponsesDeltaToChatGenerationChunk = convertResponsesDeltaToChatGenerationChunk;
892
+ exports.convertResponsesMessageToAIMessage = convertResponsesMessageToAIMessage;
893
+ exports.convertResponsesUsageToUsageMetadata = convertResponsesUsageToUsageMetadata;
894
+ exports.convertStandardContentMessageToResponsesInput = convertStandardContentMessageToResponsesInput;
895
+ //# sourceMappingURL=responses.cjs.map