Skip to content

Conversation

okorepanov
Copy link
Contributor

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?

Related Open Issues

#521

Description

The problem was initially raised out of jsonl file uploading, which led to error below:

# OpenAI HTTP Error (spotted in ruby-openai 7.1.0): {"error"=>{"code"=>"invalidPayload", "message"=>"The file has no or an empty content type specified."}}
# Faraday::BadRequestError: the server responded with status 400

Previously there was empty content-type hardcoded value while sending http parameters - https://github.com/alexrudall/ruby-openai/blob/main/lib/openai/http.rb#L91-L94

In order to fix an issue with jsonl file, we were patching the gem in our project, writing something like that:

def multipart_parameters(parameters)
  parameters&.transform_values do |value|
    next value unless value.respond_to?(:close) # File or IO object.

    # Faraday::UploadIO does not require a path, so we will pass it
    # only if it is available. This allows StringIO objects to be
    # passed in as well.
    path = value.respond_to?(:path) ? value.path : nil

    mime_type = path&.end_with?('.jsonl') ? Mime[:json].to_s : ''
    Faraday::UploadIO.new(value, mime_type, path)
  end
end

In scope of this PR I'm trying to handle this logic via usage of MIME::Types (mime-types gem).
In order to jsonl file to be recognised as application/json content type we had to manually register it upon project building, that's why I've created OpenAI::MimeTypes module, where we also call .register to register some custom types (might be extended in future).

I've tested it locally, sending jsonl file for batch purposes - it've worked as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant