Skip to content

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

Python

Add configurable LLM timeout and retry settings for local model support

Murtaza Nasira871a4968ae1
diff --git a/config/env.asr.example b/config/env.asr.example
index 2a437626..158db342 100644
--- a/config/env.asr.example
+++ b/config/env.asr.example
@@ -60,6 +60,16 @@ GPT5_VERBOSITY=medium
# CHAT_GPT5_REASONING_EFFORT=medium
# CHAT_GPT5_VERBOSITY=medium
+# --- LLM Timeout & Retry ---
+# Read timeout for LLM requests in seconds (default 600). Increase for local
+# models (Ollama, vLLM) that need more time for long transcripts.
+# Connect/write timeouts stay at 30s so bad URLs fail fast.
+# LLM_REQUEST_TIMEOUT=1800
+#
+# Max retries on timeout (default 2). Set to 0 for local inference to avoid
+# queuing duplicate requests when your model is still processing the first one.
+# LLM_MAX_RETRIES=0
+
# --- LLM Streaming Compatibility ---
# Some LLM servers (e.g., certain vLLM configurations) don't support OpenAI's
# stream_options parameter. If chat streaming hangs or fails, try disabling this.
diff --git a/config/env.whisper.example b/config/env.whisper.example
index 79a51f05..4971fdf5 100644
--- a/config/env.whisper.example
+++ b/config/env.whisper.example
@@ -62,6 +62,16 @@ GPT5_VERBOSITY=medium
# CHAT_GPT5_REASONING_EFFORT=medium
# CHAT_GPT5_VERBOSITY=medium
+# --- LLM Timeout & Retry ---
+# Read timeout for LLM requests in seconds (default 600). Increase for local
+# models (Ollama, vLLM) that need more time for long transcripts.
+# Connect/write timeouts stay at 30s so bad URLs fail fast.
+# LLM_REQUEST_TIMEOUT=1800
+#
+# Max retries on timeout (default 2). Set to 0 for local inference to avoid
+# queuing duplicate requests when your model is still processing the first one.
+# LLM_MAX_RETRIES=0
+
# --- LLM Streaming Compatibility ---
# Some LLM servers (e.g., certain vLLM configurations) don't support OpenAI's
# stream_options parameter. If chat streaming hangs or fails, try disabling this.
diff --git a/src/config/app_config.py b/src/config/app_config.py
index af3a1d70..a85afc70 100644
--- a/src/config/app_config.py
+++ b/src/config/app_config.py
@@ -108,7 +108,8 @@ def initialize_config(app):
client = None
try:
api_key = TEXT_MODEL_API_KEY or "not-needed"
- client = OpenAI(api_key=api_key, base_url=TEXT_MODEL_BASE_URL, http_client=http_client_no_proxy)
+ from src.services.llm import llm_timeout, LLM_MAX_RETRIES
+ client = OpenAI(api_key=api_key, base_url=TEXT_MODEL_BASE_URL, http_client=http_client_no_proxy, timeout=llm_timeout, max_retries=LLM_MAX_RETRIES)
app.logger.info(f"LLM client initialized: {TEXT_MODEL_BASE_URL} / {TEXT_MODEL_NAME}")
except Exception as e:
app.logger.error(f"Failed to initialize LLM client: {e}")
diff --git a/src/services/llm.py b/src/services/llm.py
index c8e9f6bd..9da66ffb 100644
--- a/src/services/llm.py
+++ b/src/services/llm.py
@@ -43,6 +43,13 @@ CHAT_GPT5_VERBOSITY = os.environ.get("CHAT_GPT5_VERBOSITY")
# Streaming options - disable for LLM servers that don't support OpenAI's stream_options
ENABLE_STREAM_OPTIONS = os.environ.get("ENABLE_STREAM_OPTIONS", "true").lower() == "true"
+# LLM timeout and retry configuration
+# Read timeout controls how long to wait for model inference (the bottleneck for local models)
+# Connect/write stay short so misconfigured URLs fail fast instead of hanging
+LLM_REQUEST_TIMEOUT = int(os.environ.get("LLM_REQUEST_TIMEOUT", "600"))
+LLM_MAX_RETRIES = int(os.environ.get("LLM_MAX_RETRIES", "2"))
+llm_timeout = httpx.Timeout(connect=30.0, read=float(LLM_REQUEST_TIMEOUT), write=30.0, pool=30.0)
+
def get_chat_config():
"""
@@ -85,8 +92,12 @@ try:
client = OpenAI(
api_key=api_key,
base_url=TEXT_MODEL_BASE_URL,
- http_client=http_client_no_proxy
+ http_client=http_client_no_proxy,
+ timeout=llm_timeout,
+ max_retries=LLM_MAX_RETRIES,
)
+ if LLM_REQUEST_TIMEOUT != 600 or LLM_MAX_RETRIES != 2:
+ logger.info(f"LLM client timeout: {LLM_REQUEST_TIMEOUT}s, max retries: {LLM_MAX_RETRIES}")
except Exception as client_init_e:
client = None
@@ -100,7 +111,9 @@ try:
chat_client = OpenAI(
api_key=chat_config['api_key'],
base_url=chat_config['base_url'],
- http_client=http_client_no_proxy
+ http_client=http_client_no_proxy,
+ timeout=llm_timeout,
+ max_retries=LLM_MAX_RETRIES,
)
logger.info(f"Separate chat client initialized: {chat_config['base_url']} / {chat_config['model_name']}")
else: