fix(llm): filter temperature parameter for OpenAI reasoning models#1526
fix(llm): filter temperature parameter for OpenAI reasoning models#1526
Conversation
Greptile OverviewGreptile SummaryThis PR fixes a critical bug where OpenAI reasoning models (o1, o3, gpt-5 series excluding gpt-5-chat) fail with Key Changes:
Issues Found:
|
| Filename | Score | Overview |
|---|---|---|
| nemoguardrails/actions/llm/utils.py | 4/5 | Added _filter_params_for_openai_reasoning_models() to remove temperature param for o1/o3/gpt-5 models before binding, fixing API errors |
| tests/test_actions_llm_utils.py | 5/5 | Comprehensive test coverage with 13 test cases covering reasoning models (o1, o3, gpt-5*), regular models (gpt-4*), and edge cases |
Sequence Diagram
sequenceDiagram
participant User
participant LLMCall as llm_call()
participant Filter as _filter_params_for_openai_reasoning_models()
participant LLM as LangChain LLM
participant API as OpenAI API
User->>LLMCall: call with llm_params={temperature: 0.001}
LLMCall->>Filter: filter params for reasoning model
Filter->>Filter: check if model is o1/o3/gpt-5*
alt is reasoning model
Filter->>Filter: remove temperature parameter
Filter-->>LLMCall: {max_tokens: ...} (temp removed)
else not reasoning model
Filter-->>LLMCall: {temperature: 0.001, max_tokens: ...}
end
LLMCall->>LLM: llm.bind(**filtered_params)
LLM->>API: API call with allowed params
API-->>LLM: success response
LLM-->>LLMCall: response
LLMCall-->>User: generated text
There was a problem hiding this comment.
Additional Comments (3)
-
nemoguardrails/library/hallucination/actions.py, line 82 (link)logic: this
.bind(temperature=1.0)call will fail with OpenAI reasoning models since it bypasses the_filter_params_for_openai_reasoning_models()function. Consider importing and using the filter function here or calling throughllm_call() -
nemoguardrails/evaluate/evaluate_factcheck.py, line 98 (link)logic: this
.bind(temperature=0.8)call will fail with OpenAI reasoning models. Consider importing and using_filter_params_for_openai_reasoning_models()function -
nemoguardrails/evaluate/evaluate_hallucination.py, line 74 (link)logic: if
llm_paramscontainstemperature, this.bind()call will fail with OpenAI reasoning models. Consider importing and using_filter_params_for_openai_reasoning_models()function
2 files reviewed, 4 comments
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
tgasser-nv
left a comment
There was a problem hiding this comment.
LGTM, just a small comment on how to pick up OpenAI reasoning model names
OpenAI reasoning models (o1, o3, gpt-5 series excluding gpt-5-chat) only support `temperature=1`. When NeMo Guardrails uses `.bind(temperature=0.001)` for deterministic tasks like self-check input/output, the API returns an error: ``` Unsupported value: 'temperature' does not support 0.001 with this model. Only the default (1) value is supported. ``` This happens because LangChain's `ChatOpenAI` handles temperature restrictions at initialization time (setting `temperature=None` for reasoning models), but `.bind()` bypasses this protection and passes the temperature directly to the API. - Added `_filter_params_for_openai_reasoning_models()` function that: - Detects reasoning models by name (o1*, o3*, gpt-5* excluding gpt-5-chat) - Removes the `temperature` parameter before binding for these models - Follows the same pattern as LangChain's `validate_temperature` validator
a41d963 to
6a0fcb4
Compare
OpenAI reasoning models (o1, o3, gpt-5 series excluding gpt-5-chat) only support
temperature=1. When NeMo Guardrails uses.bind(temperature=0.001)for deterministic tasks like self-check input/output, the API returns an error:This happens because LangChain's
ChatOpenAIhandles temperature restrictions at initialization time (settingtemperature=Nonefor reasoning models), but.bind()bypasses this protection and passes the temperature directly to the API._filter_params_for_openai_reasoning_models()function that:temperatureparameter before binding for these modelsvalidate_temperaturevalidatorto repro
or
Fixes: #1512 #992