spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

OpenAI Audio: JSON transcription responses discard non-text metadata

Open woonjangahn opened this issue 1 year ago • 3 comments

Bug description
The AudioTranscriptionResponse or AudioTranscription implementation discards valuable JSON metadata (language, segments, duration, etc.) from OpenAI's audio transcription API responses. When responseFormat.isJsonType() is true, only the text field is preserved , despite the API returning structured JSON containing additional fields. This violates the principle of least surprise since users explicitly requesting JSON format expect full structured data.

Environment

  • Spring AI version: 1.1.0-SNAPSHOT (current main branch)
  • Java version: 21
  • Model Provider: OpenAI
  • Relevant classes: AudioTranscriptionResponse, AudioTranscription, AudioTranscriptionMetadata

Steps to reproduce

  1. Configure audio transcription with JSON response format:
    spring.ai.openai.audio.options.response-format=verbose_json
    
  2. Transcribe audio containing metadata (e.g., segments/timestamps)
  3. Inspect AudioTranscriptionResponse:
    AudioTranscriptionResponse response = client.call(prompt);
    response.getResults().get(0); // Only contains text
    response.getMetadata();   // Empty, lacks JSON body fields
    

Expected behavior

  • Full JSON response should be accessible in metadata
  • AudioTranscription should include:
    class AudioTranscription(
      // not sure if this should be a JSON String 
      // maybe nullable(or empty) fields such as segments and words in `AudioTranscription` or `AudioTranscriptionResponse` might work
      private final String text; 
      private AudioTranscriptionMetadata transcriptionMetadata;
    ) {}
    
  • Backward compatibility maintained for non-JSON responses

Minimal Complete Reproducible example

@Test
void shouldReturnFullJsonMetadata() {
	OpenAiAudioTranscriptionOptions transcriptionOptions = OpenAiAudioTranscriptionOptions.builder()
			.responseFormat(TranscriptResponseFormat.VERBOSE_JSON)
			.temperature(0f)
			.build();
	AudioTranscriptionPrompt transcriptionRequest = new AudioTranscriptionPrompt(this.audioFile,
			transcriptionOptions);
	AudioTranscriptionResponse response = this.transcriptionModel.call(transcriptionRequest);
	assertThat(response.getResults()).hasSize(1);

	// this fails
	assertThat(response.getResults().get(0))
			.isEqualTo(new AudioTranscription("[{\"id\":0,\"seek\":0,\"start\":0.0,\"end\":11.0,\"text\":\"And so my fellow Americans, ask not what your country can do for you, ask what you can do for your country.\",\"tokens\":[50364,400,370,452,7177,6280,11,1029,406,437,428,1941,393,360,337,291,11,1029,437,291,393,360,337,428,1941,13,50914],\"temperature\":0.0,\"avgLogprob\":-0.3053564,\"compressionRatio\":1.3544304,\"noSpeechProb\":0.03939722}]"));

	// or maybe metadata should not be empty?
	assertThat(response.getMetadata().isEmpty()).isTrue();
}

woonjangahn avatar Jun 01 '25 05:06 woonjangahn

The OpenAiAudioOptions class does not exist in 1.0.0+ Even if you meant the OpenAiAudioTranscriptionOptions class, the withResponseFormat method does not exist either. In the Builder class, methods starting with with are from an old approach and have been deprecated. Therefore, it seems that your version is not 1.0.0+. Please specify the specific version.

Could you please check again?

dev-jonghoonpark avatar Jun 01 '25 06:06 dev-jonghoonpark

@dev-jonghoonpark My apologies for the earlier mistake! I've gone ahead and corrected it. Could you please review the changes when you have a moment? Thank you for your patience!

woonjangahn avatar Jun 01 '25 22:06 woonjangahn

@woonjangahn Good issue discovered 👍

in, OpenAiAudioTranscriptionModel.java The verbose_json option is being successfully sent to OpenAI, and the results are being received correctly.

However, in the middle of the process, instead of retrieving the full result, only the text is being extracted. It seems this part could benefit from some improvement.

ResponseEntity<StructuredResponse> transcriptionEntity = this.retryTemplate
				.execute(ctx -> this.audioApi.createTranscription(request, StructuredResponse.class));

var transcription = transcriptionEntity.getBody();

if (transcription == null) {
	logger.warn("No transcription returned for request: {}", audioResource);
	return new AudioTranscriptionResponse(null);
}

AudioTranscription transcript = new AudioTranscription(transcription.text()); // <-- problem occurs in here

RateLimit rateLimits = OpenAiResponseHeaderExtractor.extractAiResponseHeaders(transcriptionEntity);

return new AudioTranscriptionResponse(transcript,
		OpenAiAudioTranscriptionResponseMetadata.from(transcriptionEntity.getBody())
			.withRateLimit(rateLimits));

Would you like to contribute to this? If not, please let me know.

dev-jonghoonpark avatar Jun 02 '25 05:06 dev-jonghoonpark