Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add client detail to streaming document #98

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,6 @@ as a generator (a Python function that yields output).
For example, streaming long responses generated over time:

```python
import json
import litserve as ls

class SimpleStreamAPI(ls.LitAPI):
Expand All @@ -472,7 +471,7 @@ class SimpleStreamAPI(ls.LitAPI):

def encode_response(self, output):
for out in output:
yield json.dumps({"output": out})
yield {"output": out}


if __name__ == "__main__":
Expand All @@ -481,6 +480,19 @@ if __name__ == "__main__":
server.run(port=8000)
```

To consume a streaming response, your client should iterate through the response content as follows, otherwise
your response content would be concatenated as a byte string.

```python
import requests

url = "http://127.0.0.1:8000/predict"
resp = requests.post(url, json={"input": 1.0}, headers=None, stream=True)
for line in resp.iter_content(5000):
if line:
print(line.decode("utf-8"))
```

 

</details>
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/default_batched_streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
import numpy as np

import json
import litserve as ls


Expand All @@ -30,7 +29,7 @@ def predict(self, x):

def encode_response(self, output_stream):
for outputs in output_stream:
yield [json.dumps({"output": output}) for output in outputs]
yield [{"output": output} for output in outputs]


if __name__ == "__main__":
Expand Down
Loading