{"record":{"id":"a777c3a2587dae37","repo":"huggingface/transformers","slug":"unexpected-fields-in-the-request-unexpected-a777c3","errorCode":null,"errorMessage":"Unexpected fields in the request: {unexpected}","messagePattern":"Unexpected fields in the request: (.+?)","errorType":"http","errorClass":"HTTPException","httpStatus":422,"severity":"error","filePath":"src/transformers/cli/serving/utils.py","lineNumber":1092,"sourceCode":"    def __init__(\n        self,\n        model_manager: \"ModelManager\",\n        generation_state: GenerationState,\n        chat_template_kwargs: dict | None = None,\n    ):\n        self.model_manager = model_manager\n        self.generation_state = generation_state\n        self.chat_template_kwargs = chat_template_kwargs or {}\n\n    def _validate_request(self, body: dict) -> None:\n        \"\"\"Validate request fields against the handler's params class and unused fields.\"\"\"\n        from fastapi import HTTPException\n\n        input_keys = set(body.keys())\n        if self._valid_params_class is not None:\n            unexpected = input_keys - getattr(self._valid_params_class, \"__mutable_keys__\", set())\n            if unexpected:\n                raise HTTPException(status_code=422, detail=f\"Unexpected fields in the request: {unexpected}\")\n        unused = input_keys & self._unused_fields\n        if unused:\n            logger.warning_once(f\"Ignoring unsupported fields in the request: {unused}\")\n\n    @staticmethod\n    def chunk_to_sse(chunk: \"str | pydantic.BaseModel\") -> str:\n        \"\"\"Format a pydantic model or string as an SSE ``data:`` line.\"\"\"\n        if isinstance(chunk, str):\n            return chunk if chunk.startswith(\"data: \") else f\"data: {chunk}\\n\\n\"\n        return f\"data: {chunk.model_dump_json(exclude_none=True)}\\n\\n\"\n\n    def _resolve_model(self, body: dict) -> tuple[str, \"PreTrainedModel\", \"ProcessorMixin | PreTrainedTokenizerFast\"]:\n        \"\"\"Apply force_model, load model + processor.\n\n        Returns ``(model_id, model, processor)``.\n        \"\"\"\n        from fastapi import HTTPException\n","sourceCodeStart":1074,"sourceCodeEnd":1110,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/cli/serving/utils.py#L1074-L1110","documentation":"HTTP 422 from the generic request validator shared by the JSON-body handlers (chat/completions style endpoints). It subtracts the request body's keys from the handler's params class __mutable_keys__ and rejects any key the params class does not declare. Recognized-but-unsupported fields only log a warning; unknown fields fail.","triggerScenarios":"POST to a chat/completions-style endpoint with body keys outside the handler's params class — e.g. custom params like 'top_k' where unsupported, 'user_id', or misspelled names like 'temperatur'.","commonSituations":"Clients written against the OpenAI API sending extensions this server does not define; version skew where a field was added or renamed between transformers releases; SDK defaults injecting metadata fields.","solutions":["Drop the fields named in the error detail from the request body","Print the handler's params class __mutable_keys__ to see the accepted schema for your transformers version","If you control both sides, add the field to the params class instead of sending unknown keys"],"exampleFix":"// before\nbody = {model: 'gpt2', messages, user_id: 'abc'}\n// after\nbody = {model: 'gpt2', messages}","handlingStrategy":"validation","validationCode":"allowed = set(ParamsClass.__mutable_keys__)  # the handler's params class\nclean = {k: v for k, v in body.items() if k in allowed}\nbody = clean","typeGuard":null,"tryCatchPattern":"if resp.status_code == 422 and 'Unexpected fields' in resp.text:\n    unexpected = set(re.findall(r\"'([^']+)'\", resp.json()['detail']))\n    body = {k: v for k, v in body.items() if k not in unexpected}\n    resp = client.post('/v1/chat/completions', json=body)","preventionTips":["Filter request bodies against the params class before sending","Pin the transformers version on both client and server so the accepted schema is stable"],"tags":["api","serving","validation","http-422"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}