{"record":{"id":"3634ecdb32a0300b","repo":"oobabooga/textgen","slug":"max-tokens-is-0-but-no-logprobs-parameter-was-spec","errorCode":null,"errorMessage":"max_tokens is 0 but no logprobs parameter was specified.","messagePattern":"max_tokens is 0 but no logprobs parameter was specified\\.","errorType":"exception","errorClass":"InvalidRequestError","httpStatus":400,"severity":"error","filePath":"modules/api/completions.py","lineNumber":835,"sourceCode":"                            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:\n            logger.info(f\"Found {len(raw_images)} image(s) in request.\")\n            generate_params['raw_images'] = raw_images","sourceCodeStart":817,"sourceCodeEnd":853,"githubUrl":"https://github.com/oobabooga/textgen/blob/ed888c71f221df552750e1834b3654abab8ae345/modules/api/completions.py#L817-L853","documentation":"In the text completions path, max_tokens=0 is a special mode for returning only prompt logprobs without generating anything. Setting 0 without also setting the 'logprobs' parameter is rejected with InvalidRequestError (400, param='max_tokens'), since a 0-token completion with no logprobs would return nothing useful.","triggerScenarios":"POST /v1/completions with {\"max_tokens\": 0} and no 'logprobs' key, or with {\"max_tokens\": 0, \"logprobs\": null}.","commonSituations":"Token-budget managers that legitimately compute 0 remaining tokens; test payloads with 0; users trying to 'just count tokens' without knowing the logprobs convention.","solutions":["If you truly want zero generation, add a logprobs parameter: {\"max_tokens\": 0, \"logprobs\": 5}.","Otherwise clamp to at least 1, or omit max_tokens to let the server auto-size.","For pure token counting, use the /v1/token-count or token encoding endpoints instead of a 0-token completion."],"exampleFix":"# before\n{\"model\": \"x\", \"prompt\": p, \"max_tokens\": 0}\n\n# after\n{\"model\": \"x\", \"prompt\": p, \"max_tokens\": 0, \"logprobs\": 5}","handlingStrategy":"validation","validationCode":"def normalize_completion_params(body: dict) -> dict:\n    mt = body.get('max_tokens')\n    if mt == 0 and body.get('logprobs') is None:\n        body.pop('max_tokens', None)  # or set logprobs\n    return body","typeGuard":"def zero_tokens_ok(body: dict) -> bool:\n    return body.get('max_tokens') != 0 or body.get('logprobs') is not None","tryCatchPattern":"try:\n    resp = client.completions.create(model=m, prompt=p, max_tokens=0)\nexcept openai.BadRequestError as e:\n    if 'no logprobs parameter' in str(e):\n        resp = client.completions.create(model=m, prompt=p, max_tokens=0, logprobs=5)\n    else:\n        raise","preventionTips":["Treat max_tokens=0 as logprob-query mode; always pair it with logprobs.","For token counting use the dedicated token endpoints, not 0-token completions.","Clamp budget managers to >= 1 for normal generation."],"tags":["openai-api","completions","max-tokens","logprobs","validation"],"backgroundTag":null,"analyzedSha":"ed888c71f221df552750e1834b3654abab8ae345","analyzedAt":"2026-08-15T05:24:21.000Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}