I wrote a Python API using FastAPI and deployed to Vercel and it just kept failing with error “This Serverless Function has crashed†no matter what i tried. I am new to Python APIs and thought the problem was my code, so I kept looking up different ways people are achieving FastAPI on Vercel.
Eventually I dropped back to building a Hello World api and after that was working I gradually added things back in until it wasn’t.
It was the install of typing
that caused it! I wasn’t even using it in this Hello World app. Who would have thunk! Once I removed that from the requirements.txt it “Hello World’d†just fine.
I cannot see any posts on this problem. It surely isn’t just me, giving that typing seems to be core to Pydantic which from what I understand is part of the reason for using FastAPI. I’m newish to all this so I might be wrong.
Here is my reproduction. I will investigate alternatives and I’d appreciate if someone could suggest a way around this.
Also of note - there is no log file that shows any problem. It seems to build just fine. I guess it happens when it is wrapped and sent to AWS Lamba (that how it happens, right?). Is there any way to see what happens to the code that is sent to Lambda? Is there any way to see what command is being run?
$ cat vercel.json
{
"version": 2,
"builds": [
{
"src": "app/main.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "app/main.py"
}
]
}
$ cat app/main.py
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def read_root():
return {"message": "Hello, World!"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
$ cat requirements.txt
fastapi
uvicorn
typing
I wrote that locally and vercel --prod
and saw the runtime error.
Remove “typingâ€, vercel --prod
again and it starts up just fine.
OK, there are errors in the runtime log. This is repeated over and over:
Traceback (most recent call last):
File "/var/task/vc__handler__python.py", line 14, in <module>
__vc_spec.loader.exec_module(__vc_module)
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/var/task/app/main.py", line 1, in <module>
from fastapi import FastAPI
File "/var/task/fastapi/__init__.py", line 7, in <module>
from .applications import FastAPI as FastAPI
File "/var/task/fastapi/applications.py", line 2, in <module>
from typing import (
File "/var/task/typing.py", line 1359, in <module>
class Callable(extra=collections_abc.Callable, metaclass=CallableMeta):
File "/var/task/typing.py", line 1007, in __new__
self._abc_registry = extra._abc_registry
^^^^^^^^^^^^^^^^^^^
AttributeError: type object 'Callable' has no attribute '_abc_registry'
Python process exited with exit status: 1. The logs above can help with debugging the issue.
2 posts - 1 participant