Integrating a Python endpoint in the t3 stack
Is it possible to integrate a Python endpoint in the t3 stack? I need a Python endpoint because the package that I wanna use is an AI one and currently unavailable in JS/TS.
18 Replies
couldnt you just run it as a separate server and then call it from within a trpc endpoint?
not the most efficent but itll get the job done if you want to leverage trpc
Write a GraphQL or OpenAPI Schema for it and make a request to it from your tRPC backend
This might be what you are looking for if using Vercel for hosting:
https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python
Vercel Documentation
Using the Python Runtime with Serverless Functions
Learn how to use the Python runtime to compile Python Serverless Functions on Vercel.
Yes, I can host a separate api for that endpoint but ideally I would like to keep everything in the same monorepo. Is it possible? Has anyone done it? I read online that some people got it working by putting the *.py files in /api (not /pages/api)
I tried and couldnt get it working
I was able to get this code to run locally
It basically calls a python file located next to itself
but it fails to run on vercel
I suspect the reason is that vercel doesnt have python installed
I was wondering if it's possble to install all nextjs dependencies and pythons?
Take a look again at the link above: https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/python
Vercel Documentation
Using the Python Runtime with Serverless Functions
Learn how to use the Python runtime to compile Python Serverless Functions on Vercel.
I did
The page is answering your question: Yes its possible, yes you can install packages (see bottom of the page requirements.txt)
Im asking if it's possible to install both nextJS and python
together under pages/api
Ah, maybe not. In their example repo it looks like this: https://github.com/vercel/examples/tree/main/python/hello-world
GitHub
examples/python/hello-world at main · vercel/examples
Enjoy our curated collection of examples and solutions. Use these patterns to build your own robust and scalable applications. - examples/python/hello-world at main · vercel/examples
So you might need another deployment.
Though based on this, its possible: https://stackoverflow.com/questions/63652016/python-serverless-function-vercel-next-js
Stack Overflow
Python Serverless Function Vercel - Next.js
I found out that I could use Python to create a serverless function inside a Next.js project. Once deployed to Vercel, it will get converted into a serverless function.
I went through the docs and ...
That's what I did and failed haha
.
Deployment failed or getting error hitting the endpoint?
I'll test on one of my projects to see if I can get it to work
Found some more info here:
https://github.com/vercel/vercel/discussions/6197
https://github.com/vercel/vercel/discussions/4023
Looks like while it is possible to mix & match, it's not as straight forward.
You have to move the api files to the root of your project
/api
and it might cause a few issues with 3rd party packages that are expecting pages/api
to exist.I read those two discussions previously too. Didn't help. Were you able to get it working?
No, can't move my
/pages/api
folder in the project I was gonna test since I have some third party packages I didn't want to mess with.
I might try later tonight on a fresh T3 and see if I can get it to work. I'll let you know what I learn.Thanks. Appreciate it