Skip to content

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

@meltingscales

Description

@meltingscales

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

  • 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

Activity

rattrayalex

rattrayalex commented on Dec 11, 2023

@rattrayalex
Collaborator

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

zba

zba commented on Jan 16, 2024

@zba

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.

rusdevops

rusdevops commented on Aug 3, 2024

@rusdevops

@rattrayalex Please reopen issue, and see what happens if you use an empty string:
encode/httpx#1640 (comment)

LocalProtocolError: Illegal header value b'Bearer '

Need check empty case in this:

return {"Authorization": f"Bearer {api_key}"}

rattrayalex

rattrayalex commented on Aug 11, 2024

@rattrayalex
Collaborator

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.

sunsam31

sunsam31 commented on Dec 5, 2024

@sunsam31

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

WASasquatch

WASasquatch commented on Feb 25, 2025

@WASasquatch

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?

wayyoungboy

wayyoungboy commented on Mar 31, 2025

@wayyoungboy

@WASasquatch @meltingscales This PR can solve this problem:#2266

arty-hlr

arty-hlr commented on Mar 31, 2025

@arty-hlr

Also following, same issue with lmstudio

TrigonaMinima

TrigonaMinima commented on Apr 19, 2025

@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

WASasquatch

WASasquatch commented on Apr 19, 2025

@WASasquatch

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.

RobertCraigie

RobertCraigie commented on Jun 27, 2025

@RobertCraigie
Collaborator

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.

#2432

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @rattrayalex@zba@WASasquatch@TrigonaMinima@meltingscales

      Issue actions

        Lack of API key will cause `openai.OpenAIError` when using "LM Studio" REST API · Issue #961 · openai/openai-python