{"record":{"id":"e49516e0e97f28d7","repo":"xtekky/gpt4free","slug":"thinking-mode-must-be-an-integer-between-0-and-4","errorCode":null,"errorMessage":"Thinking mode must be an integer between 0 and 4","messagePattern":"Thinking mode must be an integer between 0 and 4","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Gemini.py","lineNumber":253,"sourceCode":"            line, buffer = buffer.split(b\"\\n\", 1)\n            yield line.decode(\"utf-8\", errors=\"replace\")\n    if buffer:\n        yield buffer.decode(\"utf-8\", errors=\"replace\")\n\n\ndef _resolve_model(model: str, think_override: int = None) -> tuple[str, bool]:\n    requested_model = model\n    think_mode = think_override\n    if \"@think=\" in model:\n        model, think_value = model.rsplit(\"@think=\", 1)\n        requested_model = model\n        try:\n            think_mode = int(think_value)\n        except ValueError as exc:\n            raise ValueError(f\"Invalid thinking mode: {think_value!r}\") from exc\n    if think_mode is not None:\n        if not isinstance(think_mode, int) or not 0 <= think_mode <= 4:\n            raise ValueError(\"Thinking mode must be an integer between 0 and 4\")\n    model = MODEL_ALIASES.get(model, model)\n    if model not in models:\n        raise ValueError(\n            f\"Unknown Gemini model: {model}. \" f\"Supported models: {', '.join(models)}\"\n        )\n    expanded_thinking = (\n        requested_model in EXPANDED_MODEL_ALIASES\n        if think_mode is None\n        else think_mode <= 2\n    )\n    return model, expanded_thinking\n\n\ndef _normalize_messages(messages: Messages | None) -> Messages:\n    if messages is None:\n        return []\n    if not isinstance(messages, list):\n        raise TypeError(\"messages must be a list\")","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Gemini.py#L235-L271","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"# before\nmodel = \"gemini-2.5-flash@think=9\"  # ValueError: Thinking mode must be an integer between 0 and 4\n\n# after\nmodel = \"gemini-2.5-flash@think=4\"","handlingStrategy":"validation","validationCode":"def clamp_think_level(value):\n    n = int(value)\n    if not 0 <= n <= 4:\n        raise ValueError(f\"think level {n} out of range 0-4\")\n    return n\n\nsafe_level = clamp_think_level(user_input)","typeGuard":"def is_valid_think_override(value) -> bool:\n    return type(value) is int and 0 <= value <= 4","tryCatchPattern":"try:\n    resp = await client.chat.completions.create(model=m, provider=Gemini, messages=msgs, think_override=lvl)\nexcept ValueError as e:\n    if \"between 0 and 4\" in str(e):\n        lvl = max(0, min(4, lvl))\n        resp = await client.chat.completions.create(model=m, provider=Gemini, messages=msgs, think_override=lvl)","preventionTips":["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."],"tags":["validation","gemini","range-check","configuration"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}