From b84785b5b7516268272167a0284c3e755f1ae248 Mon Sep 17 00:00:00 2001 From: Tim Elfrink Date: Fri, 12 Sep 2025 22:41:30 +0200 Subject: [PATCH] fix(lm_studio): resolve illegal Bearer header value issue - Change default API key from space ' ' to 'fake-api-key' - Fixes httpcore.LocalProtocolError: Illegal header value b'Bearer ' - Maintains compatibility with explicit API keys and environment variables - Add comprehensive tests for provider info retrieval Fixes #14502 --- litellm/llms/lm_studio/chat/transformation.py | 4 +-- .../test_lm_studio_chat_transformation.py | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/litellm/llms/lm_studio/chat/transformation.py b/litellm/llms/lm_studio/chat/transformation.py index f7a2cc0f28..7b188ff33f 100644 --- a/litellm/llms/lm_studio/chat/transformation.py +++ b/litellm/llms/lm_studio/chat/transformation.py @@ -15,8 +15,8 @@ class LMStudioChatConfig(OpenAIGPTConfig): ) -> Tuple[Optional[str], Optional[str]]: api_base = api_base or get_secret_str("LM_STUDIO_API_BASE") # type: ignore dynamic_api_key = ( - api_key or get_secret_str("LM_STUDIO_API_KEY") or " " - ) # vllm does not require an api key + api_key or get_secret_str("LM_STUDIO_API_KEY") or "fake-api-key" + ) # LM Studio does not require an api key, but OpenAI client requires non-None value return api_base, dynamic_api_key def map_openai_params( diff --git a/tests/test_litellm/llms/lm_studio/test_lm_studio_chat_transformation.py b/tests/test_litellm/llms/lm_studio/test_lm_studio_chat_transformation.py index 1f09b1e540..964c85da3d 100644 --- a/tests/test_litellm/llms/lm_studio/test_lm_studio_chat_transformation.py +++ b/tests/test_litellm/llms/lm_studio/test_lm_studio_chat_transformation.py @@ -1,5 +1,6 @@ import os import sys +from unittest.mock import patch from pydantic import BaseModel @@ -53,3 +54,32 @@ class TestLMStudioChatConfigResponseFormat: assert mapped_schema["properties"] == schema["properties"] opt_schema = optional_params["response_format"]["json_schema"]["schema"] assert opt_schema["properties"] == schema["properties"] + + +def test_lm_studio_get_openai_compatible_provider_info(): + """Test provider info retrieval""" + config = LMStudioChatConfig() + + # Test default behavior (no API key provided) + _, api_key = config._get_openai_compatible_provider_info(None, None) + assert api_key == "fake-api-key" + + # Test explicit API key + _, api_key = config._get_openai_compatible_provider_info(None, "test-key") + assert api_key == "test-key" + + +def test_lm_studio_get_openai_compatible_provider_info_with_env(): + """Test provider info retrieval with environment variables.""" + config = LMStudioChatConfig() + + with patch.dict( + "os.environ", + { + "LM_STUDIO_API_BASE": "http://localhost:1234/v1", + "LM_STUDIO_API_KEY": "env_api_key", + }, + ): + api_base, api_key = config._get_openai_compatible_provider_info(None, None) + assert api_base == "http://localhost:1234/v1" + assert api_key == "env_api_key"