Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Fix app context error in chat streaming generator

Murtaza Nasirc97545ec37fd
diff --git a/src/api/recordings.py b/src/api/recordings.py
index 02d3a656..0bddccd7 100644
--- a/src/api/recordings.py
+++ b/src/api/recordings.py
@@ -2832,6 +2832,10 @@ Additional context and notes about the meeting:
app = current_app._get_current_object()
def generate():
+ # Push app context for entire generator execution
+ # This is needed because call_chat_completion checks budget which requires db access
+ ctx = app.app_context()
+ ctx.push()
try:
# Enable streaming with user_id for budget enforcement
stream = call_chat_completion(
@@ -2854,6 +2858,8 @@ Additional context and notes about the meeting:
app.logger.error(f"Error during chat stream generation: {str(e)}")
# Yield an error message in SSE format
yield f"data: {json.dumps({'error': str(e)})}\n\n"
+ finally:
+ ctx.pop()
return Response(generate(), mimetype='text/event-stream')