Skip to content

feat: add configurable SDK log level (#3096)#3197

Open
lrg913427-dot wants to merge 1 commit intoopenai:mainfrom
lrg913427-dot:feat/configurable-log-level
Open

feat: add configurable SDK log level (#3096)#3197
lrg913427-dot wants to merge 1 commit intoopenai:mainfrom
lrg913427-dot:feat/configurable-log-level

Conversation

@lrg913427-dot
Copy link
Copy Markdown

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 the OPENAI_LOG environment 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()

import openai

openai.set_log_level("warning")  # string level (case-insensitive)
openai.set_log_level(logging.WARNING)  # numeric level

Supported string levels: "debug", "info", "warning", "warn", "error", "critical", "fatal"

2. New environment variable: OPENAI_LOG_LEVEL

export OPENAI_LOG_LEVEL=warning

Takes precedence over the legacy OPENAI_LOG variable.

3. Extended OPENAI_LOG environment variable (backwards compatible)

The existing OPENAI_LOG variable 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(), extended set_log_level(), and updated setup_logging()
  • src/openai/_utils/__init__.py — Export set_log_level
  • src/openai/__init__.py — Export set_log_level in public API and __all__
  • tests/test_utils/test_logging.py — 16 new tests covering all functionality

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
@lrg913427-dot lrg913427-dot requested a review from a team as a code owner May 2, 2026 23:56
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

set post and response log level

1 participant