Repository navigation
Conversation
PR Summary by QodoForward reasoning_effort to Grok behind opt-in compatibility flag
AI Description
Diagram
High-Level Assessment
Files changed (3)
|
Code Review by Qodo
1. Grok reasoning injected broadly
|
| grok_model = model.rsplit('/', 1)[-1] | ||
| if model_base.startswith('gpt-5'): | ||
| # Use configured reasoning_effort or default to MEDIUM | ||
| config_effort = get_settings().config.reasoning_effort |
There was a problem hiding this comment.
1. Grok reasoning injected broadly 📘 Rule violation ⛨ Security
The new Grok branch injects reasoning_effort/allowed_openai_params based only on the model basename, so any provider-prefixed model ending with grok-* (including OpenRouter-routed models) can incorrectly receive Grok-specific kwargs. This risks cross-provider configuration leakage and can cause unexpected request behavior/failures, including overriding OpenRouter’s separate reasoning configuration by sourcing from config.reasoning_effort instead of openrouter.reasoning_effort.
Agent Prompt
## Issue description
`enable_grok_reasoning_effort` currently injects Grok-specific `reasoning_kwargs` (including `reasoning_effort` and `allowed_openai_params`) whenever the model’s basename starts with `grok-`, regardless of the actual provider indicated by the model prefix. This can leak Grok-only kwargs into non-xAI providers (notably `openrouter/.../grok-*`), altering OpenRouter requests and conflicting with OpenRouter’s dedicated reasoning configuration.
## Issue Context
- Compliance requires computing/using provider identity before injecting provider-specific kwargs, and only applying those kwargs when the resolved provider matches.
- Current Grok detection is basename-based (`model.rsplit('/', 1)[-1]`), so provider-prefixed models ending in `grok-*` also match.
- OpenRouter already configures reasoning via its own block (using `openrouter.reasoning_effort` and `extra_body["reasoning"]`), so adding top-level `reasoning_effort` sourced from `config.reasoning_effort` changes behavior for OpenRouter-routed Grok models.
- Suggested direction: make the Grok condition provider-aware (e.g., skip when `model.startswith("openrouter/")` or reuse an existing `is_openrouter` flag), so OpenRouter-routed Grok models are controlled only by the OpenRouter block.
## Fix Focus Areas
- pr_agent/algo/ai_handlers/litellm_ai_handler.py[450-536]
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
IsmaelMartinez
left a comment
There was a problem hiding this comment.
Changes needed, and an apology first: five weeks with no maintainer reply on this is too long, and that one is ours.
The mechanism and the tests both hold up, but the gate is too broad. With the flag on, openrouter/x-ai/grok-4 picks up reasoning_effort from [config] on top of the extra_body.reasoning the OpenRouter block already set from [openrouter]. Suggestion inline.
allowed_openai_params also pushes the field past litellm's own capability check, so xai/grok-2 and xai/grok-3 receive it too. MAX_TOKENS has no xai/ Grok past 3, so grok-4.5 and grok-4.20-multi-agent raise before a request is built unless custom_model_max_tokens is set. Their entries belong in this PR.
configuration.toml needs a rebase, and CI has not run on this head yet.
| elif grok_model.startswith('grok-') and get_settings().config.get( | ||
| "enable_grok_reasoning_effort", False): |
There was a problem hiding this comment.
grok_model is the basename, so openrouter/x-ai/grok-4 lands here too and gets reasoning_effort from [config] while the OpenRouter block below is already setting extra_body.reasoning from [openrouter].reasoning_effort. is_openrouter is already computed above.
| elif grok_model.startswith('grok-') and get_settings().config.get( | |
| "enable_grok_reasoning_effort", False): | |
| elif (grok_model.startswith('grok-') and not is_openrouter | |
| and get_settings().config.get("enable_grok_reasoning_effort", False)): |
| ) | ||
| reasoning_kwargs = { | ||
| "reasoning_effort": effort, | ||
| "allowed_openai_params": ["reasoning_effort"], |
There was a problem hiding this comment.
On the pinned litellm 1.98 this is doing more than it looks: grok-2 and grok-3 have reasoning_effort dropped without the allowlist and included with it, and those are the only xai/ Grok families MAX_TOKENS covers today. grok-4.5 does not need the allowlist at all; grok-4.20-multi-agent genuinely does. Worth narrowing it to the models that need it, and adding the Grok 4.x MAX_TOKENS entries so the documented models can actually run.
| return response | ||
|
|
||
|
|
||
| async def _run_completion(monkeypatch, model, reasoning_effort="medium", enabled=False): |
There was a problem hiding this comment.
Qodo's second finding applies here: test_litellm_reasoning_effort.py deletes AWS_USE_IMDS and the AWS/OPENAI vars before constructing the handler, because __init__ branches on them. Worth matching.
Add opt-in model-supported reasoning_effort forwarding for bare and provider-prefixed Grok models without changing temperature. Tests: 1346 passed, 1 skipped, 1 xfailed.
…dels Addresses @IsmaelMartinez's review on The-PR-Agent#2530. All three findings verified against the code and reproduced before fixing. 1. OpenRouter double-configuration. He wrote: "grok_model is the basename, so openrouter/x-ai/grok-4 lands here too and gets reasoning_effort from [config] while the OpenRouter block below is already setting extra_body.reasoning from [openrouter]." Reproduced exactly -- with the flag on, openrouter/x-ai/grok-4.5 came out with BOTH top-level reasoning_effort='high' (from [config]) and extra_body.reasoning={'effort':'high'} (from [openrouter]). Adopted his inline `not is_openrouter` suggestion; the OpenRouter block is now the sole owner of that path. 2. allowed_openai_params too broad. He wrote: "grok-2 and grok-3 have reasoning_effort dropped without the allowlist and included with it ... grok-4.5 does not need the allowlist at all; grok-4.20-multi-agent genuinely does." Confirmed against the pinned litellm: get_supported_openai_params reports reasoning_effort for xai/grok-4.5 and xai/grok-4.20-multi-agent but not for xai/grok-3. The allowlist is now added only when litellm does not already report the param, mirroring how main does it for its own Grok path. 3. MAX_TOKENS entries. Added xai/grok-4.20-multi-agent (main's The-PR-Agent#2871, merged today, added the 4.5/4.6 entries but not this one), so the documented xhigh-capable model can actually run without custom_model_max_tokens. Also, and this narrows the PR substantially: main gained native always-on Grok 4.5/4.6 reasoning support in The-PR-Agent#2871 while this sat. The opt-in branch now also skips any model main already covers via GROK_REASONING_EFFORT_LEVELS, so it no longer overrides main's clamping. The residual scope is xAI-direct Grok families main does not cover. Rebased onto main (a64730a); the configuration.toml conflict he flagged is resolved (kept main's add_user_to_requests + "max" effort alongside our flag). Tests: adds the OpenRouter-isolation regression, an allowlist-narrowing test, and a test that natively-supported models are left to main. Adopts the _restore_litellm_globals autouse fixture per his third note. tests/unittest: 2702 passed, 1 pre-existing failure (TestLiteLLMReasoningEffortGrok::...[grok-4.6-xhigh-False]) that reproduces identically on pristine upstream/main and is untouched by this branch. Co-authored-by: IsmaelMartinez <IsmaelMartinez@users.noreply.github.com>
11c7a1b to
7719da2
Compare
|
@IsmaelMartinez — rebased and all three findings fixed at head 1. The OpenRouter double-config — reproduced exactly as you described.
Built a probe over the real handler rather than reading the diff, and the doubled request is exactly what came out: Took your inline 2.
Checked each against the pinned litellm's own So the unconditional allowlist was pushing the param onto 3. While this sat, #2871 merged (today) and added native always-on Grok 4.5/4.6 support: Rather than let this PR fight main, I narrowed it: the opt-in branch now also skips any model main already covers, so it can't override main's clamping. This leaves the PR much smaller than when you reviewed it, and that's worth your explicit call: the residual is an opt-in flag for xAI-direct Grok families that main doesn't natively cover. If you'd rather see Grok models simply added to
Done — kept main's Tests: adds regressions for the OpenRouter isolation, the allowlist narrowing (both directions), and native-model deference; adopts the env/globals fixture per your third comment. Credit is in the commit trailer. |
| import openai | ||
| import pytest | ||
|
|
||
| import pr_agent.algo.ai_handlers.litellm_ai_handler as litellm_handler |
|
Thanks for the rebase, and for the honest read on scope: I think you called it right. With #2871 in, the registry route covers the models people actually use, so I will close this in favour of that and open the follow-up adding the On the failure you flagged, it is not main. |
|
Correction on the version I named: it is not specific to 1.93.0. |
|
Closing in favour of the registry route, as agreed. Thanks again for the rebase and for calling the scope honestly; the follow-up will carry the grok-4.20 and 4.3 ids with credit to you. |
|
Follow-up is up as #2890, with you as co-author. One narrowing worth flagging: I dropped the |
Summary
config.enable_grok_reasoning_effortcompatibility flaglow,medium, andhigheffort values to bare and provider-prefixed Grok models, plusxhighforgrok-4.20-multi-agentallowed_openai_params=["reasoning_effort"]opt-in when forwarding the fieldmediumwith a warning for values outside Grok 4.5's documented effort setCompatibility
The feature is disabled by default. Grok requests remain unchanged unless an operator explicitly enables
enable_grok_reasoning_effort.xAI's reasoning contract documents
low/medium/highfor Grok 4.5 and addsxhighforgrok-4.20-multi-agent: https://docs.x.ai/developers/model-capabilities/text/reasoningTesting
52 passed1346 passed, 1 skipped, 1 xfailed--max-line-length=120)main