{"record":{"id":"8012ff8cefb701c3","repo":"mlflow/mlflow","slug":"cannot-set-both-temperature-and-top-p-paramete","errorCode":null,"errorMessage":"Cannot set both 'temperature' and 'top_p' parameters.","messagePattern":"Cannot set both 'temperature' and 'top_p' parameters\\.","errorType":"http","errorClass":"AIGatewayException","httpStatus":422,"severity":"error","filePath":"mlflow/gateway/providers/anthropic.py","lineNumber":124,"sourceCode":"    for value in schema.values():\n        match value:\n            case dict():\n                _enforce_strict_schema(value)\n            case list():\n                for item in value:\n                    if isinstance(item, dict):\n                        _enforce_strict_schema(item)\n\n\nclass AnthropicAdapter(ProviderAdapter):\n    @classmethod\n    def chat_to_model(cls, payload, config):\n        key_mapping = {\"stop\": \"stop_sequences\"}\n        payload[\"model\"] = config.model.name\n        payload = rename_payload_keys(payload, key_mapping)\n\n        if \"top_p\" in payload and \"temperature\" in payload:\n            raise AIGatewayException(\n                status_code=422, detail=\"Cannot set both 'temperature' and 'top_p' parameters.\"\n            )\n\n        max_completion_tokens = payload.pop(\"max_completion_tokens\", None)\n        max_tokens = payload.get(\"max_tokens\") or max_completion_tokens\n        if max_tokens is None:\n            max_tokens = MLFLOW_AI_GATEWAY_ANTHROPIC_DEFAULT_MAX_TOKENS\n        if max_tokens > MLFLOW_AI_GATEWAY_ANTHROPIC_MAXIMUM_MAX_TOKENS:\n            raise AIGatewayException(\n                status_code=422,\n                detail=\"Invalid value for max_tokens: cannot exceed \"\n                f\"{MLFLOW_AI_GATEWAY_ANTHROPIC_MAXIMUM_MAX_TOKENS}.\",\n            )\n        payload[\"max_tokens\"] = max_tokens\n\n        if payload.pop(\"n\", 1) != 1:\n            raise AIGatewayException(\n                status_code=422,","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/gateway/providers/anthropic.py#L106-L142","documentation":"Anthropic's chat API rejects combining 'temperature' and 'top_p'; the MLflow gateway's Anthropic provider enforces this client-side and raises a 422 if both keys are present in the chat payload. Only one sampling knob may be set. Remove 'top_p' (keep 'temperature') to resolve.","triggerScenarios":"POSTing a chat request to an Anthropic gateway endpoint where both 'temperature' and 'top_p' keys exist in the payload after stop->stop_sequences renaming.","commonSituations":"Copying payload defaults from an OpenAI request where both were set; merging config dictionaries that each contribute one of the two keys; framework defaults that always emit both.","solutions":["Remove 'top_p' from the payload and keep only 'temperature'.","Alternatively remove 'temperature' and keep only 'top_p'.","Strip one of the keys in the caller before sending if your framework always sets both."],"exampleFix":"// before\npayload = {\"messages\": msgs, \"temperature\": 0.7, \"top_p\": 0.9}\n// after\npayload = {\"messages\": msgs, \"temperature\": 0.7}","handlingStrategy":"validation","validationCode":"def sanitize_anthropic_chat(payload):\n    if \"temperature\" in payload and \"top_p\" in payload:\n        payload.pop(\"top_p\", None)\n    return payload\nsanitize_anthropic_chat(payload)","typeGuard":null,"tryCatchPattern":"try:\n    resp = gateway.chat(endpoint=\"anthropic-chat\", payload=payload)\nexcept MlflowException as e:\n    if \"temperature\" in str(e) and \"top_p\" in str(e):\n        payload.pop(\"top_p\", None)\n        resp = gateway.chat(endpoint=\"anthropic-chat\", payload=payload)\n    else:\n        raise","preventionTips":["Choose one sampling strategy (temperature OR top_p) at config time.","Sanitize payloads before dispatching to Anthropic endpoints.","Audit shared payload templates for both keys."],"tags":["mlflow-gateway","anthropic","invalid-parameters","payload-validation"],"backgroundTag":"conflicting-parameters","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}