From 5c2e2445d3d174e46fdf3b0b2016505faef744d5 Mon Sep 17 00:00:00 2001 From: Julian Risch Date: Fri, 30 May 2025 11:21:10 +0200 Subject: [PATCH] fix: fix mypy issue in OpenAIChatGenerator that appears since mypy 1.16 (#9456) * fix mypy issue with 1.16 * add release note --- haystack/components/generators/chat/openai.py | 16 ++++++++++++++-- .../notes/fix-mypy-1-16-8ed5d200c9e2018f.yaml | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/fix-mypy-1-16-8ed5d200c9e2018f.yaml diff --git a/haystack/components/generators/chat/openai.py b/haystack/components/generators/chat/openai.py index 8f0d99cad..d5c80cdf9 100644 --- a/haystack/components/generators/chat/openai.py +++ b/haystack/components/generators/chat/openai.py @@ -421,13 +421,19 @@ class OpenAIChatGenerator: chunks: List[StreamingChunk] = [] chunk = None chunk_delta: StreamingChunk + last_chunk: Optional[ChatCompletionChunk] = None for chunk in chat_completion: # pylint: disable=not-an-iterable assert len(chunk.choices) <= 1, "Streaming responses should have at most one choice." chunk_delta = self._convert_chat_completion_chunk_to_streaming_chunk(chunk) chunks.append(chunk_delta) callback(chunk_delta) - return [self._convert_streaming_chunks_to_chat_message(chunk, chunks)] + last_chunk = chunk + + if not last_chunk: + raise ValueError("No chunks received from the stream") + + return [self._convert_streaming_chunks_to_chat_message(last_chunk, chunks)] async def _handle_async_stream_response( self, chat_completion: AsyncStream, callback: AsyncStreamingCallbackT @@ -435,13 +441,19 @@ class OpenAIChatGenerator: chunks: List[StreamingChunk] = [] chunk = None chunk_delta: StreamingChunk + last_chunk: Optional[ChatCompletionChunk] = None async for chunk in chat_completion: # pylint: disable=not-an-iterable assert len(chunk.choices) <= 1, "Streaming responses should have at most one choice." chunk_delta = self._convert_chat_completion_chunk_to_streaming_chunk(chunk) chunks.append(chunk_delta) await callback(chunk_delta) - return [self._convert_streaming_chunks_to_chat_message(chunk, chunks)] + last_chunk = chunk + + if not last_chunk: + raise ValueError("No chunks received from the stream") + + return [self._convert_streaming_chunks_to_chat_message(last_chunk, chunks)] def _check_finish_reason(self, meta: Dict[str, Any]) -> None: if meta["finish_reason"] == "length": diff --git a/releasenotes/notes/fix-mypy-1-16-8ed5d200c9e2018f.yaml b/releasenotes/notes/fix-mypy-1-16-8ed5d200c9e2018f.yaml new file mode 100644 index 000000000..5b08f3922 --- /dev/null +++ b/releasenotes/notes/fix-mypy-1-16-8ed5d200c9e2018f.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - | + Fixed a mypy issue in the OpenAIChatGenerator and its handling of stream responses. This issue only occurs with mypy >=1.16.0.