-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Closed
Description
Confirm this is an issue with the Python library and not an underlying OpenAI API
- This is an issue with the Python libraryTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
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:
- Use "LM Studio" to start a REST API locally
- 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)
- 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
andreasntr
Metadata
Metadata
Assignees
Labels
No labels
Activity
rattrayalex commentedon Dec 11, 2023
This is intended behavior. To send no API key, use an empty string (
""
).zba commentedon Jan 16, 2024
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 commentedon Aug 3, 2024
@rattrayalex Please reopen issue, and see what happens if you use an empty string:
encode/httpx#1640 (comment)
Need check empty case in this:
openai-python/src/openai/_client.py
Line 160 in da48e4c
rusdevops commentedon Aug 3, 2024
I created PR: https://github.com/openai/openai-python/pull/1594/files
rattrayalex commentedon Aug 11, 2024
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 commentedon Dec 5, 2024
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 commentedon Feb 25, 2025
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:
However, using LiteLLM will result in error. Simple example:
Error stack:
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?
fix: when api_key is None, the request should not contain 'Authorizat…
wayyoungboy commentedon Mar 31, 2025
@WASasquatch @meltingscales This PR can solve this problem:#2266
arty-hlr commentedon Mar 31, 2025
Also following, same issue with lmstudio
fix: when api_key is None, the request should not contain 'Authorizat…
TrigonaMinima commentedon Apr 19, 2025
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-overviewWASasquatch commentedon Apr 19, 2025
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 commentedon Jun 27, 2025
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