Repository navigation
fix(openai): preserve reasoning_effort summary field for Responses API #23151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,22 @@ def _normalize_reasoning_effort_for_chat_completion( | |
| return None | ||
|
|
||
|
|
||
| def _get_effort_level(value: Union[str, dict, None]) -> Optional[str]: | ||
| """Extract the effective effort level from reasoning_effort (string or dict). | ||
|
|
||
| Use this for guards that compare effort level (e.g. xhigh validation, "none" checks). | ||
| Ensures dict inputs like {"effort": "none", "summary": "detailed"} are correctly | ||
| treated as effort="none" for validation purposes. | ||
| """ | ||
| if value is None: | ||
| return None | ||
| if isinstance(value, str): | ||
| return value | ||
| if isinstance(value, dict) and "effort" in value: | ||
| return value["effort"] | ||
| return None | ||
|
|
||
|
|
||
| class OpenAIGPT5Config(OpenAIGPTConfig): | ||
| """Configuration for gpt-5 models including GPT-5-Codex variants. | ||
|
|
||
|
|
@@ -70,6 +86,19 @@ def is_model_gpt_5_4_model(cls, model: str) -> bool: | |
| model_name = model.split("/")[-1] | ||
| return model_name.startswith("gpt-5.4") | ||
|
|
||
| @classmethod | ||
| def is_model_gpt_5_4_plus_model(cls, model: str) -> bool: | ||
| """Check if the model is gpt-5.4 or newer (5.4, 5.5, 5.6, etc., including pro).""" | ||
| model_name = model.split("/")[-1] | ||
| if not model_name.startswith("gpt-5."): | ||
| return False | ||
| try: | ||
| version_str = model_name.replace("gpt-5.", "").split("-")[0] | ||
| major = version_str.split(".")[0] | ||
| return int(major) >= 4 | ||
| except (ValueError, IndexError): | ||
| return False | ||
|
Comment on lines
+89
to
+100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hardcoded model-version parsing violates the "no model flags in code" rule
Per the custom rule, the capability flag ( Rule Used: What: Do not hardcode model-specific flags in the ... (source) |
||
|
|
||
| @classmethod | ||
| def _supports_reasoning_effort_level(cls, model: str, level: str) -> bool: | ||
| """Check if the model supports a specific reasoning_effort level. | ||
|
|
@@ -150,21 +179,32 @@ def map_openai_params( | |
| drop_params=drop_params, | ||
| ) | ||
|
|
||
| # Normalize reasoning_effort: chat completion API expects a string, not a dict | ||
| # (e.g. {'effort': 'high', 'summary': 'detailed'} -> 'high') | ||
| # Get raw reasoning_effort and effective effort level for all guards. | ||
| # Use effective_effort (extracted string) for xhigh validation, "none" checks, and | ||
| # tool/sampling guards — dict inputs like {"effort": "none", "summary": "detailed"} | ||
| # must be treated as effort="none" to avoid incorrect tool-drop or sampling errors. | ||
| raw_reasoning_effort = ( | ||
| non_default_params.get("reasoning_effort") | ||
| or optional_params.get("reasoning_effort") | ||
| ) | ||
| normalized = _normalize_reasoning_effort_for_chat_completion(raw_reasoning_effort) | ||
| if raw_reasoning_effort is not None and normalized is not None: | ||
| if "reasoning_effort" in non_default_params: | ||
| non_default_params["reasoning_effort"] = normalized | ||
| if "reasoning_effort" in optional_params: | ||
| optional_params["reasoning_effort"] = normalized | ||
|
|
||
| reasoning_effort = normalized or raw_reasoning_effort | ||
| if reasoning_effort is not None and reasoning_effort == "xhigh": | ||
| effective_effort = _get_effort_level(raw_reasoning_effort) | ||
|
|
||
| # Normalize to string for Chat Completions API when dict has only "effort". | ||
| # Preserve full dict (e.g. {"effort": "high", "summary": "detailed"}) for Responses API. | ||
| if isinstance(raw_reasoning_effort, dict) and set(raw_reasoning_effort.keys()) <= {"effort"}: | ||
| normalized = _normalize_reasoning_effort_for_chat_completion(raw_reasoning_effort) | ||
| if normalized is not None: | ||
| if "reasoning_effort" in non_default_params: | ||
| non_default_params["reasoning_effort"] = normalized | ||
| if "reasoning_effort" in optional_params: | ||
| optional_params["reasoning_effort"] = normalized | ||
|
|
||
| reasoning_effort = ( | ||
| non_default_params.get("reasoning_effort") | ||
| or optional_params.get("reasoning_effort") | ||
| or raw_reasoning_effort | ||
| ) | ||
| if effective_effort is not None and effective_effort == "xhigh": | ||
| if not self._supports_reasoning_effort_level(model, "xhigh"): | ||
| if litellm.drop_params or drop_params: | ||
| non_default_params.pop("reasoning_effort", None) | ||
|
|
@@ -191,17 +231,20 @@ def map_openai_params( | |
| has_tools = bool( | ||
| non_default_params.get("tools") or optional_params.get("tools") | ||
| ) | ||
| if has_tools and reasoning_effort not in (None, "none"): | ||
| non_default_params.pop("reasoning_effort", None) | ||
| optional_params.pop("reasoning_effort", None) | ||
| reasoning_effort = None | ||
| if has_tools and effective_effort not in (None, "none"): | ||
| # Check if this will be routed to Responses API | ||
| # If so, keep reasoning_effort; otherwise drop it for chat completions API | ||
| if not self.is_model_gpt_5_4_plus_model(model): | ||
| non_default_params.pop("reasoning_effort", None) | ||
| optional_params.pop("reasoning_effort", None) | ||
| reasoning_effort = None | ||
|
|
||
| # gpt-5.1/5.2 support logprobs, top_p, top_logprobs only when reasoning_effort="none" | ||
| supports_none = self._supports_reasoning_effort_level(model, "none") | ||
| if supports_none: | ||
| sampling_params = ["logprobs", "top_logprobs", "top_p"] | ||
| has_sampling = any(p in non_default_params for p in sampling_params) | ||
| if has_sampling and reasoning_effort not in (None, "none"): | ||
| if has_sampling and effective_effort not in (None, "none"): | ||
| if litellm.drop_params or drop_params: | ||
| for p in sampling_params: | ||
| non_default_params.pop(p, None) | ||
|
|
@@ -211,15 +254,15 @@ def map_openai_params( | |
| "gpt-5.1/5.2/5.4 only support logprobs, top_p, top_logprobs when " | ||
| "reasoning_effort='none'. Current reasoning_effort='{}'. " | ||
| "To drop unsupported params set `litellm.drop_params = True`" | ||
| ).format(reasoning_effort), | ||
| ).format(effective_effort), | ||
| status_code=400, | ||
| ) | ||
|
|
||
| if "temperature" in non_default_params: | ||
| temperature_value: Optional[float] = non_default_params.pop("temperature") | ||
| if temperature_value is not None: | ||
| # models supporting reasoning_effort="none" also support flexible temperature | ||
| if supports_none and (reasoning_effort == "none" or reasoning_effort is None): | ||
| if supports_none and (effective_effort == "none" or effective_effort is None): | ||
| optional_params["temperature"] = temperature_value | ||
| elif temperature_value == 1: | ||
| optional_params["temperature"] = temperature_value | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate implementation of
_normalize_reasoning_effort_for_chat_completion_get_effort_level(lines 28–41) and_normalize_reasoning_effort_for_chat_completion(lines 11–25) have byte-for-byte identical implementations. Both functions:NoneforNoneinputvalue["effort"]for a dict that contains"effort"Noneas a fallbackHaving two functions with the same logic but different names makes it easy for them to diverge in the future. Consider removing
_normalize_reasoning_effort_for_chat_completionand calling_get_effort_leveleverywhere, or at least making one delegate to the other (e.g._normalize_reasoning_effort_for_chat_completion = _get_effort_level).