Implementation (Advanced)
Custom Responses
Get more control by directly creating the response object.
By default, Truss wraps prediction results into an HTTP response. For advanced use cases, you can create response objects manually to:
- Control HTTP status codes.
- Use server-sent events (SSEs) for streaming responses.
You can return a response from predict or postprocess, but not both.
Returning Custom Response Objects
Any subclass of starlette.responses.Response is supported.
If
predict
returns a response, postprocess
cannot be used.Example: Streaming with SSEs
For server-sent events (SSEs), use StreamingResponse
:
Limitations
- Response headers are not fully propagated – include metadata in the response body.
Also see Using Request Objects for handling raw requests.
Was this page helpful?