Skip to content

Map audio/mpeg to the OpenAI mp3 input audio format - #6710

Open
chabinhwang wants to merge 1 commit into
spring-projects:mainfrom
chabinhwang:openai-audio-mpeg-input-format
Open

Map audio/mpeg to the OpenAI mp3 input audio format#6710
chabinhwang wants to merge 1 commit into
spring-projects:mainfrom
chabinhwang:openai-audio-mpeg-input-format

Conversation

@chabinhwang

Copy link
Copy Markdown
Contributor

Summary

The OpenAI audio input format was chosen with mimeType.contains("mp3"), falling back to WAV for anything else.
audio/mpeg is the media type registered for MP3 (RFC 3003) and does not contain the substring mp3, so MP3 audio declared that way was silently sent to OpenAI labeled as wav.

This moves the decision into toInputAudioFormat(String), which accepts audio/mp3 and audio/mpeg (case-insensitively) for MP3 and warns when an audio type that is neither MP3 nor WAV is forwarded as WAV.

Why audio/mpeg matters

Media.Format exposes no audio constants, so callers build the MIME type themselves, and audio/mpeg is what standard detection produces for an .mp3 file.
Spring AI's own speech documentation uses audio/mpeg for MP3 output, so feeding generated speech back in as chat input hits exactly this path.
The failure is silent at the Spring AI layer — the request is built and sent with the wrong format declaration.

The reference documentation previously listed only audio/mp3 and audio/wav; it now lists audio/mpeg as well.

Testing

Two parameterized tests added to OpenAiChatModelTests, both going through createRequest so they assert on the ChatCompletionCreateParams actually sent:

  • userMessageMp3MediaMapsToMp3InputAudioFormataudio/mp3, audio/mpeg, audio/MPEG
  • userMessageWavMediaMapsToWavInputAudioFormataudio/wav, audio/x-wav, audio/vnd.wave

Both also assert the base64 payload is unchanged.

Verified locally:

  • ./mvnw -Dmaven.build.cache.enabled=false -pl models/spring-ai-openai clean packageTests run: 176, Failures: 0, Errors: 0, checkstyle and spring-javaformat pass
  • Reverted only the audio/mpeg handling and re-ran the new tests: the audio/mpeg and audio/MPEG cases fail with expected: mp3 but was: wav, while the audio/mp3 and WAV cases pass either way

Integration tests requiring an OpenAI API key were not executed.

Scope

Only the input audio format resolution and the corresponding documentation line changed. The output audio path and the transcription/speech models are untouched.

The audio input format was derived by looking for "mp3" in the media
MIME type, so "audio/mpeg", the media type registered for MP3 and the
one Spring's MimeType detection returns, was silently sent to OpenAI as
wav. Resolve the format in a dedicated method that also accepts
"audio/mpeg" and warns when an unsupported audio type is forwarded.

Signed-off-by: chabinhwang <7chabin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants