Open
Description
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:
- 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
Metadata
Metadata
Assignees
Labels
No labels