Skip to content

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

Python

feat: add GPT5_FORCE_MODE / CHAT_GPT5_FORCE_MODE to force GPT-5 parameter handling on Azure AI Foundry and other gateways (#367)

Murtaza Nasirfc30ec6a210c
diff --git a/config/env.asr.example b/config/env.asr.example
index ee8f7869..45c39bd6 100644
--- a/config/env.asr.example
+++ b/config/env.asr.example
@@ -23,6 +23,14 @@ TEXT_MODEL_NAME=openai/gpt-4o-mini
# If using GPT-5 models (gpt-5, gpt-5-mini, gpt-5-nano, gpt-5-chat-latest) with OpenAI API,
# these parameters will be used instead of temperature.
#
+# By default GPT-5 parameter handling (reasoning_effort/verbosity,
+# max_completion_tokens) activates only for GPT-5 model names on the official
+# OpenAI API. Set this to force it on for GPT-5 models served through Azure AI
+# Foundry or other gateways the URL check cannot detect:
+# GPT5_FORCE_MODE=true
+# Same, for a separately configured chat model (not inherited from the above):
+# CHAT_GPT5_FORCE_MODE=true
+#
# Example GPT-5 configuration:
# TEXT_MODEL_BASE_URL=https://api.openai.com/v1
# TEXT_MODEL_NAME=gpt-5-mini
diff --git a/config/env.transcription.example b/config/env.transcription.example
index f9950b51..273e983c 100644
--- a/config/env.transcription.example
+++ b/config/env.transcription.example
@@ -47,6 +47,14 @@ TEXT_MODEL_NAME=openai/gpt-4o-mini
# PREFIX_CACHE_OPTIMIZED_PROMPTS=false
# --- GPT-5 Specific Settings (only used with OpenAI API and GPT-5 models) ---
+# By default GPT-5 parameter handling (reasoning_effort/verbosity,
+# max_completion_tokens) activates only for GPT-5 model names on the official
+# OpenAI API. Set this to force it on for GPT-5 models served through Azure AI
+# Foundry or other gateways the URL check cannot detect:
+# GPT5_FORCE_MODE=true
+# Same, for a separately configured chat model (not inherited from the above):
+# CHAT_GPT5_FORCE_MODE=true
+#
# Reasoning effort: minimal, low, medium, high (default: medium)
GPT5_REASONING_EFFORT=medium
# Verbosity: low, medium, high (default: medium)
diff --git a/config/env.whisper.example b/config/env.whisper.example
index 50e5bdf7..c976a1b6 100644
--- a/config/env.whisper.example
+++ b/config/env.whisper.example
@@ -25,6 +25,14 @@ TEXT_MODEL_NAME=openai/gpt-4o-mini
# If using GPT-5 models (gpt-5, gpt-5-mini, gpt-5-nano, gpt-5-chat-latest) with OpenAI API,
# these parameters will be used instead of temperature.
#
+# By default GPT-5 parameter handling (reasoning_effort/verbosity,
+# max_completion_tokens) activates only for GPT-5 model names on the official
+# OpenAI API. Set this to force it on for GPT-5 models served through Azure AI
+# Foundry or other gateways the URL check cannot detect:
+# GPT5_FORCE_MODE=true
+# Same, for a separately configured chat model (not inherited from the above):
+# CHAT_GPT5_FORCE_MODE=true
+#
# Example GPT-5 configuration:
# TEXT_MODEL_BASE_URL=https://api.openai.com/v1
# TEXT_MODEL_NAME=gpt-5-mini
diff --git a/config/env.whisperx.example b/config/env.whisperx.example
index 3fe2d4d2..f0513664 100644
--- a/config/env.whisperx.example
+++ b/config/env.whisperx.example
@@ -33,6 +33,14 @@ TEXT_MODEL_NAME=openai/gpt-4o-mini
# If using GPT-5 models (gpt-5, gpt-5-mini, gpt-5-nano, gpt-5-chat-latest) with OpenAI API,
# these parameters will be used instead of temperature.
#
+# By default GPT-5 parameter handling (reasoning_effort/verbosity,
+# max_completion_tokens) activates only for GPT-5 model names on the official
+# OpenAI API. Set this to force it on for GPT-5 models served through Azure AI
+# Foundry or other gateways the URL check cannot detect:
+# GPT5_FORCE_MODE=true
+# Same, for a separately configured chat model (not inherited from the above):
+# CHAT_GPT5_FORCE_MODE=true
+#
# Example GPT-5 configuration:
# TEXT_MODEL_BASE_URL=https://api.openai.com/v1
# TEXT_MODEL_NAME=gpt-5-mini
diff --git a/docs/admin-guide/model-configuration.md b/docs/admin-guide/model-configuration.md
index ae4266b2..4744f0a9 100644
--- a/docs/admin-guide/model-configuration.md
+++ b/docs/admin-guide/model-configuration.md
@@ -131,6 +131,19 @@ Speakr automatically detects when you're using:
1. A GPT-5 model (based on model name)
2. The official OpenAI API (based on base URL containing `api.openai.com`)
+### GPT-5 Through Azure AI Foundry and Other Gateways
+
+The automatic detection only activates on the official OpenAI API URL. If you serve a GPT-5 model through Azure AI Foundry or another gateway, requests fail with an error such as `Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead.` In that case, force GPT-5 parameter handling explicitly:
+
+```bash
+# Force GPT-5 parameters (reasoning_effort, verbosity, max_completion_tokens)
+# regardless of the base URL
+GPT5_FORCE_MODE=true
+
+# Same, for a separately configured chat model (not inherited from the above)
+# CHAT_GPT5_FORCE_MODE=true
+```
+
When both conditions are met, Speakr automatically:
- Removes `temperature` parameter from API calls
diff --git a/src/services/llm.py b/src/services/llm.py
index d665de81..31274e3b 100644
--- a/src/services/llm.py
+++ b/src/services/llm.py
@@ -102,6 +102,15 @@ llm_timeout = httpx.Timeout(
)
+def _gpt5_mode_forced(env_var="GPT5_FORCE_MODE"):
+ """Whether GPT-5 parameter handling is forced on via environment variable.
+
+ The autodetection requires api.openai.com in the base URL, which misses
+ GPT-5 models served through Azure AI Foundry or other gateways (#367).
+ """
+ return str(os.environ.get(env_var, "")).strip().lower() in ("true", "1", "yes")
+
+
def get_chat_config():
"""
Get chat model configuration, falling back to TEXT_MODEL if not set.
@@ -114,14 +123,18 @@ def get_chat_config():
'base_url': CHAT_MODEL_BASE_URL or TEXT_MODEL_BASE_URL,
'model_name': CHAT_MODEL_NAME,
'gpt5_reasoning_effort': CHAT_GPT5_REASONING_EFFORT or os.environ.get("GPT5_REASONING_EFFORT", "medium"),
- 'gpt5_verbosity': CHAT_GPT5_VERBOSITY or os.environ.get("GPT5_VERBOSITY", "medium")
+ 'gpt5_verbosity': CHAT_GPT5_VERBOSITY or os.environ.get("GPT5_VERBOSITY", "medium"),
+ # A separate chat model does not inherit GPT5_FORCE_MODE: it may
+ # be a different (non-GPT-5) model than the main text model.
+ 'gpt5_force': _gpt5_mode_forced("CHAT_GPT5_FORCE_MODE"),
}
return {
'api_key': TEXT_MODEL_API_KEY,
'base_url': TEXT_MODEL_BASE_URL,
'model_name': TEXT_MODEL_NAME,
'gpt5_reasoning_effort': os.environ.get("GPT5_REASONING_EFFORT", "medium"),
- 'gpt5_verbosity': os.environ.get("GPT5_VERBOSITY", "medium")
+ 'gpt5_verbosity': os.environ.get("GPT5_VERBOSITY", "medium"),
+ 'gpt5_force': _gpt5_mode_forced(),
}
@@ -247,8 +260,9 @@ def call_llm_completion(messages, temperature=0.7, response_format=None, stream=
logger.warning(f"Budget check failed for user {user_id}: {e}")
try:
- # Check if we're using GPT-5 with OpenAI API
- using_gpt5 = is_gpt5_model(TEXT_MODEL_NAME) and is_using_openai_api()
+ # Check if we're using GPT-5 with OpenAI API (or forced via env for
+ # Azure AI Foundry and other gateways the URL check can't detect)
+ using_gpt5 = _gpt5_mode_forced() or (is_gpt5_model(TEXT_MODEL_NAME) and is_using_openai_api())
completion_args = {
"model": TEXT_MODEL_NAME,
@@ -396,8 +410,9 @@ def call_chat_completion(messages, temperature=0.7, response_format=None, stream
model_name = chat_config['model_name']
base_url = chat_config['base_url'] or ''
- # Check if we're using GPT-5 with OpenAI API
- using_gpt5 = is_gpt5_model(model_name) and 'api.openai.com' in base_url
+ # Check if we're using GPT-5 with OpenAI API (or forced via env for
+ # Azure AI Foundry and other gateways the URL check can't detect)
+ using_gpt5 = chat_config['gpt5_force'] or (is_gpt5_model(model_name) and 'api.openai.com' in base_url)
completion_args = {
"model": model_name,