openai-node icon indicating copy to clipboard operation
openai-node copied to clipboard

Error thrown if `type?: function` is not specified specified in `tool_choice`

Open huypham50 opened this issue 2 years ago • 3 comments

Confirm this is a Node library issue and not an underlying OpenAI API issue

  • [X] This is an issue with the Node library

Describe the bug

const response = await openai.chat.completions.create({
  model: 'gpt-4-1106-preview',
  messages: [{
    role: 'user',
    content: prompt,
  }],
  tool_choice: {
    type: 'function', // this prop is optional but if you remove it an error will be thrown (documented below)
    function: {
      name,
    }
  },
  tools: [
    {
      type: 'function',
      function: {
        name,
        description,
        parameters
      }
    }
  ],
})
error: {
  message: "'$.tool_choice' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.",
  type: 'invalid_request_error',
  param: null,
  code: null
},
code: null,
param: null,
type: 'invalid_request_error'

To Reproduce

See above.

Also, it looks like parallel function calling is disabled if tool_choice is specified, is this intended?

Code snippets

No response

OS

macOS

Node version

v18.12.1

Library version

^4.19.0

huypham50 avatar Nov 21 '23 18:11 huypham50

Thanks, I'll look into whether type: 'function' should be required in the client (which I think is the case) or whether the API should be relaxed. I'm not sure you should expect a response this week, however.

Also, it looks like parallel function calling is disabled if tool_choice is specified, is this intended?

Yes, that is intended.

rattrayalex avatar Nov 22 '23 00:11 rattrayalex

Thanks for the quick fix!

Also, it looks like parallel function calling is disabled if tool_choice is specified, is this intended?

Yes, that is intended.

I don't think this should be the case. I can specify the function that will be called and it should be able to run in parallel as well.

If we take a look at the example in the docs, get_current_weather should be able to process all 3 cities and not just 1. If you specify tool_choice, the response will only include the first city (SF).

huypham50 avatar Nov 22 '23 05:11 huypham50

I see your point, and you're welcome to suggest that at community.openai.com where the backend teams monitor for API feedback.

For now, you can of course work around this by only passing the tool you want called and omitting tool_choice.

rattrayalex avatar Nov 22 '23 19:11 rattrayalex