{"id":"47b459a384d476fe","repo":"tiangolo/fastapi","slug":"e-errors-include-url-false","errorCode":null,"errorMessage":"e.errors(include_url=False)","messagePattern":"e\\.errors\\(include_url=False\\)","errorType":"http","errorClass":"HTTPException","httpStatus":422,"severity":"error","filePath":"docs_src/path_operation_advanced_configuration/tutorial007_py310.py","lineNumber":31,"sourceCode":"@app.post(\n    \"/items/\",\n    openapi_extra={\n        \"requestBody\": {\n            \"content\": {\"application/x-yaml\": {\"schema\": Item.model_json_schema()}},\n            \"required\": True,\n        },\n    },\n)\nasync def create_item(request: Request):\n    raw_body = await request.body()\n    try:\n        data = yaml.safe_load(raw_body)\n    except yaml.YAMLError:\n        raise HTTPException(status_code=422, detail=\"Invalid YAML\")\n    try:\n        item = Item.model_validate(data)\n    except ValidationError as e:\n        raise HTTPException(status_code=422, detail=e.errors(include_url=False))\n    return item\n","sourceCodeStart":13,"sourceCodeEnd":33,"githubUrl":"https://github.com/tiangolo/fastapi/blob/42a41db11f6882807ac3c057b942178d53b97438/docs_src/path_operation_advanced_configuration/tutorial007_py310.py#L13-L33","documentation":"This line fires when a YAML request body fails Pydantic validation against the `Item` model. `ValidationError.errors(include_url=False)` returns a list of per-field error dicts (with the Pydantic docs URL stripped) that FastAPI serializes into the 422 response `detail`. The `include_url` keyword is Pydantic v2 only; on Pydantic v1 this exact call raises `TypeError: errors() got an unexpected keyword argument 'include_url'`.","triggerScenarios":"`POST /items/` with `Content-Type: application/x-yaml` and a body missing the required `tags` field, with `tags` set to a non-list, or with valid YAML that parses to `None`/wrong types.","commonSituations":"Migrating Pydantic v1 -> v2 (the kwarg did not exist in v1); clients sending malformed YAML or JSON-as-YAML; forgetting the `application/x-yaml` content type so the custom `request.body()` parse path is taken; Pydantic version pin mismatch between dev and CI.","solutions":["Ensure the YAML body supplies all required `Item` fields (`name: str`, `tags: list[str]`) before sending.","If you are on Pydantic v1, drop `include_url=False` or upgrade to Pydantic v2 (`pip install -U pydantic`).","Build the body from a validated model instance (`yaml.dump(item.model_dump())`) instead of hand-writing YAML.","Return a simpler `detail` (e.g. `detail=str(e)`) if you do not want the full structured error list."],"exampleFix":"# before\nraise HTTPException(status_code=422, detail=e.errors(include_url=False))\n# after (Pydantic v1 compatible)\nraise HTTPException(status_code=422, detail=e.errors())","handlingStrategy":"validation","validationCode":"import yaml\nALLOWED = {\"name\": str, \"tags\": list}\ndef safe_yaml_payload(raw: bytes) -> dict:\n    data = yaml.safe_load(raw) or {}\n    assert isinstance(data, dict), \"YAML must parse to a mapping\"\n    for k, t in ALLOWED.items():\n        assert isinstance(data.get(k), t), f\"{k} must be {t.__name__}\"\n    assert all(isinstance(x, str) for x in data[\"tags\"]), \"tags must be list[str]\"\n    return data","typeGuard":"def is_item_dict(data: object) -> bool:\n    return (isinstance(data, dict)\n            and isinstance(data.get(\"name\"), str)\n            and isinstance(data.get(\"tags\"), list)\n            and all(isinstance(t, str) for t in data[\"tags\"]))","tryCatchPattern":"try:\n    resp = client.post(\"/items/\", content=yaml.dump(payload),\n                       headers={\"content-type\": \"application/x-yaml\"})\n    resp.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code == 422:\n        log.error(\"validation errors: %s\", e.response.json()[\"detail\"])\n    raise","preventionTips":["Generate the YAML body from a validated model instance, not by hand.","Pin Pydantic to v2 when using `include_url=False`.","Always send `Content-Type: application/x-yaml` so the custom parser runs."],"tags":["pydantic","validation","yaml","fastapi","pydantic-v2"],"analyzedSha":"42a41db11f6882807ac3c057b942178d53b97438","analyzedAt":"2026-08-04T19:23:32.007Z","schemaVersion":2}