python data_error ai_generated true

fastapi.exceptions.RequestValidationError: path -> id value is not a valid uuid (type=type_error.uuid)

ID: python/fastapi-path-param-422-uuid

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-07-08First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

The path parameter is declared as UUID but the URL segment is not a valid UUID string.

generic

中文

路径参数声明为 UUID,但 URL 片段不是有效的 UUID 字符串。

Workarounds

  1. 90% success
    @app.get('/items/{item_id}')
    def get(item_id: str):
        try: uid = UUID(item_id)
        except ValueError: raise HTTPException(400)
  2. 88% success
    from fastapi import Path
    def get(item_id: UUID = Path(...)): ...

Dead Ends

Common approaches that don't work:

  1. 75% fail

    Still fails if the regex matches but the value is not a UUID.

  2. 90% fail

    Validation runs before the handler; except never triggers.