🐛 Bug Report: POST /stream returns HTTP 400 for unhandled exceptions instead of HTTP 500
📜 Description
The POST /stream endpoint returns HTTP 400 when an unhandled exception occurs, incorrectly classifying server-side failures as client errors.
In docsgpt/api/answer/routes/stream.py (around lines 252–261), the catch-all exception handler returns status code 400. However, the non-streaming endpoint in docsgpt/api/answer/routes/answer.py (around lines 234–239) correctly returns HTTP 500 for unexpected exceptions.
This inconsistency can cause monitoring systems and API clients to misinterpret internal server failures as invalid client requests.
👟 Reproduction steps
- Set up and run DocsGPT according to the project's setup instructions.
- Send a valid request to the
POST /streamendpoint. - Trigger an unexpected server-side exception during request processing, for example, by mocking an internal dependency to raise an exception in a test environment.
- Observe the HTTP status code returned by the endpoint.
- Compare the behavior with the non-streaming
/api/answerendpoint under equivalent failure conditions.
Observed: The streaming endpoint returns HTTP 400 for an unhandled exception.
Note: The exact request payload and failure conditions depend on the exception being triggered.
👍 Expected behavior
Unhandled server-side exceptions in POST /stream should return HTTP 500 (Internal Server Error), consistent with the non-streaming /api/answer endpoint.
Client errors should continue to return appropriate 4xx status codes.
👎 Actual Behavior with Screenshots
The catch-all exception handler in docsgpt/api/answer/routes/stream.py returns HTTP 400 when an unexpected exception occurs.
This incorrectly indicates that the client sent a bad request, even when the failure originates from the server.
No screenshot is attached because this issue concerns backend API response handling and can be verified through the HTTP response status code.
💻 Operating system
Linux
What browsers are you seeing the problem on?
No response
🤖 What development environment are you experiencing this bug on?
Docker
🔒 Did you set the correct environment variables in the right path? List the environment variable names (not values please!)
No response
📃 Provide any additional context for the Bug.
The issue appears to be a one-line fix in the catch-all exception handler in docsgpt/api/answer/routes/stream.py.
The proposed change is to return HTTP 500 for unexpected exceptions while preserving the existing error response format and logging.
A regression test should verify that unhandled exceptions return HTTP 500 and that existing client-error handling remains unchanged.
📖 Relevant log output
👀 Have you spent some time to check if this bug has been raised before?
- [x] I checked and didn't find similar issue
🔗 Are you willing to submit PR?
Yes, I am willing to submit a PR!
🧑⚖️ Code of Conduct
- [x] I agree to follow this project's Code of Conduct
@yashascoding, you're assigned.
- Open a PR or post progress within 14 days; otherwise the issue goes back to others.
- Read
CONTRIBUTING.md. Add a regression test for an unexpected exception before the SSE response starts, and verify existing 4xx handling stays unchanged. Once response headers are sent, the HTTP status cannot change. - Include a screenshot or short recording showing the HTTP response status.
Automated triage. A maintainer will follow up.
@arc53-machine Could you please assign this issue to me ?
@yashascoding You're already assigned to this issue; no assignment change is needed.
Automated triage. A maintainer will follow up.
ok..thanks