feat: add configurable SDK log level (#3096)#3197
feat: add configurable SDK log level (#3096)#3197lrg913427-dot wants to merge 1 commit intoopenai:mainfrom
Conversation
Add support for configuring the OpenAI SDK log level via: 1. `openai.set_log_level()` function for programmatic control - Accepts string levels: "debug", "info", "warning", "error", "critical" - Accepts numeric logging levels (e.g. logging.WARNING) - Case-insensitive string matching 2. `OPENAI_LOG_LEVEL` environment variable (new, recommended) - Takes precedence over the legacy `OPENAI_LOG` variable - Supports all standard log level names 3. Extended `OPENAI_LOG` environment variable (backwards compatible) - Now supports "warning", "error", "critical" in addition to "debug" and "info" Closes openai#3096
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d0130f8ef1
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| ) | ||
|
|
||
|
|
||
| def _parse_log_level(value: str) -> int | None: |
There was a problem hiding this comment.
Use 3.9-compatible union syntax in
_parse_log_level
openai._utils._logs is imported during import openai, and this annotation uses int | None without from __future__ import annotations. Because the package declares Python 3.9 support (pyproject.toml has requires-python = ">= 3.9"), this expression will raise TypeError at import time on 3.9, preventing the SDK from loading for all 3.9 users. Please switch this annotation to a 3.9-safe form (for example Optional[int]/Union[int, None]) or enable postponed evaluation in this module.
Useful? React with 👍 / 👎.
Closes #3096
Summary
This PR adds the ability to configure the OpenAI SDK log level, addressing the feature request in #3096.
Problem
Previously, the SDK only supported
"debug"and"info"log levels via theOPENAI_LOGenvironment variable. Users could not set higher log levels like"warning"or"error"to reduce log verbosity.Solution
Three ways to configure the log level:
1. Programmatic API:
openai.set_log_level()Supported string levels:
"debug","info","warning","warn","error","critical","fatal"2. New environment variable:
OPENAI_LOG_LEVELexport OPENAI_LOG_LEVEL=warningTakes precedence over the legacy
OPENAI_LOGvariable.3. Extended
OPENAI_LOGenvironment variable (backwards compatible)The existing
OPENAI_LOGvariable now also supports"warning","error", and"critical"in addition to"debug"and"info".Changes
src/openai/_utils/_logs.py— Core implementation:_LOG_LEVEL_MAP,_parse_log_level(), extendedset_log_level(), and updatedsetup_logging()src/openai/_utils/__init__.py— Exportset_log_levelsrc/openai/__init__.py— Exportset_log_levelin public API and__all__tests/test_utils/test_logging.py— 16 new tests covering all functionality