{"record":{"id":"e7228a4b7772be89","repo":"oobabooga/textgen","slug":"max-tokens-must-be-greater-than-or-equal-to-0","errorCode":null,"errorMessage":"max_tokens must be greater than or equal to 0.","messagePattern":"max_tokens must be greater than or equal to 0\\.","errorType":"exception","errorClass":"InvalidRequestError","httpStatus":400,"severity":"error","filePath":"modules/api/completions.py","lineNumber":833,"sourceCode":"                    elif isinstance(content, list):\n                        for item in content:\n                            if isinstance(item, dict) and item.get('type') == 'text':\n                                prompt_text += item.get('text', '')\n\n            # Allow empty prompts for image-only requests\n            body[prompt_str] = prompt_text\n        else:\n            raise InvalidRequestError(\"Missing required input\", param=prompt_str)\n\n    # common params\n    generate_params = process_parameters(body, is_legacy=is_legacy)\n    max_tokens = generate_params['max_new_tokens']\n    if max_tokens is None:\n        generate_params['max_new_tokens'] = 512\n        generate_params['auto_max_new_tokens'] = True\n        max_tokens = 512\n    elif max_tokens < 0:\n        raise InvalidRequestError(message=\"max_tokens must be greater than or equal to 0.\", param=\"max_tokens\")\n    elif max_tokens == 0 and body.get('logprobs') is None:\n        raise InvalidRequestError(message=\"max_tokens is 0 but no logprobs parameter was specified.\", param=\"max_tokens\")\n\n    generate_params['stream'] = stream\n    if stop_event is not None:\n        generate_params['stop_event'] = stop_event\n    requested_model = generate_params.pop('model')\n    logprob_proc = generate_params.pop('logprob_proc', None)\n    if logprob_proc:\n        logprob_proc.token_alternatives_history.clear()\n    suffix = body['suffix'] if body['suffix'] else ''\n    echo = body['echo']\n\n    # Add messages to generate_params if present for multimodal processing\n    if body.get('messages'):\n        generate_params['messages'] = body['messages']\n        raw_images = convert_openai_messages_to_images(generate_params['messages'])\n        if raw_images:","sourceCodeStart":815,"sourceCodeEnd":851,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/modules/api/completions.py#L815-L851","documentation":"In the text completions path, max_tokens (mapped to max_new_tokens) must be >= 0; negative values raise InvalidRequestError (400, param='max_tokens'). Unlike the chat path, 0 is allowed here but only together with 'logprobs' (see error 14); None means auto (512 + auto_max_new_tokens).","triggerScenarios":"POST /v1/completions with {\"max_tokens\": -1} or any negative value, e.g. computed as remaining_budget where the budget is overshot.","commonSituations":"Using -1 as an 'unlimited' convention (this server wants None/omission for auto); arithmetic bugs in sliding-window token managers; porting configs from backends that accept negative max_tokens.","solutions":["Send a non-negative integer, or omit max_tokens for auto sizing.","Clamp computed values: max(0, n) client-side.","Use 0 only when also requesting logprobs."],"exampleFix":"# before\n{\"model\": \"x\", \"prompt\": p, \"max_tokens\": -1}\n\n# after\n{\"model\": \"x\", \"prompt\": p}  # omit for auto, or set e.g. \"max_tokens\": 256","handlingStrategy":"validation","validationCode":"def safe_max_tokens(v):\n    if v is None or v >= 0:\n        return v\n    return None  # negative -> omit for auto sizing","typeGuard":"def is_valid_completion_max_tokens(v) -> bool:\n    return v is None or (isinstance(v, int) and not isinstance(v, bool) and v >= 0)","tryCatchPattern":"try:\n    resp = client.completions.create(model=m, prompt=p, max_tokens=mt)\nexcept openai.BadRequestError as e:\n    if 'greater than or equal to 0' in str(e):\n        resp = client.completions.create(model=m, prompt=p)  # omit: auto\n    else:\n        raise","preventionTips":["Clamp negatives to 0 or omit the field client-side.","Use omission, not -1, to request auto sizing.","Add a unit test on your token-budget arithmetic."],"tags":["openai-api","completions","max-tokens","validation"],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}