Skip to content

Commit

Permalink
fix(OpenAI Node): Throw node operations error in case of openAi clien…
Browse files Browse the repository at this point in the history
…t error (#10448)

Co-authored-by: Shireen Missi <[email protected]>
  • Loading branch information
michael-radency and ShireenMissi authored Aug 16, 2024
1 parent 77ebd93 commit 0d3ed46
Showing 1 changed file with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import { OpenAIAssistantRunnable } from 'langchain/experimental/openai_assistant
import type { OpenAIToolType } from 'langchain/dist/experimental/openai_assistant/schema';
import { OpenAI as OpenAIClient } from 'openai';

import { NodeConnectionType, NodeOperationError, updateDisplayOptions } from 'n8n-workflow';
import {
ApplicationError,
NodeConnectionType,
NodeOperationError,
updateDisplayOptions,
} from 'n8n-workflow';
import type {
IDataObject,
IExecuteFunctions,
Expand Down Expand Up @@ -228,25 +233,36 @@ export async function execute(this: IExecuteFunctions, i: number): Promise<INode
}
}

const response = await agentExecutor.withConfig(getTracingConfig(this)).invoke(chainValues);
if (memory) {
await memory.saveContext({ input }, { output: response.output });
let filteredResponse: IDataObject = {};
try {
const response = await agentExecutor.withConfig(getTracingConfig(this)).invoke(chainValues);
if (memory) {
await memory.saveContext({ input }, { output: response.output });

if (response.threadId && response.runId) {
const threadRun = await client.beta.threads.runs.retrieve(response.threadId, response.runId);
response.usage = threadRun.usage;
if (response.threadId && response.runId) {
const threadRun = await client.beta.threads.runs.retrieve(
response.threadId,
response.runId,
);
response.usage = threadRun.usage;
}
}
}

if (
options.preserveOriginalTools !== false &&
nodeVersion >= 1.3 &&
(assistantTools ?? [])?.length
) {
await client.beta.assistants.update(assistantId, {
tools: assistantTools,
});
if (
options.preserveOriginalTools !== false &&
nodeVersion >= 1.3 &&
(assistantTools ?? [])?.length
) {
await client.beta.assistants.update(assistantId, {
tools: assistantTools,
});
}
filteredResponse = omit(response, ['signal', 'timeout']) as IDataObject;
} catch (error) {
if (!(error instanceof ApplicationError)) {
throw new NodeOperationError(this.getNode(), error.message, { itemIndex: i });
}
}
const filteredResponse = omit(response, ['signal', 'timeout']);

return [{ json: filteredResponse, pairedItem: { item: i } }];
}

0 comments on commit 0d3ed46

Please sign in to comment.