Skip to content

fix: convert mislabeled audio before STT#9329

Open
zzz27578 wants to merge 2 commits into
AstrBotDevs:masterfrom
zzz27578:fix/convert-mislabeled-audio-before-stt
Open

fix: convert mislabeled audio before STT#9329
zzz27578 wants to merge 2 commits into
AstrBotDevs:masterfrom
zzz27578:fix/convert-mislabeled-audio-before-stt

Conversation

@zzz27578

@zzz27578 zzz27578 commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • verify audio magic bytes before skipping same-extension conversion
  • convert non-WAV payloads that were materialized with a .wav suffix
  • preserve the existing Ogg/Opus no-op behavior
  • add regression coverage for AMR bytes stored under a .wav filename

Root cause

MediaResolver can materialize extensionless or base64 audio with the default .wav suffix. ensure_wav() correctly detects that the bytes are not WAV and requests WAV conversion, but convert_audio_format() previously returned early based only on the filename suffix. The unchanged non-RIFF payload was then rejected by the MiMo STT boundary validation.

This change keeps the fast path only when both the suffix and detected audio format match the requested output format.

Follow-up to #9113 and #9118.

Validation

  • uv run pytest tests/test_media_utils.py -q -k "not test_file_uri_to_path_supports_localhost_and_encoded_paths" ? 50 passed, 1 Windows-specific path test deselected
  • uv run pytest tests/test_mimo_api_sources.py -q ? 19 passed
  • uv run ruff check astrbot/core/utils/media_utils.py tests/test_media_utils.py
  • uv run ruff format --check astrbot/core/utils/media_utils.py tests/test_media_utils.py
  • manual Windows verification with aiocqhttp + MiMo STT: a QQ voice message was converted, transcribed correctly, and produced the expected 111 response

Summary by Sourcery

Ensure audio format conversion validates the underlying magic bytes instead of relying solely on file extensions before speech-to-text processing.

Bug Fixes:

  • Fix WAV conversion for non-WAV audio payloads that were materialized with a .wav suffix to avoid STT boundary rejection.

Tests:

  • Add regression test verifying ensure_wav converts non-WAV AMR bytes stored under a .wav filename and produces a valid RIFF/WAV output.

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend labels Jul 20, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enhances the convert_audio_format function in astrbot/core/utils/media_utils.py to verify the actual audio format using _get_audio_magic_type rather than relying solely on the file extension, and adds a corresponding unit test. The reviewer correctly identified that calling _get_audio_magic_type synchronously inside an asynchronous function will block the asyncio event loop, and suggested offloading this operation to a separate thread using asyncio.to_thread.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread astrbot/core/utils/media_utils.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core The bug / feature is about astrbot's core, backend size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant