FastAPI - badly formed hexadecimal UUID string
Asked Answered
F

2

1

I'm using FastAPI & SQLModel to insert data into SQLite database (I'm posting a few fields, then SQLModel adds UUID and datetime fields and inserts into db).

While posting data to FastAPI, I occasionally get ValueError:

ValueError: badly formed hexadecimal UUID string

I don't think there's a problem with the data I'm sending as I'm not sending id field, so maybe I misconfigured my SQLModel?


Models:

# -*- coding: utf-8 -*-
import uuid
from typing import Optional
from datetime import datetime, date

from pydantic import UUID4, HttpUrl
from sqlmodel import Field, SQLModel


class EventBase(SQLModel):
    title: str
    text: str
    date: date
    URL: HttpUrl
    category: Optional[str] = Field(default=None) 


class Event(EventBase, table=True):
    id: UUID4 = Field(default_factory=uuid.uuid4, primary_key=True)
    created_at: datetime = Field(default_factory=datetime.utcnow)

Edit: decided to try this approach replacing UUID4 with uuid.UUID, but it didn't work as well:


Uvicorn debug:

INFO:     127.0.0.1:53312 - "POST /speeches/ HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "c:\github\speeches-api\env\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 373, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "c:\github\speeches-api\env\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
    return await self.app(scope, receive, send)
  File "c:\github\speeches-api\env\lib\site-packages\uvicorn\middleware\debug.py", line 96, in __call__
    raise exc from None
  File "c:\github\speeches-api\env\lib\site-packages\uvicorn\middleware\debug.py", line 93, in __call__
    await self.app(scope, receive, inner_send)
  File "c:\github\speeches-api\env\lib\site-packages\fastapi\applications.py", line 208, in __call__
    await super().__call__(scope, receive, send)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
    raise exc
  File "c:\github\speeches-api\env\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\exceptions.py", line 82, in __call__
    raise exc
  File "c:\github\speeches-api\env\lib\site-packages\starlette\exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\routing.py", line 61, in app
    response = await func(request)
  File "c:\github\speeches-api\env\lib\site-packages\fastapi\routing.py", line 226, in app
    raw_response = await run_endpoint_function(
  File "c:\github\speeches-api\env\lib\site-packages\fastapi\routing.py", line 161, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "c:\github\speeches-api\env\lib\site-packages\starlette\concurrency.py", line 39, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "c:\github\speeches-api\env\lib\site-packages\anyio\to_thread.py", line 28, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
  File "c:\github\speeches-api\env\lib\site-packages\anyio\_backends\_asyncio.py", line 818, in run_sync_in_worker_thread
    return await future
  File "c:\github\speeches-api\env\lib\site-packages\anyio\_backends\_asyncio.py", line 754, in run
    result = context.run(func, *args)
  File ".\app\main.py", line 29, in create_events
    session.refresh(event)
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\orm\session.py", line 2315, in refresh
    loading.load_on_ident(
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\orm\loading.py", line 407, in load_on_ident
    return load_on_pk_identity(
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\orm\loading.py", line 541, in load_on_pk_identity
    return result.one()
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\engine\result.py", line 1407, in one
    return self._only_one_row(
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\engine\result.py", line 558, in _only_one_row
    row = onerow(hard_close=True)
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\engine\result.py", line 1271, in _fetchone_impl
    return self._real_result._fetchone_impl(hard_close=hard_close)
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\engine\result.py", line 1674, in _fetchone_impl
    row = next(self.iterator, _NO_ROW)
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\orm\loading.py", line 147, in chunks
    fetch = cursor._raw_all_rows()
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\engine\result.py", line 392, in _raw_all_rows
    return [make_row(row) for row in rows]
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\engine\result.py", line 392, in <listcomp>
    return [make_row(row) for row in rows]
  File "c:\github\speeches-api\env\lib\site-packages\sqlalchemy\sql\type_api.py", line 1537, in process
    return process_value(value, dialect)
  File "c:\github\speeches-api\env\lib\site-packages\sqlmodel\sql\sqltypes.py", line 59, in process_result_value
    value = uuid.UUID(value)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\uuid.py", line 169, in __init__
    raise ValueError('badly formed hexadecimal UUID string')
ValueError: badly formed hexadecimal UUID string
Fairminded answered 10/12, 2021 at 11:17 Comment(0)
F
4

I learnt that the problem was described in this issue: data validation 'works except whenever the uuid.uuid4 default factory creates a UUID with a hexstring that starts with a zero.'

Proposed (temp) solution is posted below in the comments, by chriswhite199:

adjusted for my case:

def new_uuid() -> uuid.UUID:
    # Note: Work around UUIDs with leading zeros: https://github.com/tiangolo/sqlmodel/issues/25
    # by making sure uuid str does not start with a leading 0
    val = uuid.uuid4()
    while val.hex[0] == '0':
        val = uuid.uuid4()
    return val

class Event(EventBase, table=True):
    id: UUID4 = Field(default_factory=new_uuid, primary_key=True)
    created_at: datetime = Field(default_factory=datetime.utcnow)
Fairminded answered 14/12, 2021 at 15:11 Comment(0)
T
0

I think you need to do a str(uuid.uuid4()) in default factory

Tessellation answered 10/12, 2021 at 12:3 Comment(1)
that didn't work sadlyFairminded

© 2022 - 2024 — McMap. All rights reserved.