openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

Lack of API key will cause `openai.OpenAIError` when using "LM Studio" REST API

Open meltingscales opened this issue 2 years ago • 7 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [X] This is an issue with the Python library

Describe the bug

Summary: When running "LM Studio" for local AI models, I cannot use the openai package to interact with the REST API, because it does not require an API key.

Solution:

I was able to fix this by commenting out the below lines in ./openai/_client.py>OpenAI>__init__#L92:

        if api_key is None:
            raise OpenAIError(
                "The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable"
            )

To Reproduce

Reproduce:

  1. Use "LM Studio" to start a REST API locally
  2. Run the below code:
import selenium
import os
import openai
from openai import OpenAI

client = OpenAI(
    base_url='http://localhost:1234/v1'
)

# start LM Studio, rest API?

# TODO: The 'openai.api_base' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_base="http://localhost:1234/v1")'
# openai.api_base = "http://localhost:1234/v1" # point to the local server
 # no need for an API key

completion = client.chat.completions.create(model="local-model", # this field is currently unused
messages=[
  {"role": "system", "content": "Always answer in rhymes."},
  {"role": "user", "content": "Introduce yourself."}
])

print(completion.choices[0].message)
  1. Receive this error:
 src  python .\test.py
Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_exceptions.py", line 10, in map_exceptions
    yield
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 142, in _send_request_headers
    event = h11.Request(
            ^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_events.py", line 96, in __init__
    self, "headers", normalize_and_validate(headers, _parsed=_parsed)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_headers.py", line 164, in normalize_and_validate
    validate(_field_value_re, value, "Illegal header value {!r}", value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_util.py", line 91, in validate
    raise LocalProtocolError(msg)
h11._util.LocalProtocolError: Illegal header value b'Bearer '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 66, in map_httpcore_exceptions
    yield
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 228, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\connection_pool.py", line 268, in handle_request
    raise exc
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\connection.py", line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 133, in handle_request
    raise exc
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 92, in handle_request
    self._send_request_headers(**kwargs)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 141, in _send_request_headers
    with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.LocalProtocolError: Illegal header value b'Bearer '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\openai\_base_client.py", line 872, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 901, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 929, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 966, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 1002, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 227, in handle_request
    with map_httpcore_exceptions():
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 83, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.LocalProtocolError: Illegal header value b'Bearer '

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_exceptions.py", line 10, in map_exceptions
    yield
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 142, in _send_request_headers
    event = h11.Request(
            ^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_events.py", line 96, in __init__
    self, "headers", normalize_and_validate(headers, _parsed=_parsed)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_headers.py", line 164, in normalize_and_validate
    validate(_field_value_re, value, "Illegal header value {!r}", value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_util.py", line 91, in validate
    raise LocalProtocolError(msg)
h11._util.LocalProtocolError: Illegal header value b'Bearer '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 66, in map_httpcore_exceptions
    yield
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 228, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\connection_pool.py", line 268, in handle_request
    raise exc
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\connection.py", line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 133, in handle_request
    raise exc
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 92, in handle_request
    self._send_request_headers(**kwargs)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 141, in _send_request_headers
    with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.LocalProtocolError: Illegal header value b'Bearer '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\openai\_base_client.py", line 872, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 901, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 929, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 966, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_client.py", line 1002, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 227, in handle_request
    with map_httpcore_exceptions():
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\contextlib.py", line 155, in __exit__
    self.gen.throw(value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpx\_transports\default.py", line 83, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.LocalProtocolError: Illegal header value b'Bearer '

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_exceptions.py", line 10, in map_exceptions
    yield
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\httpcore\_sync\http11.py", line 142, in _send_request_headers
    event = h11.Request(
            ^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_events.py", line 96, in __init__
    self, "headers", normalize_and_validate(headers, _parsed=_parsed)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_headers.py", line 164, in normalize_and_validate
    validate(_field_value_re, value, "Illegal header value {!r}", value)
  File "C:\Users\henry\.conda\envs\america-elections-2024\Lib\site-packages\h11\_util.py", line 91, in validate
    raise LocalProtocolError(msg)
h11._util.LocalProtocolError: Illegal header value b'Bearer '

Code snippets

No response

OS

Windows 10.0.22621 Build 22621

Python version

Python 3.12.0 | packaged by Anaconda, Inc. | (main, Oct 2 2023, 17:20:38) [MSC v.1916 64 bit (AMD64)] on win32

Library version

openai v1.3.8

meltingscales avatar Dec 10 '23 20:12 meltingscales

This is intended behavior. To send no API key, use an empty string ("").

rattrayalex avatar Dec 11 '23 01:12 rattrayalex

This is intended behavior. To send no API key, use an empty string ("").

if you set it empty string you getting illegal header as above, I searched a lot, it turned that my server just accept any api key, as Beaver header, if I not setup authorisation. @meltingscales - may be your server too.

zba avatar Jan 16 '24 21:01 zba

@rattrayalex Please reopen issue, and see what happens if you use an empty string: https://github.com/encode/httpx/issues/1640#issuecomment-842036339

LocalProtocolError: Illegal header value b'Bearer '

Need check empty case in this: https://github.com/openai/openai-python/blob/da48e4cac78d1d4ac749e2aa5cfd619fde1e6c68/src/openai/_client.py#L160

rusdevops avatar Aug 03 '24 11:08 rusdevops

I created PR: https://github.com/openai/openai-python/pull/1594/files

rusdevops avatar Aug 03 '24 12:08 rusdevops

Ah, thank you – you're correct that we should not send the Authorization header when the api key is empty. We'll try to fix that soon.

rattrayalex avatar Aug 11 '24 22:08 rattrayalex

Getting the same error with Python 3.12, crewai and imported openai when I try to connect to LM Studio 0.3.5> Code: import os import openai from crewai import Agent, Task, Crew, Process from crewai_tools import SerperDevTool from langchain_community.tools import DuckDuckGoSearchRun from dotenv import load_dotenv, find_dotenv

Load environment variables

load_dotenv(find_dotenv()) openai.api_key = ""
openai.api_base = "http://127.0.0.1:1234/v1/models/"

Error: utils.py", line 310, in exception_type raise AuthenticationError( litellm.exceptions.AuthenticationError: litellm.AuthenticationError: AuthenticationError: OpenAIException - The api_key client option must be set either by passing api_key to the client or by setting the OPENAI_API_KEY environment variable

sunsam31 avatar Dec 05 '24 02:12 sunsam31

Ah, thank you – you're correct that we should not send the Authorization header when the api key is empty. We'll try to fix that soon.

When will this be fixed? I am getting connection errors when using lm_studio provider because api key is blank, which results in:

httpx.LocalProtocolError: Illegal header value b'Bearer '

Using requests works fine:

import requests

def main():
    url = "http://localhost:1337/v1/chat/completions"
    headers = {
        "Content-Type": "application/json"
    }
    payload = {
        "model": "deepseek-r1-distill-qwen-7b",
        "messages": [
            { "role": "system", "content": "Always answer in rhymes. Today is Thursday" },
            { "role": "user", "content": "What day is it today?" }
        ],
        "temperature": 0.7,
        "max_tokens": -1,
        "stream": False
    }
    
    response = requests.post(url, headers=headers, json=payload)
    if response.ok:
        print("Response:", response.json())
    else:
        print("Error:", response.status_code, response.text)

if __name__ == '__main__':
    main()

However, using LiteLLM will result in error. Simple example:

from litellm import completion
import os

os.environ['LM_STUDIO_API_BASE'] = "http://localhost:1337/v1"
os.environ['LM_STUDIO_API_KEY'] = ""

response = completion(
    model="lm_studio/deepseek-r1-distill-qwen-7b",
    messages=[
        {
            "role": "user",
            "content": "What's the weather like in Boston today in Fahrenheit?",
        }
    ]
)
print(response)

Error stack:

PS C:\Scripts> & c:/Scripts/Scripts/Scripts/python.exe c:/Scripts/test_litellm.py

Give Feedback / Get Help: https://github.com/BerriAI/litellm/issues/new
LiteLLM.Info: If you need to debug this error, use `litellm._turn_on_debug()'.


Provider List: https://docs.litellm.ai/docs/providers

Traceback (most recent call last):
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_transports\default.py", line 101, in map_httpcore_exceptions
    yield
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_transports\default.py", line 250, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_sync\connection_pool.py", line 256, in handle_request
    raise exc from None
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_sync\connection_pool.py", line 236, in handle_request
    response = connection.handle_request(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_sync\connection.py", line 103, in handle_request
    return self._connection.handle_request(request)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_sync\http11.py", line 136, in handle_request
    raise exc
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_sync\http11.py", line 86, in handle_request
    self._send_request_headers(**kwargs)
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_sync\http11.py", line 144, in _send_request_headers
    with map_exceptions({h11.LocalProtocolError: LocalProtocolError}):
  File "C:\Users\jorda\.pyenv\pyenv-win\versions\3.11.9\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\Scripts\Scripts\Lib\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.LocalProtocolError: Illegal header value b'Bearer  '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 996, in _request
    response = self._client.send(
               ^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_client.py", line 914, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_client.py", line 942, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_client.py", line 979, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_client.py", line 1014, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_transports\default.py", line 249, in handle_request
    with map_httpcore_exceptions():
  File "C:\Users\jorda\.pyenv\pyenv-win\versions\3.11.9\Lib\contextlib.py", line 158, in __exit__
    self.gen.throw(typ, value, traceback)
  File "C:\Scripts\Scripts\Lib\site-packages\httpx\_transports\default.py", line 118, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.LocalProtocolError: Illegal header value b'Bearer  '

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\llms\openai\openai.py", line 726, in completion
    raise e
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\llms\openai\openai.py", line 653, in completion
    self.make_sync_openai_chat_completion_request(
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\litellm_core_utils\logging_utils.py", line 145, in sync_wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\llms\openai\openai.py", line 472, in make_sync_openai_chat_completion_request
    raise e
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\llms\openai\openai.py", line 454, in make_sync_openai_chat_completion_request
    raw_response = openai_client.chat.completions.with_raw_response.create(
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_legacy_response.py", line 364, in wrapped
    return cast(LegacyAPIResponse[R], func(*args, **kwargs))
                                      ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_utils\_utils.py", line 279, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\resources\chat\completions.py", line 863, in create
    return self._post(
           ^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 1283, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 960, in request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 1020, in _request
    return self._retry_request(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 1098, in _retry_request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 1020, in _request
    return self._retry_request(
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 1098, in _retry_request
    return self._request(
           ^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\openai\_base_client.py", line 1030, in _request
    raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\main.py", line 1724, in completion
    raise e
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\main.py", line 1697, in completion
    response = openai_chat_completions.completion(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\llms\openai\openai.py", line 736, in completion
    raise OpenAIError(
litellm.llms.openai.common_utils.OpenAIError: Connection error.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Scripts\test_litellm.py", line 8, in <module>
    response = completion(
               ^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\utils.py", line 1190, in wrapper
    raise e
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\utils.py", line 1068, in wrapper
    result = original_function(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\main.py", line 3085, in completion
    raise exception_type(
          ^^^^^^^^^^^^^^^
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py", line 2202, in exception_type
    raise e
  File "C:\Scripts\Scripts\Lib\site-packages\litellm\litellm_core_utils\exception_mapping_utils.py", line 452, in exception_type
    raise APIError(
litellm.exceptions.APIError: litellm.APIError: APIError: Lm_studioException - Connection error.
PS C:\Scripts> 

Update: Using a fake API key gets past this, but in LM Studio, at least my copy, there is no way to set an API key in settings and this is simply not applicable it seems?

WASasquatch avatar Feb 25 '25 17:02 WASasquatch

@WASasquatch @meltingscales This PR can solve this problem:https://github.com/openai/openai-python/pull/2266

wayyoungboy avatar Mar 31 '25 06:03 wayyoungboy

Also following, same issue with lmstudio

arty-hlr avatar Mar 31 '25 10:03 arty-hlr

If api_key="lm-studio" (any non-empty string) then there is no error and I get the correct response. Check out the example on this page: https://lmstudio.ai/docs/app/api/endpoints/openai#endpoints-overview

TrigonaMinima avatar Apr 19 '25 08:04 TrigonaMinima

If api_key="lm-studio" (any non-empty string) then there is no error and I get the correct response. Check out the example on this page: https://lmstudio.ai/docs/app/api/endpoints/openai#endpoints-overview

Yeah I think they are just quoting the fixes we have found, as that's a recent addition, and naturally they don't control OpenAI repo. This is fix I shared on discord and stuff for a fake key.

WASasquatch avatar Apr 19 '25 15:04 WASasquatch

This will be fixed in the next release. If you pass an empty API key we will no longer try to send it in the headers.

https://github.com/openai/openai-python/pull/2432

RobertCraigie avatar Jun 27 '25 16:06 RobertCraigie