FastAPI on Vercel Function: ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'

I’m deploying a development branch as preview with a FastAPI backend located at src/api/index.py. When hitting the api/auth/me route, I’m getting a ModuleNotFoundError: No module named 'pydantic_core._pydantic_core
The code in src/api/index.py has changed very little compared to the current production deployment (only a few lines were added). The requirements in src/api/pyproject.toml are identical to what’s deployed in production with pinned versions I know are present in the current production version.
The build completes successfully and the Python dependencies install without errors, but at runtime in the Vercel Function Lambda environment, pydantic_core cannot be found. I suspect this issue is due to a changed way in which Vercel bundles Python functions in the Lambda environment.

The current behaviour is the one described, the expected one is for the backend to run smoothly since the requirements did not change.

In production pyproject.toml

[project]
name = "my-prj-api"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "boto3>=1.40.61",
    "fastapi>=0.120.0",
    "pydantic>=2.12.3",
    "python-dotenv>=1.1.1",
    "requests>=2.32.5",
    "supabase>=2.22.2",
    "uvicorn[standard]>=0.38.0",
]
[dependency-groups]
dev = [
    "pytest>=8.4.2",
    "ruff>=0.14.6",
    "mypy>=1.18.2",
    "boto3-stubs>=1.41.4",
    "types-requests>=2.32.4.20250913",
]

development branch pyproject.toml with pinned versions as per the ones in the last production deployment

[project]
name = "timely-api"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.11"
dependencies = [
    "boto3>=1.40.61",
    "fastapi==0.120.1",
    "pydantic==2.12.3",
    "pydantic-settings==2.11.0",
    "pydantic-core==2.41.4",
    "python-dotenv>=1.1.1",
    "requests>=2.32.5",
    "supabase>=2.22.2",
    "uvicorn[standard]>=0.38.0",
]
[dependency-groups]
dev = [
    "pytest>=8.4.2",
    "ruff>=0.14.6",
    "mypy>=1.18.2",
    "boto3-stubs>=1.41.4",
    "types-requests>=2.32.4.20250913",
]

Anyone experiencing the same?

1 Like

Hi, Do you have .python-version in your project? Could you temporarily enable source code visibility so we can take a look? https://vercel.com/kb/guide/how-to-allow-the-vercel-support-team-to-access-your-deployment-source-code

1 Like

I’m experiencing the exact same issue when running FastAPI with pydantic on Vercel Functions.

I think it’s because pydantic_core is actually built with native code (Rust in this case) ….see this issue from pydantic No module named 'pydantic_core._pydantic_core' using AWS Lambdas · Issue #6557 · pydantic/pydantic · GitHub.

It looks like Vercel’s Python runtime is using an x86_64 architecture, so I tried installing the package directly using the following command, but still no luck. See these docs from AWS: Working with .zip file archives for Python Lambda functions - AWS Lambda

I ran this command in the build via:

[tool.verson.scripts]
pip install -r requirements.txt && pip install --platform manylinux2014_x86_64 --target package --implementation cp --python-version 3.12 --only-binary=:all: --upgrade pydantic

The build succeeds, but navigating to the index of the app fails with the same error as OP.
Help here would be appreciated!

Hi @alexkan,

Could you enable source code visibility https://vercel.com/kb/guide/how-to-allow-the-vercel-support-team-to-access-your-deployment-source-code and share us the project?

I can confirm that removing `.python-version` from the folder where the backend lives solves the problem.