From 2425f750dc04e6fa442c458ce35cff1ee3a73846 Mon Sep 17 00:00:00 2001 From: Jacob Lee Date: Thu, 21 Mar 2024 20:45:00 -0700 Subject: [PATCH] Ensure no null values are passed (#4854) --- libs/langchain-openai/src/chat_models.ts | 31 +++++++++++++++--------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/libs/langchain-openai/src/chat_models.ts b/libs/langchain-openai/src/chat_models.ts index a5bbbf2de9d0..6a433490001b 100644 --- a/libs/langchain-openai/src/chat_models.ts +++ b/libs/langchain-openai/src/chat_models.ts @@ -175,17 +175,26 @@ function _convertDeltaToMessageChunk( function convertMessagesToOpenAIParams(messages: BaseMessage[]) { // TODO: Function messages do not support array content, fix cast - return messages.map( - (message) => - ({ - role: messageToOpenAIRole(message), - content: message.content, - name: message.name, - function_call: message.additional_kwargs.function_call, - tool_calls: message.additional_kwargs.tool_calls, - tool_call_id: (message as ToolMessage).tool_call_id, - } as OpenAICompletionParam) - ); + return messages.map((message) => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const completionParam: Record = { + role: messageToOpenAIRole(message), + content: message.content, + }; + if (message.name != null) { + completionParam.name = message.name; + } + if (message.additional_kwargs.function_call != null) { + completionParam.function_call = message.additional_kwargs.function_call; + } + if (message.additional_kwargs.tool_calls != null) { + completionParam.tool_calls = message.additional_kwargs.tool_calls; + } + if ((message as ToolMessage).tool_call_id != null) { + completionParam.tool_call_id = (message as ToolMessage).tool_call_id; + } + return completionParam as OpenAICompletionParam; + }); } export interface ChatOpenAICallOptions