I am working on a Next.js project with a Python Flask API that I’m deploying to Vercel. My deployments are consistently failing with the error:
Error: A Serverless Function has exceeded the unzipped maximum size of 250 MB.
I believe this indicates that one or more of my Python serverless functions (API routes) is exceeding Vercel’s unzipped size limit.
- Current Behavior: The Vercel build finishes collecting page data, but then fails during the “Deploying outputs” phase with the
unzipped maximum size of 250 MB
error. I cannot currently identify which specific function is causing this, or what is contributing most to its size.
Troubleshooting Steps & Context
Local Bundle Size Check Script: I’ve implemented a local Node.js script (scripts/check-bundle-size.js
) that runs du -sk
(Linux/macOS) or PowerShell’s Get-ChildItem | Measure-Object -Sum
(Windows) on the .vercel/output/functions
directory after next build
.
- Problem: When I run
npm run build
(which executesnext build && node scripts/check-bundle-size.js
), my script reports “No functions directory found. Skipping bundle size check.” This indicates thatfunctions
directory is not yet populated by Vercel during thenext build
phase, so my script is not effective in catching the oversized function during a Vercel cloud build.
- Missing Specific Function Name in Logs: The Vercel build logs, as shown in the snippets below, indicate the general 250MB error, but they do not specify the name of the function (e.g.,
api_my_specific_function.func
) that is oversized, which makes targeted debugging difficult.
requirements.txt
(example, likely the source of the bloat): (I suspect boto3
and its transitive dependencies might be a significant contributor, but I can’t confirm without knowing which function is oversized.)
`Flask==3.0.3
Flask-Cors==3.0.10
Flask-RESTful==0.3.9
PyJWT==2.10.1
boto3==1.35.90
python-dotenv==1.0.1
- How can I get more detailed logs from Vercel that specifically tell me the name and exact unzipped size of the serverless function(s) exceeding the 250 MB limit? The current general error message is not specific enough for debugging.
- Once I identify the specific function, what are the most effective strategies to diagnose which part of its code or which specific dependencies are causing the excessive bundle size? (I know about
du -sh
but need to figure out the root cause for Vercel’s packaging). - Are there any best practices for Python/Next.js on Vercel to preemptively avoid such large function bundles, especially with common libraries like
boto3
?
Any help or guidance would be greatly appreciated! Thanks in advance.