Streaming¶
Info
✨ New in v0.4.0
OpenAI uses server-sent events (SSE) for streaming. Those types of responses are slightly different than standard HTTP responses.
The content in the response is an iterable stream of data. Decoded, it would look like this:
event: thread.run.created
data: {"id":"run_123","object":"thread.run","created_at":1710330640,"assistant_id":"asst_123","thread_id":"thread_123","status":"queued","started_at":null,"expires_at":1710331240,"cancelled_at":null,"failed_at":null,"completed_at":null,"required_action":null,"last_error":null,"model":"gpt-4-turbo","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":null,"response_format":"auto","tool_choice":"auto"}}
...
event: thread.run.completed
data: {"id":"run_123","object":"thread.run","created_at":1710330640,"assistant_id":"asst_123","thread_id":"thread_123","status":"completed","started_at":1710330641,"expires_at":null,"cancelled_at":null,"failed_at":null,"completed_at":1710330642,"required_action":null,"last_error":null,"model":"gpt-4-turbo","instructions":null,"tools":[],"metadata":{},"temperature":1.0,"top_p":1.0,"max_completion_tokens":null,"max_prompt_tokens":null,"truncation_strategy":{"type":"auto","last_messages":null},"incomplete_details":null,"usage":{"prompt_tokens":20,"completion_tokens":11,"total_tokens":31},"response_format":"auto","tool_choice":"auto"}}
event: done
data: [DONE]
To mock that, an EventStream
and AsyncEventStream
classes are provided.
These classes are meant to be inherited and built on top of. The main part that you would need to worry about is overriding the generate
method. This method yields Event
objects which can be encoded as SSE events and the resulting stream will look like the example above.
The stream object can be passed in to an HTTPX response as an argument for content
. To do this, you'll need to also make use of function responses.
Info
The classes are generic so you must provide the type of the event
Example¶
Here's an example using chat completions API.
- Your stream class must inherit from either
EventStream
orAsyncEventStream
and you must provide the event type - Override the
generate
method - Yield an event. This event type must match the type provided.
- Construct the stream object
- Pass the stream as
content
on the response object
More examples can be found in the examples directory in the repo: