xtekky/gpt4free · error · ValueError

Invalid thinking mode: {think_value!r}

Error message

Invalid thinking mode: {think_value!r}

What it means

ValueError from Gemini._resolve_model when the model string uses the @think= suffix syntax (e.g. "gemini-2.5-flash@think=high") but the value after @think= is not parseable as an integer. The code does int(think_value) and converts the resulting ValueError into a descriptive one; think levels must be numeric (0-4).

Source

Thrown at g4f/Provider/needs_auth/Gemini.py:250

            ) from exc
        buffer += chunk
        while b"\n" in buffer:
            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:

View on GitHub (pinned to 973504e177)

Solutions

  1. Use an integer: "gemini-2.5-flash@think=2".
  2. Or drop the suffix entirely and use the think_override parameter instead.
  3. Strip whitespace/typos from the suffix value.

Example fix

# before
model = "gemini-2.5-flash@think=high"  # ValueError: Invalid thinking mode: 'high'

# after
model = "gemini-2.5-flash@think=2"
Defensive patterns

Strategy: validation

Validate before calling

import re

def valid_think_suffix(model: str) -> bool:
    m = re.search(r"@think=(.+)$", model)
    if not m:
        return True
    return m.group(1).strip().lstrip("-").isdigit()

assert valid_think_suffix(requested_model), f"bad @think= suffix: {requested_model}"

Type guard

def is_valid_think_model(model: str) -> bool:
    if "@think=" not in model:
        return True
    value = model.rsplit("@think=", 1)[1]
    return value.isdigit() and 0 <= int(value) <= 4

Try / catch

try:
    resp = await client.chat.completions.create(model=m, provider=Gemini, messages=msgs)
except ValueError as e:
    if "Invalid thinking mode" in str(e):
        m = m.rsplit("@think=", 1)[0]

Prevention

When it happens

Trigger: Passing a model like "gemini@think=high" or "gemini@think=true" — any non-integer think value. The rsplit on "@think=" succeeds so parsing is attempted and fails.

Common situations: Assuming textual levels (low/medium/high) are supported; typos like @think=2.5 (float string) or trailing whitespace; copying model names from docs of a different library.

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/482aa6249241085e. Report an issue: GitHub.