You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

调用OpenAI Whisper-1转写音频报错:'Transcription'对象不可下标访问

问题解决:'Transcription' object is not subscriptable 报错

你遇到的问题是因为OpenAI Python SDK返回的Transcription是一个对象,不是字典,不能用方括号[]来访问属性。

错误原因

你写的transcript['transcription']['text']是字典的访问方式,但实际返回的transcript是一个包含转写结果的对象,直接通过点语法就能拿到文本内容。

修正后的代码

from openai import OpenAI

client = OpenAI(api_key="my_key")

audio_file_path ="C:/Users/name/OneDrive/VS Folder/speech.mp3"

with open(audio_file_path, "rb") as audio_file:
    try:
        transcript = client.audio.transcriptions.create(
            model="whisper-1", 
            file=audio_file
        )
        
        # 直接用点语法访问text属性
        transcribed_text = transcript.text
        
        # 打印转写结果
        print(transcribed_text)
        
    except Exception as e:
        print(f"An error occurred: {e}")

额外说明

如果需要查看Transcription对象的所有属性,可以打印dir(transcript)或者直接打印transcript,会看到它包含text、language等属性,都可以通过点语法访问。

内容的提问来源于stack exchange,提问作者TheAnalyst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 15:52:03