Unable to call Streaming Response from FastAPI in production
My StreamingResponse with FastAPI using Hypercorn works in development but not during production on Railway.
The deploy logs show a Prisma debug but stops mid way through the function with no error. On the frontend it Errors with 504 because it just Timesout.
Is there anything unique I should be aware of with Streaming Responses on Railway?
My API route I am calling is attached
Solution:Jump to solution
I figured it out. When disconnecting from Prisma Query Engine it would just freeze the server. I switched from using Hypercorn to Uvicorn and now it works!
26 Replies
Project ID:
272293fe-814d-4a92-9d85-82c242f56daa
this is just SSE right?
Yes its via an API call from a next.js server
no issues with SSE on railway - https://utilities.up.railway.app/sse
are you sending SSEs to a client's browser or? need a little more context here
Yes, sorry, I am sending it to a clients browser. They make an API call from the next.js backend to Railway for this 'gen_query'.
where does fastapi come into play with next and a clients browser
A call from next/api is sent to fastAPI via:
the whole route.ts is as follows:
for testing, cut out the nextjs app and call the public domain of the fastapi service
Okay will do. I have tested several different ways to make API calls but it seems once it hits one error or warning it stalls and I cant call it again... I thought it was a hypercorn thing maybe
this is no doubt a code or config issue, its just a question of where
What is the best way of logging on Railway during API calls?
json structured logs would be best
okay i'll try it out. thanks!
How come debugging in Deploy Logs is highlighted red with a level: "error" with really no other information besides this?
I get it that this means that its printing to stderr
are you doing json logging?
alot of it is print(). Should I use 'structlog' or is there a preference on Railway?
if you are just using print what other information would you expect to be printed beside your message?
I was just confused to why it 'errored' with printing to stderr.
The main problem is I am just struggling to work out how to debug this issue because all I get is a FUNCTION_INVOCATION_TIMEOUT when I make calls in production. In Development I get no errors come up and it works fine in development.
What would be the best way to debug this?
adding verbose debug logging, you are finding its hard to debug because you do not have the level of observability into your code that you need to.
Ok so I added
Which offers plenty of system info during deploy. Although the debugging log stops displaying once prisma is disconnected. After that nothing (There should be callbacks logged at this point). If I try to make any further requests no debugging is displayed at all.
are you making sure to log unbuffered?
How do I do that?
you would need to reference the loggers / python docs for that
Solution
I figured it out. When disconnecting from Prisma Query Engine it would just freeze the server. I switched from using Hypercorn to Uvicorn and now it works!
awesome, glad to hear it
thanks for the support
no problem!