iflytek/astron-agent · error · ValueError

音频转换失败

Error message

音频转换失败: {e}

What it means

convert_to_wav raises ValueError('音频转换失败: ...') when any step of decoding the input audio and re-encoding it to WAV fails. This is a plain ValueError (not the service exception type), chained with `from e` to preserve the underlying cause (unsupported format, corrupt data, missing decoder).

Solutions

  1. Read the chained cause (`from e`) to see the exact decode failure.
  2. Run detect_audio_format/validate_audio_format on the input before converting and reject unsupported formats early.
  3. Verify the input file is complete and matches its declared format.
  4. Ensure required audio codecs/ffmpeg are installed in the runtime image.

Example fix

// before
wav, props = AudioConverter.convert_to_wav(audio_bytes)

// after
ok, fmt = AudioConverter.validate_audio_format(audio_bytes)
if not ok:
    raise HTTPException(400, f"unsupported audio format: {fmt}")
try:
    wav, props = AudioConverter.convert_to_wav(audio_bytes)
except ValueError as e:
    raise HTTPException(400, str(e)) from e
Defensive patterns

Strategy: validation

Validate before calling

ok, fmt = AudioConverter.validate_audio_format(audio_bytes)
if not ok:
    raise HTTPException(status_code=400, detail=f'unsupported audio: {fmt}')

Try / catch

try:
    wav, props = AudioConverter.convert_to_wav(audio_bytes)
except ValueError as e:
    logger.warning(f"audio conversion failed: {e}")
    raise HTTPException(status_code=400, detail=str(e)) from e

Prevention

When it happens

Trigger: Calling convert_to_wav with bytes that AudioConverter cannot decode: corrupt/truncated audio, unsupported container/codec, empty input, or an ffmpeg/decoder environment problem.

Common situations: Users uploading exotic formats (e.g. AMR, opus in unusual containers), truncated uploads, wrong file extension vs real content, missing audio decoding library in the deployment image.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of iflytek/astron-agent@5e758547a8 (2026-09-12). Data as JSON: /api/errors/d7acfdd3cf698fda. Report an issue: GitHub.

Appendix: source

Thrown at core/plugin/aitools/service/ise/ise_client.py:140

            else:
                # Try to auto-detect the format.
                audio = AudioSegment.from_file(io.BytesIO(audio_data))

            # Convert the audio to the target format: 16kHz, 16bit, 1 channel.
            audio = audio.set_frame_rate(16000)  # Set the sample rate to 16kHz.
            audio = audio.set_sample_width(2)  # Set the sample width to 16bit.
            audio = audio.set_channels(1)  # Set the number of channels to 1.

            # Export the audio as WAV format.
            wav_io = io.BytesIO()
            audio.export(wav_io, format="wav")
            wav_data = wav_io.getvalue()
            wav_io.close()

            return wav_data, original_properties

        except Exception as e:
            raise ValueError(f"音频转换失败: {e}") from e

    @staticmethod
    def validate_audio_format(audio_data: bytes) -> Tuple[bool, str]:
        """Validate the audio format."""
        try:
            format_type = AudioConverter.detect_audio_format(audio_data)
            if format_type == "wav":
                audio = AudioSegment.from_wav(io.BytesIO(audio_data))
                if (
                    audio.frame_rate == 16000
                    and audio.sample_width == 2
                    and audio.channels == 1
                ):
                    return True, "音频格式符合要求"
                return (
                    False,
                    f"WAV格式不符合要求: {audio.frame_rate}Hz,\
                            {audio.sample_width * 8}bit, {audio.channels}声道",

View on GitHub (pinned to 5e758547a8)