When creating custom middleware for LangSmith Deployment, you may encounter a ModuleNotFoundError: No module named 'fastapi' error when trying to import FastAPI in your middleware code. This occurs because LangSmith Deployment does not automatically include FastAPI as a dependency.
Solution
To resolve this issue, you must explicitly declare FastAPI as a dependency in your project's pyproject.toml file.
Add FastAPI to the dependencies section of your pyproject.toml:
[project]
dependencies = [
"fastapi",
# your other dependencies
]Or if you're using a specific version:
[project]
dependencies = [
"fastapi>=0.100.0",
# your other dependencies
]Why this happens
LangSmith Deployment operates with a minimal dependency set and does not automatically inject FastAPI or other web framework dependencies. This design allows for flexibility in choosing your preferred web framework and versions, but requires explicit declaration of any framework-specific dependencies your custom middleware needs.
After adding the dependency
Once you've added FastAPI to your dependencies:
Reinstall your project dependencies
Redeploy your LangSmith Deployment application
Your custom middleware should now be able to import FastAPI without errors
This same principle applies to any other external libraries your custom middleware may need - they must be explicitly declared in your project dependencies.