1515from __future__ import annotations
1616
1717import json
18- from typing import Any , Optional
18+ from typing import Optional
1919
2020from openai import AsyncStream , Stream
21+ from openai .types .chat import ChatCompletionChunk
2122
2223from opentelemetry .semconv ._incubating .attributes import (
2324 openai_attributes as OpenAIAttributes ,
@@ -41,35 +42,35 @@ class _ChatStreamMixin:
4142
4243 _self_invocation : InferenceInvocation
4344 _self_capture_content : bool
44- _self_choice_buffers : list
45+ _self_choice_buffers : list [ ChoiceBuffer ]
4546 _self_response_id : Optional [str ]
4647 _self_response_model : Optional [str ]
4748 _self_service_tier : Optional [str ]
4849 _self_prompt_tokens : Optional [int ]
4950 _self_completion_tokens : Optional [int ]
5051
51- def _set_response_model (self , chunk ) :
52+ def _set_response_model (self , chunk : ChatCompletionChunk ) -> None :
5253 if self ._self_response_model :
5354 return
5455
5556 if chunk .model :
5657 self ._self_response_model = chunk .model
5758
58- def _set_response_id (self , chunk ) :
59+ def _set_response_id (self , chunk : ChatCompletionChunk ) -> None :
5960 if self ._self_response_id :
6061 return
6162
6263 if chunk .id :
6364 self ._self_response_id = chunk .id
6465
65- def _set_response_service_tier (self , chunk ) :
66+ def _set_response_service_tier (self , chunk : ChatCompletionChunk ) -> None :
6667 if self ._self_service_tier :
6768 return
6869
6970 if chunk .service_tier :
7071 self ._self_service_tier = chunk .service_tier
7172
72- def _build_streaming_response (self , chunk ) :
73+ def _build_streaming_response (self , chunk : ChatCompletionChunk ) -> None :
7374 if chunk .choices is None :
7475 return
7576
@@ -96,19 +97,19 @@ def _build_streaming_response(self, chunk):
9697 tool_call
9798 )
9899
99- def _set_usage (self , chunk ) :
100+ def _set_usage (self , chunk : ChatCompletionChunk ) -> None :
100101 if chunk .usage :
101102 self ._self_completion_tokens = chunk .usage .completion_tokens
102103 self ._self_prompt_tokens = chunk .usage .prompt_tokens
103104
104- def _process_chunk (self , chunk ) :
105+ def _process_chunk (self , chunk : ChatCompletionChunk ) -> None :
105106 self ._set_response_id (chunk )
106107 self ._set_response_model (chunk )
107108 self ._set_response_service_tier (chunk )
108109 self ._build_streaming_response (chunk )
109110 self ._set_usage (chunk )
110111
111- def _set_output_messages (self ):
112+ def _set_output_messages (self ) -> None :
112113 if not self ._self_capture_content : # optimization
113114 return
114115 output_messages = []
@@ -149,7 +150,7 @@ def _on_stream_end(self) -> None:
149150 def _on_stream_error (self , error : BaseException ) -> None :
150151 self ._cleanup (error )
151152
152- def parse (self ):
153+ def parse (self ) -> _ChatStreamMixin :
153154 """Called when using with_raw_response with stream=True."""
154155 return self
155156
@@ -182,14 +183,14 @@ def _cleanup(self, error: Optional[BaseException] = None) -> None:
182183
183184class ChatStreamWrapper (
184185 _ChatStreamMixin ,
185- SyncStreamWrapper [Any ],
186+ SyncStreamWrapper [ChatCompletionChunk ],
186187):
187188 def __init__ (
188189 self ,
189- stream : Stream ,
190+ stream : Stream [ ChatCompletionChunk ] ,
190191 invocation : InferenceInvocation ,
191192 capture_content : bool ,
192- ):
193+ ) -> None :
193194 super ().__init__ (stream )
194195 self ._self_invocation = invocation
195196 self ._self_choice_buffers = []
@@ -203,14 +204,14 @@ def __init__(
203204
204205class AsyncChatStreamWrapper (
205206 _ChatStreamMixin ,
206- AsyncStreamWrapper [Any ],
207+ AsyncStreamWrapper [ChatCompletionChunk ],
207208):
208209 def __init__ (
209210 self ,
210- stream : AsyncStream ,
211+ stream : AsyncStream [ ChatCompletionChunk ] ,
211212 invocation : InferenceInvocation ,
212213 capture_content : bool ,
213- ):
214+ ) -> None :
214215 super ().__init__ (stream )
215216 self ._self_invocation = invocation
216217 self ._self_choice_buffers = []
0 commit comments