fix(sdk/python): generate_music no longer hardcodes stream=true for non-pcm16 formats - #963
Open
AbirAbbas wants to merge 1 commit into
Open
fix(sdk/python): generate_music no longer hardcodes stream=true for non-pcm16 formats#963AbirAbbas wants to merge 1 commit into
AbirAbbas wants to merge 1 commit into
Conversation
…eaming OpenRouter path (#584) OpenRouterProvider.generate_music hardcoded "stream": True with a default format of "wav", so the default call reproduced #584's 400: 'audio.format' does not support 'wav' when stream=true. Supported values are: 'pcm16' PR #962 fixed exactly this for generate_audio but left generate_music untouched. Apply the same routing here, reusing the helpers #962 added rather than duplicating them: - wav -> request pcm16 over the wire, stream over SSE, then re-wrap the pcm16 payload as a RIFF/WAVE container via _wrap_pcm16_as_wav_b64 - pcm16 -> stream over SSE, returned base64 untouched - mp3 / flac / opus -> stream=False via _nonstream_openrouter_audio with label="music" Everything else about generate_music is unchanged: prompt/duration-hint message shape, model resolution and openrouter/ prefix stripping, duration validation, and the returned MultimodalResponse fields. Two existing SSE tests requested the default wav format while asserting the raw streamed base64 — only the pcm16 wire path can return that, so they are pinned to format="pcm16" and now assert stream is True, the same treatment #962 gave the generate_audio tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
OpenRouterProvider.generate_musicstill hardcoded"stream": Truewith a default format ofwav, so the plaingenerate_music("...")call reproduced issue #584's 400:'audio.format' does not support 'wav' when stream=true. Supported values are: 'pcm16'. PR #962 fixed exactly this forgenerate_audiobut did not touchgenerate_music. This applies the same format-driven transport routing togenerate_music, reusing the helpers #962 added (_nonstream_openrouter_audio,_wrap_pcm16_as_wav_b64) rather than duplicating them:wav→ wirepcm16over SSE then re-wrapped client-side as RIFF/WAVE,pcm16→ SSE unchanged,mp3/flac/opus→stream=Falsevia the non-streaming JSON path withlabel="music".Everything else about
generate_musicis unchanged — prompt/duration-hint message shape, model resolution andopenrouter/prefix stripping, duration validation, and the returnedMultimodalResponsefields — and there is a test pinning that.Type of change
Test plan
All commands run from
sdk/python/.uvx ruff@0.15.22 check .→All checks passed!uvx ruff@0.15.22 format agentfield/media_providers.py tests/test_openrouter_audio.py tests/test_media_integration.pythenuvx ruff@0.15.22 format --check <same files>→3 files already formatteduv run --extra dev ./scripts/run_pytest.sh -p no:cacheprovider→2009 passed, 4 skipped, 38 deselected in 112.36s, exit 0; coverageTOTAL 2002 stmts / 116 miss / 94%uv run --python 3.10 --extra dev ./scripts/run_pytest.sh -p no:cacheprovider --no-cov -q→ exit 0 (tests=2013, failures=0, errors=0, skipped=4from the JUnit summary; this project's-qrun does not print the trailing counts line under 3.10)uv run --extra dev python -m pytest tests/test_openrouter_audio.py tests/test_media_integration.py -p no:cacheprovider --no-cov -q→68 passedRed-before-green check: with the
media_providers.pychange reverted and only the new tests in place,pytest tests/test_openrouter_audio.py::TestOpenRouterMusicFormatRoutingfails 4 of 7 —test_default_format_streams_pcm16_and_returns_wav,test_compressed_formats_use_nonstream_json[mp3],test_compressed_formats_use_nonstream_json[flac],test_request_shape_is_identical_across_transports[mp3-False]— i.e. every contract item that describes new behaviour genuinely fails against the old code.Validation contract
Behaviours this change must exhibit, and the test each maps to (all in
sdk/python/tests/test_openrouter_audio.py, classTestOpenRouterMusicFormatRouting, asserting on the payload actually POSTed and on the returned audio via the same aiohttp fake pattern thegenerate_audiotests use):generate_music(prompt=...)call (noformat) sendsstream: Truewithaudio.format == "pcm16"on the wire, and returns audio whoseformatis"wav"and whose base64 decodes to a RIFF/WAVE container still containing the streamed pcm bytes.test_default_format_streams_pcm16_and_returns_wavformat="pcm16"sendsstream: Truewith wire formatpcm16and returns the concatenated base64 verbatim, format"pcm16".test_pcm16_streams_and_returns_data_untouchedformat="mp3"andformat="flac"sendstream: Falseto/chat/completionswith the requested wire format, and the result is parsed fromchoices[0].message.audio.{data,transcript}.test_compressed_formats_use_nonstream_json[mp3],[flac]RuntimeErrorwhose message contains"music"and the status code.test_nonstream_http_error_mentions_music_and_status(402 +opus)google/lyria-3-pro,openrouter/prefix stripped,messages == [{"role": "user", "content": "<prompt> (duration: N seconds)"}],modalities == ["text", "audio"]; duration validation still raisesValueErrorbefore any HTTP call.test_request_shape_is_identical_across_transports[wav/pcm16/mp3],test_duration_validation_runs_before_any_requestTwo existing tests were re-pinned, not reshaped
test_openrouter_generate_music_streamsandtest_media_integration.py::test_music_generation_returns_audiorequested the defaultwavformat while asserting the raw streamed base64 came back. Only thepcm16wire path can return that, so both are now pinned toformat="pcm16"and additionally assertstream is True— the same treatment #962 gave the twogenerate_audioSSE tests. Their intent (SSE parsing →AudioOutput) is preserved; the new C1 test covers what the defaultwavcall now does.Note on the diff
ruff formatreflowed two pre-existing f-strings inside_nonstream_openrouter_audio(added by #962, which left them not format-clean). Those two hunks are mechanical and are required forruff format --checkon this file to pass; no other unrelated formatting was touched.The WAV re-wrap uses
sample_rate=24000, matchinggenerate_audioexactly so the two paths stay consistent. Thegenerate_musicdocstring's "48kHz stereo" line describes the upstream model output, not the client-side mono RIFF header; if OpenRouter's music pcm16 stream turns out to be 48 kHz, that constant should change in both call sites together.Test coverage
generate_music(non-stream dispatch, wav re-wrap) are exercised by C1/C3/C4.coverage-baseline.jsonin this PR only if the removal caused a legitimate regression and I called it out in the summary above. No code removed; no baseline change.Checklist
Related issues / PRs
Refs #584
Refs #962