xtekky/gpt4free · error · ValueError
Thinking mode must be an integer between 0 and 4
Error message
Thinking mode must be an integer between 0 and 4
What it means
ValueError from Gemini._resolve_model: a thinking level was resolved (via @think= suffix or think_override) but it is not an integer in the inclusive range 0-4. Gemini's thinking-mode knob only accepts five discrete levels, so anything outside that band — or a non-int type from think_override — is rejected before any request is made.
Source
Thrown at g4f/Provider/needs_auth/Gemini.py:253
line, buffer = buffer.split(b"\n", 1)
yield line.decode("utf-8", errors="replace")
if buffer:
yield buffer.decode("utf-8", errors="replace")
def _resolve_model(model: str, think_override: int = None) -> tuple[str, bool]:
requested_model = model
think_mode = think_override
if "@think=" in model:
model, think_value = model.rsplit("@think=", 1)
requested_model = model
try:
think_mode = int(think_value)
except ValueError as exc:
raise ValueError(f"Invalid thinking mode: {think_value!r}") from exc
if think_mode is not None:
if not isinstance(think_mode, int) or not 0 <= think_mode <= 4:
raise ValueError("Thinking mode must be an integer between 0 and 4")
model = MODEL_ALIASES.get(model, model)
if model not in models:
raise ValueError(
f"Unknown Gemini model: {model}. " f"Supported models: {', '.join(models)}"
)
expanded_thinking = (
requested_model in EXPANDED_MODEL_ALIASES
if think_mode is None
else think_mode <= 2
)
return model, expanded_thinking
def _normalize_messages(messages: Messages | None) -> Messages:
if messages is None:
return []
if not isinstance(messages, list):
raise TypeError("messages must be a list")View on GitHub (pinned to 973504e177)
Solutions
- Clamp the think level to 0-4, e.g. "@think=3".
- Pass think_override as a plain int, not a string or bool.
- Validate user-supplied think values before forwarding them to the provider.
Example fix
# before model = "gemini-2.5-flash@think=9" # ValueError: Thinking mode must be an integer between 0 and 4 # after model = "gemini-2.5-flash@think=4"
Defensive patterns
Strategy: validation
Validate before calling
def clamp_think_level(value):
n = int(value)
if not 0 <= n <= 4:
raise ValueError(f"think level {n} out of range 0-4")
return n
safe_level = clamp_think_level(user_input) Type guard
def is_valid_think_override(value) -> bool:
return type(value) is int and 0 <= value <= 4 Try / catch
try:
resp = await client.chat.completions.create(model=m, provider=Gemini, messages=msgs, think_override=lvl)
except ValueError as e:
if "between 0 and 4" in str(e):
lvl = max(0, min(4, lvl))
resp = await client.chat.completions.create(model=m, provider=Gemini, messages=msgs, think_override=lvl) Prevention
- Clamp think levels to 0-4 at the config layer.
- Use type(value) is int to reject bools and strings.
- Document the 0-4 scale wherever users configure thinking mode.
When it happens
Trigger: Passing @think=7 or @think=-1 in the model name, or a think_override that is a bool/float/string. Note the is-instance check also catches values like True (bool is an int subclass but the isinstance gate is on int, so bools and other types fail).
Common situations: Guessing a 0-10 scale from other providers; passing think_override as a string "2"; copy-pasted configs from tools using a different level range.
Related errors
- Invalid thinking mode: {think_value!r}
- Unknown Gemini model: {model}. Supported models: {', '.join(
- No refresh token found in GCP_SERVICE_ACCOUNT.
- No project information found in API response.
- No Yupp accounts configured. Set YUPP_API_KEY environment va
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/e49516e0e97f28d7.
Report an issue: GitHub.