Here's my python fastapi code:
PDF_MIME_TYPE = "application/pdf"@api_router.get( "/download_pdf", response_class=StreamingResponse, responses={200: {"content": {PDF_MIME_TYPE: {}}}},)async def download_pdf(): try: markdown = await get_answers() pdf_stream = BytesIO() pdf_report(markdown, pdf_stream) headers = {"Content-Disposition": 'attachment; filename="out.pdf"'} pdf_stream.seek(0) return StreamingResponse(pdf_stream, headers=headers, media_type=PDF_MIME_TYPE) except Exception as e: logging.exception("Error downloading PDF") return f"error: {e}"app.include_router(api_router)It's working very well in my local environment. But when I deployed on Vercel, it often shows: RuntimeError: Event loop is closed, just when I access my link a little fast.
Here's the error log:
INFO:httpx:HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent "HTTP/1.1 200 OK"INFO:httpx:HTTP Request: POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent "HTTP/1.1 200 OK"127.0.0.1 - - [18/Dec/2024 10:14:25] "GET /api/download_pdf?path=download_pdf HTTP/1.1" 200 -it wrap_app_handling_exceptions(app, request)(scope, receive, send)File "/var/task/starlette/_exception_handler.py", line 53, in wrapped_appraise excFile "/var/task/starlette/_exception_handler.py", line 42, in wrapped_appawait app(scope, receive, sender)File "/var/task/starlette/routing.py", line 73, in appresponse = await f(request)^^^^^^^^^^^^^^^^File "/var/task/fastapi/routing.py", line 301, in appraw_response = await run_endpoint_function(^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/var/task/fastapi/routing.py", line 212, in run_endpoint_functionreturn await dependant.call(**values)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/var/task/api/index.py", line 22, in download_pdfmarkdown = await get_answers()^^^^^^^^^^^^^^^^^^^File "/var/task/api/deep_thinking.py", line 171, in get_answersquestions = (await reader.run(article)).data^^^^^^^^^^^^^^^^^^^^^^^^^File "/var/task/pydantic_ai/agent.py", line 251, in runmodel_response, request_cost = await agent_model.request(messages, model_settings)^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/var/task/pydantic_ai/models/gemini.py", line 176, in requestasync with self._make_request(messages, False, model_settings) as http_response:^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^File "/var/lang/lib/python3.12/contextlib.py", line 210, in __aenter__I guess this might be the same issue as: https://github.com/vercel/vercel/issues/8477 but I'm not sure.
Anyone can help?
Thanks.
