Skip to content

Commit

Permalink
add client detail to streaming document (#98)
Browse files Browse the repository at this point in the history
* update docs

* update test
  • Loading branch information
aniketmaurya authored May 21, 2024
1 parent 9942f25 commit 120b50d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
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

0 comments on commit 120b50d

Please sign in to comment.