UnicodeDecodeError with FastAPI when Handling Audio Files: How to Resolve?

I’m encountering a UnicodeDecodeError in my FastAPI application while processing audio files. The error traceback is as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Traceback (most recent call last):
File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesuvicornprotocolshttphttptools_impl.py", line 399, in run_asgi
result = await app( # type: ignore[func-returns-value]
...
File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 303, in jsonable_encoder
jsonable_encoder(
File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 289, in jsonable_encoder
encoded_value = jsonable_encoder(
File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 318, in jsonable_encoder
return ENCODERS_BY_TYPE[type(obj)](obj)
File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 59, in <lambda>
bytes: lambda o: o.decode(),
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9f in position 144: invalid start byte
</code>
<code>Traceback (most recent call last): File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesuvicornprotocolshttphttptools_impl.py", line 399, in run_asgi result = await app( # type: ignore[func-returns-value] ... File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 303, in jsonable_encoder jsonable_encoder( File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 289, in jsonable_encoder encoded_value = jsonable_encoder( File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 318, in jsonable_encoder return ENCODERS_BY_TYPE[type(obj)](obj) File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 59, in <lambda> bytes: lambda o: o.decode(), UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9f in position 144: invalid start byte </code>
Traceback (most recent call last):
  File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesuvicornprotocolshttphttptools_impl.py", line 399, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  ...
  File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 303, in jsonable_encoder
    jsonable_encoder(
  File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 289, in jsonable_encoder
    encoded_value = jsonable_encoder(
  File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 318, in jsonable_encoder
    return ENCODERS_BY_TYPE[type(obj)](obj)
  File "C:UserssanjaAppDataLocalProgramsPythonPython310libsite-packagesfastapiencoders.py", line 59, in <lambda>
    bytes: lambda o: o.decode(),
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9f in position 144: invalid start byte

Code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@app.post("/submit_response")
async def submit_response(session_id: str = Form(...), audio: UploadFile = File(...)):
session = get_session(session_id)
audio_path = f"{session_id}_response.wav"
# Save the uploaded audio file
with open(audio_path, "wb") as f:
shutil.copyfileobj(audio.file, f)
# Process the audio file here
response_text = transcribe_audio(audio_path) # Replace with actual transcription function
# Update the current scenario conversation with the candidate's response
session.current_scenario_conversation[-1] = (
session.current_scenario_conversation[-1][0],
session.current_scenario_conversation[-1][1],
response_text
)
save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1])
# Retrieve the current trait and perform satisfaction check
trait = TRAITS[session.current_trait_index]
status, feedback = satisfaction_check(
session.agents["satisfaction_check"],
session.current_scenario_conversation[-1][1],
response_text,
trait['trait_name']
)
if status == "satisfied":
# If satisfied, score the scenario and move to the next trait/scenario
score = score_scenario(session.agents["scoring"], session.current_scenario_conversation, trait)
save_conversation_to_file(session.interview_filename, ("Score", score))
move_to_next_scenario(session)
return {
"message": "Moving to next scenario",
"question": session.current_scenario_conversation[-1][1],
"score": score # Return the score
}
elif status == "insufficient":
# If the response is insufficient, generate a follow-up question
if len(session.current_scenario_conversation) >= 2:
move_to_next_scenario(session)
return {
"message": "Moving to next scenario due to insufficient response",
"question": session.current_scenario_conversation[-1][1]
}
else:
follow_up_question = generate_follow_up(
session.agents["follow_up"],
session.candidate_name,
session.current_scenario_conversation,
len(session.current_scenario_conversation),
insufficient=True
)
session.current_scenario_conversation.append(("Follow-Up", len(session.current_scenario_conversation), follow_up_question, ""))
save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1])
return {
"message": "Follow-up question for insufficient response",
"question": follow_up_question
}
else: # unsatisfied
# Generate a follow-up question for unsatisfactory response
follow_up_question = generate_follow_up(
session.agents["follow_up"],
session.candidate_name,
session.current_scenario_conversation,
len(session.current_scenario_conversation),
insufficient=False
)
session.current_scenario_conversation.append(("Follow-Up", len(session.current_scenario_conversation), follow_up_question, ""))
save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1])
if len(session.current_scenario_conversation) >= 3:
move_to_next_scenario(session)
return {
"message": "Moving to next scenario after follow-up",
"question": session.current_scenario_conversation[-1][1]
}
return {
"message": "Follow-up question for unsatisfactory response",
"question": follow_up_question
}
def transcribe_audio(audio_path: str) -> str:
"""
Transcribe audio file to text using speech_recognition.
"""
recognizer = sr.Recognizer()
try:
with sr.AudioFile(audio_path) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data)
return text
except sr.UnknownValueError:
return "Audio unintelligible"
except sr.RequestError as e:
return f"Could not request results; {e}"
</code>
<code>@app.post("/submit_response") async def submit_response(session_id: str = Form(...), audio: UploadFile = File(...)): session = get_session(session_id) audio_path = f"{session_id}_response.wav" # Save the uploaded audio file with open(audio_path, "wb") as f: shutil.copyfileobj(audio.file, f) # Process the audio file here response_text = transcribe_audio(audio_path) # Replace with actual transcription function # Update the current scenario conversation with the candidate's response session.current_scenario_conversation[-1] = ( session.current_scenario_conversation[-1][0], session.current_scenario_conversation[-1][1], response_text ) save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1]) # Retrieve the current trait and perform satisfaction check trait = TRAITS[session.current_trait_index] status, feedback = satisfaction_check( session.agents["satisfaction_check"], session.current_scenario_conversation[-1][1], response_text, trait['trait_name'] ) if status == "satisfied": # If satisfied, score the scenario and move to the next trait/scenario score = score_scenario(session.agents["scoring"], session.current_scenario_conversation, trait) save_conversation_to_file(session.interview_filename, ("Score", score)) move_to_next_scenario(session) return { "message": "Moving to next scenario", "question": session.current_scenario_conversation[-1][1], "score": score # Return the score } elif status == "insufficient": # If the response is insufficient, generate a follow-up question if len(session.current_scenario_conversation) >= 2: move_to_next_scenario(session) return { "message": "Moving to next scenario due to insufficient response", "question": session.current_scenario_conversation[-1][1] } else: follow_up_question = generate_follow_up( session.agents["follow_up"], session.candidate_name, session.current_scenario_conversation, len(session.current_scenario_conversation), insufficient=True ) session.current_scenario_conversation.append(("Follow-Up", len(session.current_scenario_conversation), follow_up_question, "")) save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1]) return { "message": "Follow-up question for insufficient response", "question": follow_up_question } else: # unsatisfied # Generate a follow-up question for unsatisfactory response follow_up_question = generate_follow_up( session.agents["follow_up"], session.candidate_name, session.current_scenario_conversation, len(session.current_scenario_conversation), insufficient=False ) session.current_scenario_conversation.append(("Follow-Up", len(session.current_scenario_conversation), follow_up_question, "")) save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1]) if len(session.current_scenario_conversation) >= 3: move_to_next_scenario(session) return { "message": "Moving to next scenario after follow-up", "question": session.current_scenario_conversation[-1][1] } return { "message": "Follow-up question for unsatisfactory response", "question": follow_up_question } def transcribe_audio(audio_path: str) -> str: """ Transcribe audio file to text using speech_recognition. """ recognizer = sr.Recognizer() try: with sr.AudioFile(audio_path) as source: audio_data = recognizer.record(source) text = recognizer.recognize_google(audio_data) return text except sr.UnknownValueError: return "Audio unintelligible" except sr.RequestError as e: return f"Could not request results; {e}" </code>
@app.post("/submit_response")
async def submit_response(session_id: str = Form(...), audio: UploadFile = File(...)):
    session = get_session(session_id)
    audio_path = f"{session_id}_response.wav"
    
    # Save the uploaded audio file
    with open(audio_path, "wb") as f:
        shutil.copyfileobj(audio.file, f)
    
    # Process the audio file here
    response_text = transcribe_audio(audio_path)  # Replace with actual transcription function

    # Update the current scenario conversation with the candidate's response
    session.current_scenario_conversation[-1] = (
        session.current_scenario_conversation[-1][0],
        session.current_scenario_conversation[-1][1],
        response_text
    )
    save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1])

    # Retrieve the current trait and perform satisfaction check
    trait = TRAITS[session.current_trait_index]
    status, feedback = satisfaction_check(
        session.agents["satisfaction_check"],
        session.current_scenario_conversation[-1][1],
        response_text,
        trait['trait_name']
    )

    if status == "satisfied":
        # If satisfied, score the scenario and move to the next trait/scenario
        score = score_scenario(session.agents["scoring"], session.current_scenario_conversation, trait)
        save_conversation_to_file(session.interview_filename, ("Score", score))
        move_to_next_scenario(session)
        return {
            "message": "Moving to next scenario",
            "question": session.current_scenario_conversation[-1][1],
            "score": score  # Return the score
        }
    elif status == "insufficient":
        # If the response is insufficient, generate a follow-up question
        if len(session.current_scenario_conversation) >= 2:
            move_to_next_scenario(session)
            return {
                "message": "Moving to next scenario due to insufficient response",
                "question": session.current_scenario_conversation[-1][1]
            }
        else:
            follow_up_question = generate_follow_up(
                session.agents["follow_up"],
                session.candidate_name,
                session.current_scenario_conversation,
                len(session.current_scenario_conversation),
                insufficient=True
            )
            session.current_scenario_conversation.append(("Follow-Up", len(session.current_scenario_conversation), follow_up_question, ""))
            save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1])
            return {
                "message": "Follow-up question for insufficient response",
                "question": follow_up_question
            }
    else:  # unsatisfied
        # Generate a follow-up question for unsatisfactory response
        follow_up_question = generate_follow_up(
            session.agents["follow_up"],
            session.candidate_name,
            session.current_scenario_conversation,
            len(session.current_scenario_conversation),
            insufficient=False
        )
        session.current_scenario_conversation.append(("Follow-Up", len(session.current_scenario_conversation), follow_up_question, ""))
        save_conversation_to_file(session.interview_filename, session.current_scenario_conversation[-1])

        if len(session.current_scenario_conversation) >= 3:
            move_to_next_scenario(session)
            return {
                "message": "Moving to next scenario after follow-up",
                "question": session.current_scenario_conversation[-1][1]
            }

        return {
            "message": "Follow-up question for unsatisfactory response",
            "question": follow_up_question
        }

def transcribe_audio(audio_path: str) -> str:
    """
    Transcribe audio file to text using speech_recognition.
    """
    recognizer = sr.Recognizer()
    try:
        with sr.AudioFile(audio_path) as source:
            audio_data = recognizer.record(source)
            text = recognizer.recognize_google(audio_data)
            return text
    except sr.UnknownValueError:
        return "Audio unintelligible"
    except sr.RequestError as e:
        return f"Could not request results; {e}"

Context:

  • I am using FastAPI to create an API that handles audio file uploads for speech-to-text processing.
  • The error occurs when the application attempts to handle or encode data returned from processing these audio files.
  • The audio files are being uploaded and processed in binary format.

Questions:

  1. What could be causing the UnicodeDecodeError in this scenario?
  2. How can I resolve this issue when dealing with binary data in FastAPI?
  3. Are there best practices for handling audio files and their encoding when integrating speech-to-text functionality in a FastAPI application?

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật