fix(openai): make GPT-5 temperature validation case-insensitive (#34012)

Fixed a bug where GPT-5 temperature validation was case-sensitive,
causing issues when users
specified Azure deployment names or model names in uppercase (e.g.,
`"GPT-5-2025-01-01"`, `"GPT-5-NANO"`). The validation now correctly
handles model names regardless of case.

  Changes made:
- Updated `validate_temperature()` method in `BaseChatOpenAI` to perform
case-insensitive
  model name comparisons
- Updated `_get_encoding_model()` method to use case-insensitive checks
for tiktoken encoder
  selection
- Added comprehensive unit tests to verify case-insensitive behavior
with various case
  combinations

  **Issue:** Fixes #34003

  **Dependencies:** None

  **Test Coverage:**
  - All existing tests pass
- New test `test_gpt_5_temperature_case_insensitive` covers uppercase,
lowercase, and
  mixed-case model names
- Tests verify both non-chat GPT-5 models (temperature removed) and chat
models (temperature
  preserved)
  - Lint and format checks pass (`make lint`, `make format`)

---------

Co-authored-by: Mason Daugherty <github@mdrxy.com>
This commit is contained in:
Abhinav
2025-11-24 06:47:03 +05:30
committed by GitHub
parent 4e4e5d7337
commit 2ba3ce81a6
2 changed files with 36 additions and 7 deletions

View File

@@ -826,14 +826,15 @@ class BaseChatOpenAI(BaseChatModel):
(Defaults to 1)
"""
model = values.get("model_name") or values.get("model") or ""
model_lower = model.lower()
# For o1 models, set temperature=1 if not provided
if model.startswith("o1") and "temperature" not in values:
if model_lower.startswith("o1") and "temperature" not in values:
values["temperature"] = 1
# For gpt-5 models, handle temperature restrictions
# Note that gpt-5-chat models do support temperature
if model.startswith("gpt-5") and "chat" not in model:
if model_lower.startswith("gpt-5") and "chat" not in model_lower:
temperature = values.get("temperature")
if temperature is not None and temperature != 1:
# For gpt-5 (non-chat), only temperature=1 is supported
@@ -1668,15 +1669,13 @@ class BaseChatOpenAI(BaseChatModel):
model = self.tiktoken_model_name
else:
model = self.model_name
try:
encoding = tiktoken.encoding_for_model(model)
except KeyError:
model_lower = model.lower()
encoder = "cl100k_base"
if (
self.model_name.startswith("gpt-4o")
or self.model_name.startswith("gpt-4.1")
or self.model_name.startswith("gpt-5")
):
if model_lower.startswith(("gpt-4o", "gpt-4.1", "gpt-5")):
encoder = "o200k_base"
encoding = tiktoken.get_encoding(encoder)
return model, encoding