When a key contains spaces, it triggers an exception with the message h11._util.LocalProtocolError: Illegal header value. When this occurs, the OpenAI library rethrows this as an openai.APIConnectionError: Connection error., which can misleadingly suggest that the issue lies with network connectivity rather than with the key's formatting.
This has the potential to lead users down the wrong path when troubleshooting, as the real issue is related to the presence of spaces in the key. A beneficial enhancement would be to strip leading and trailing spaces from keys before they are used. Implementing a simple trim operation on keys can prevent this kind of exception from happening and improve user experience by providing more accurate error feedback.
Suggested change:
Implement a preprocessing step for keys that strips leading and trailing whitespace before they are used in any API requests. This can be done either within the OpenAI library where keys are handled or as a recommended practice in the documentation.
The goal of this change is to prevent Illegal header value exceptions related to key formatting and to ensure that errors thrown reflect the actual problem more accurately.
Issue description:
When a key contains spaces, it triggers an exception with the message
h11._util.LocalProtocolError: Illegal header value
. When this occurs, the OpenAI library rethrows this as anopenai.APIConnectionError: Connection error.
, which can misleadingly suggest that the issue lies with network connectivity rather than with the key's formatting.This has the potential to lead users down the wrong path when troubleshooting, as the real issue is related to the presence of spaces in the key. A beneficial enhancement would be to strip leading and trailing spaces from keys before they are used. Implementing a simple trim operation on keys can prevent this kind of exception from happening and improve user experience by providing more accurate error feedback.
Suggested change:
The goal of this change is to prevent
Illegal header value
exceptions related to key formatting and to ensure that errors thrown reflect the actual problem more accurately.