openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
22.67k stars 3.16k forks source link

Assistants API: inconsistency between API reference and a migration guide #1335

Closed amureki closed 6 months ago

amureki commented 6 months ago

Confirm this is an issue with the Python library and not an underlying OpenAI API

Describe the bug

Greetings fellows,

A migration guide for assistant file upload suggests using the following format:

"attachments": [
    {
      "file_id": "file-123",
      "tools": [
        { "type": "file_search" },
        { "type": "code_interpreter" }
      ]
    }
  ]

However, the API reference shows something else: https://platform.openai.com/docs/api-reference/messages/createMessage#messages-createmessage-attachments

Whenever I am using the version explained in the migration guide, I get the following traceback:

Failed to run listener function (error: Error code: 400 - {'error': {'message': "Missing required parameter: 'attachments[0].tools'.", 'type': 'invalid_request_error', 'param': 'attachments[0].tools', 'code': 'missing_required_parameter'}})

To Reproduce

  1. Try to migrate existing code that uses assistants API with file uploads to the new version
  2. Get stuck. :(

Code snippets

I think, this is the suspected piece in the Python library that differs: openai.types.beta.threads.message_create_params.Attachment

class Attachment(TypedDict, total=False):
    add_to: List[Literal["file_search", "code_interpreter"]]

    file_id: str
    """The ID of the file to attach to the message."""

It expects add_to and not a tools list.

OS

macOS

Python version

Python v3.12.1

Library version

openai v1.21.2

sdcoffey commented 6 months ago

Hey @amureki!

Thanks for reporting this. This was actually a bug in the docs, which have now been fixed. The correct parameter name is tools. I'm going to close this for now, we appreciate your feedback!

amureki commented 6 months ago

@sdcoffey oh, thanks for the swift resolution. 👍

zedef99 commented 5 months ago

Hi, I have a similar issue while attaching a file to a message:

message = client.beta.threads.messages.create(
            thread_id=thread_id,
            role="user",
            content=question,
            attachments=[
                {"file_id":file1_id},
                {"tools":[{"type":"code_interpreter"}]}
            ]
    )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\resources\beta\threads\messages.py", line 87, in create
    return self._post(
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\_base_client.py", line 1240, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\_base_client.py", line 921, in request
    return self._request(
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\_base_client.py", line 1020, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Missing required parameter: 'attachments[0].tools'.", 'type': 'invalid_request_error', 'param': 'attachments[0].tools', 'code': 'missing_required_parameter'}}

Os

windows

Library Version

openai 1.27.0

Python Version

python 3.10.0

Clive321A commented 4 months ago

Hi, I have a similar issue while attaching a file to a message:

message = client.beta.threads.messages.create(
            thread_id=thread_id,
            role="user",
            content=question,
            attachments=[
                {"file_id":file1_id},
                {"tools":[{"type":"code_interpreter"}]}
            ]
    )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\resources\beta\threads\messages.py", line 87, in create
    return self._post(
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\_base_client.py", line 1240, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\_base_client.py", line 921, in request
    return self._request(
  File "C:\Users\f.stragliotto\Desktop\va question_test\.venv\lib\site-packages\openai\_base_client.py", line 1020, in _request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Missing required parameter: 'attachments[0].tools'.", 'type': 'invalid_request_error', 'param': 'attachments[0].tools', 'code': 'missing_required_parameter'}}

Os

windows

Library Version

openai 1.27.0

Python Version

python 3.10.0

Did you ever resolve this issue?, I have a similar issue, as far as I can tell my request is OK.

{
   "role":"user",
   "content":[
      {
         "type":"text",
         "text":"Hello"
      }
   ],
   "attachments":[
      {
         "file_id":"file-5qRBphw6nYJOcbPElxZWbnOz",
         "tools":[
            {
               "type":"file_search"
            }
         ]
      }
   ]
}