langserve icon indicating copy to clipboard operation
langserve copied to clipboard

support: yield `sse_starlette.ServerSentEvent` in `/stream`

Open itok01 opened this issue 1 year ago • 2 comments

Allows the /stream endpoint to return sse_starlette.ServerSentEvent from runnable, allowing you to return custom events such as event: error.

The reason for not using langserve.server_sent_events.ServerSentEvent is that is unbable to check with isinstance() whether an object is a langserve.server_sent_events.ServerSentEvent.

Example

Run ./examples/custom_events/server.py

/stream:

$ curl -X POST -d '{"input":{},"config":{}}' 'http://127.0.0.1:8000/stream'
event: metadata
data: {"run_id": "40239ec7-e492-4c26-aac7-4380df1ad050"}

event: data
data: {"message":"Message 0"}

event: data
data: {"message":"Message 1"}

event: data
data: {"message":"Message 2"}

event: data
data: {"message":"Message 3"}

event: error
data: {"message":"An error occurred"}

event: end

/invoke:

$ curl -X POST -d '{"input":{},"config":{}}' 'http://127.0.0.1:8000/invoke'
{"output":{"message":"An error occurred"},"metadata":{"run_id":"06078459-1945-46d0-97ec-d7bec22f408a","feedback_tokens":[]}}

itok01 avatar Jul 03 '24 06:07 itok01

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have the username @itok01 on file. In order for us to review and merge your code, please complete the Individual Contributor License Agreement here https://forms.gle/AQFbtkWRoHXUgipM6 .

This process is done manually on our side, so after signing the form one of the maintainers will add you to the contributors list.

For more details about why we have a CLA and other contribution guidelines please see: https://github.com/langchain-ai/langserve/blob/main/CONTRIBUTING.md.

cla-bot[bot] avatar Jul 03 '24 06:07 cla-bot[bot]

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have the username @itok01 on file. In order for us to review and merge your code, please complete the Individual Contributor License Agreement here https://forms.gle/AQFbtkWRoHXUgipM6 .

This process is done manually on our side, so after signing the form one of the maintainers will add you to the contributors list.

For more details about why we have a CLA and other contribution guidelines please see: https://github.com/langchain-ai/langserve/blob/main/CONTRIBUTING.md.

cla-bot[bot] avatar Jul 04 '24 11:07 cla-bot[bot]