n8n-io / n8n

Free and source-available fair-code licensed workflow automation tool. Easily automate tasks across different services.
https://n8n.io
Other
45.19k stars 6.15k forks source link

OpenAi Node is throwing incorrect "Internal Error" #10206

Closed Alexandero89 closed 3 weeks ago

Alexandero89 commented 1 month ago

Bug Description

When you add an OpenAI Node which should message an Assistant, it is throwing an internal Error allthough the error is thrown on the OpenAI server. it should print/show the error message received by OpenAi.

I created an Assistant on OpenAI and chose the setting to give me JSON as a response. But i forgot to define how the json should look like. I then created a n8n node which is messaging this assistant. But every time it was executed it was throwing an "Internal Error" which was basically very cryptic or meaningless for me.

The logs also had no useful information: n8n-1 | 2024-07-26T09:00:51.983Z | verbose | Workflow execution finished with error "{\n error: {\n level: 'error',\n tags: {},\n extra: undefined,\n context: { runIndex: undefined, itemIndex: 0 },\n functionality: 'regular',\n name: 'NodeOperationError',\n timestamp: 1721984451982,\n node: {\n parameters: [Object],\n id: '3c65cfe0-9a01-4ae7-bd5c-1b03c7a7253d',\n name: 'OpenAI',\n type: '@n8n/n8n-nodes-langchain.openAi',\n typeVersion: 1.4,\n position: [Array],\n credentials: [Object]\n },\n messages: [],\n description: undefined,\n message: 'Internal error',\n stack: 'NodeOperationError: Internal error\\n' +\n ' at Object.router (/usr/local/lib/node_modules/n8n/node_modules/@n8n/n8n-nodes-langchain/dist/nodes/vendors/OpenAi/actions/router.js:78:19)\\n' +\n ' at processTicksAndRejections (node:internal/process/task_queues:95:5)\\n' +\n ' at Object.execute (/usr/local/lib/node_modules/n8n/node_modules/@n8n/n8n-nodes-langchain/dist/nodes/vendors/OpenAi/OpenAi.node.js:16:16)\\n' +\n ' at Workflow.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-workflow/dist/Workflow.js:728:19)\\n' +\n ' at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:673:51\\n' +\n ' at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/WorkflowExecute.js:1085:20'\n },\n workflowId: 'LvuKPyER5qSad8IM',\n file: 'LoggerProxy.js',\n function: 'exports.verbose'\n}"

I only found out when using the OpenAI dashboard, because it shows the Error: Error streaming run: Thread messages or instructions must contain the word 'json' in some form to use 'response_format' of type 'json_object'.

After deactivating the "JSON output" in the assistant everything worked fine.

To Reproduce

  1. Create OpenAI Assistant
  2. Set Output of Assistant to "JSON" without defining the json format inside the instructions or prompt of the assistant
  3. Create n8n node and set it to "message an assistant"
  4. Execute the workflow with any given input
  5. OpenAi node is throwing "Internal Error"

Expected behavior

It would be nice if there would be two improvements. 1) Generally better logging of n8n: Allthough i set the verbose level of n8n to verbose/debug i got no useful information why this error is happening. There was just a generic "internal error" without any description. Also see the above log line.

2) The OpenAI node should not throw an generic "Internal Error": The node should not throw an exception. Instead it should check the (hoepefully correct) response from OpenAI that is telling something like "missing json declaration" and show it to the user.

I would love to help with this, but after pulling the n8n github repo i could not figure out where the code of the OpenAI node was saved. If you point me to the source code or where exactly i can push a PR i would love to help you guys.

Operating System

docker compose

n8n Version

1.52.0

Node.js Version

20

Database

SQLite (default)

Execution mode

main (default)

Joffcom commented 1 month ago

@michael-radency is this something fixed in one of your recent PRs for the error handling?

michael-radency commented 3 weeks ago

Hello @Alexandero89, thanks for reporting this issue, there was some updates to errors handling recently, could you confirm that this issue still persist?

Could you provide the workflow with example?

Alexandero89 commented 3 weeks ago

Hey @michael-radency

Just recreated the error and everything seems to work fine now. Instead of a generic internal error i get:

Full message

400 Thread messages or instructions must contain the word 'json' in some form to use 'response_format' of type 'json_object'.

And thats absolutly understandable and the correct error message.

Thanks for your effort!

janober commented 2 weeks ago

Fix got released with n8n@1.56.0