Description: Recent updates to LM Studio (0.4.0+) enforce a strict token authentication scheme when Server Authentication or Model Context Protocol (MCP) tools are active. This causes Orpheus-FastAPI to crash with an Error: API request failed with status code 401 (invalid_api_key) because the outbound requests.Session doesn't pass a bearer token.
Additionally, if the base URL points strictly to /v1, LM Studio warns of an Unexpected endpoint or method (POST /v1) and fails to generate tokens.
Here is a tested walkthrough to modify the inference engine to dynamically inject the bearer token and fix the endpoint routing path.
Fix:
Generate your API token in LM Studio
Open: your Orpheus project directory and edit the inference engine file at: tts_engine/inference.py
Locate the while retry_count < max_retries: loop block.
Replace the standard session.post execution block with the following logic to handle the token handshake and endpoint appending:
whileretry_count<max_retries:
try:
# 1. Clone the headers dictionary and force inject the LM Studio Token with Bearer schemeauth_headers=HEADERS.copy() ifisinstance(HEADERS, dict) else {}
auth_headers["Authorization"] ="Bearer <YOUR_LM_STUDIO_TOKEN_HERE>"# 2. Fix the broken base path by forcing the legacy completions endpointcorrected_url=API_URLifcorrected_url.endswith("/v1"):
corrected_url=f"{corrected_url}/completions"elifcorrected_url.endswith("/v1/"):
corrected_url=f"{corrected_url}completions"# 3. Submit the network request to the proper authenticated endpointresponse=session.post(
corrected_url, headers=auth_headers,
json=payload,
stream=True,
timeout=REQUEST_TIMEOUT
)
ifresponse.status_code!=200:
# ... [The rest of your original streaming loop logic continues here unchanged] ...Save inference.py and restart your Orpheus terminal application
Description: Recent updates to LM Studio (0.4.0+) enforce a strict token authentication scheme when Server Authentication or Model Context Protocol (MCP) tools are active. This causes Orpheus-FastAPI to crash with an Error: API request failed with status code 401 (invalid_api_key) because the outbound requests.Session doesn't pass a bearer token.
Additionally, if the base URL points strictly to /v1, LM Studio warns of an Unexpected endpoint or method (POST /v1) and fails to generate tokens.
Here is a tested walkthrough to modify the inference engine to dynamically inject the bearer token and fix the endpoint routing path.
Fix:
Generate your API token in LM Studio
Open: your Orpheus project directory and edit the inference engine file at: tts_engine/inference.py
Locate the while retry_count < max_retries: loop block.
Replace the standard session.post execution block with the following logic to handle the token handshake and endpoint appending:
Save inference.py and restart your Orpheus terminal application