Skip to content

[BUG] Incorrect MIME Type for MP3 Files #390

@kengreeff

Description

@kengreeff

Basic checks

  • I searched existing issues - this hasn't been reported
  • I can reproduce this consistently
  • This is a RubyLLM bug, not my application code

What's broken?

When using MP3 files (without ActiveStorage) as attachments you get the following:

RubyLLM::ErrorMiddleware.parse_error': Invalid value: 'mpeg'. Supported values are: 'wav' and 'mp3'. (RubyLLM::BadRequestError)

How to reproduce

Run rails c

chat = RubyLLM.chat(model: 'gpt-4o-audio-preview').ask("whisper.mp3")
DEBUG -- RubyLLM: request: POST https://api.openai.com/v1/chat/completions
DEBUG -- RubyLLM: request: {"model":"gpt-4o-audio-preview","messages":[{"role":"user","content":[{"type":"text","text":"Please transcribe this audio file in vvt format."},{"type":"input_audio","input_audio":{"data":"data":"[BASE64 DATA]"","format":"mpeg"}}]}],"stream":false}
DEBUG -- RubyLLM: error: /ruby-3.4.2/gems/ruby_llm-1.6.4/lib/ruby_llm/error.rb:52:in 'RubyLLM::ErrorMiddleware.parse_error': Invalid value: 'mpeg'. Supported values are: 'wav' and 'mp3'. (RubyLLM::BadRequestError)

Expected behavior

I would expect that the file is uploaded and transcoded. It works using the OpenAI API playground

What actually happened

I expect the cause is here:

https://github.com/crmne/ruby_llm/blob/791f28b608072f8177eaffdd8c260acc6dfa4018/lib/ruby_llm/providers/openai/media.rb

def format_audio(audio)
    {
    type: 'input_audio',
    input_audio: {
        data: audio.encoded,
        format: audio.mime_type.split('/').last
    }
    }
end

The mime type for mp3 is:

file --mime "whisper.mp3"
whisper.mp3: audio/mpeg; charset=binary

Which sets the format to mpeg

I'm happy to take a swing at this but would need to set to mp3 if mime_type was audio/mpeg and extension was also mp3, otherwise fallback to the original method.

Thoughts?

Environment

  • ruby-3.4.2
  • ruby_llm-1.6.4
  • openai
  • mac os 15.6.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions