A programming framework for agentic AI
fix: warn on empty chunks, don't error out (#5332)
Mohammad Mazraeh06c706633d17
diff --git a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.pyindex e973862df..c0e89e17b 100644--- a/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py+++ b/python/packages/autogen-ext/src/autogen_ext/models/openai/_openai_client.py@@ -646,7 +646,7 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):json_output (Optional[bool], optional): If True, the output will be in JSON format. Defaults to None.extra_create_args (Mapping[str, Any], optional): Additional arguments for the creation process. Default to `{}`.cancellation_token (Optional[CancellationToken], optional): A token to cancel the operation. Defaults to None.- max_consecutive_empty_chunk_tolerance (int): The maximum number of consecutive empty chunks to tolerate before raising a ValueError. This seems to only be needed to set when using `AzureOpenAIChatCompletionClient`. Defaults to 0.+ max_consecutive_empty_chunk_tolerance (int): [Deprecated] The maximum number of consecutive empty chunks to tolerate before raising a ValueError. This seems to only be needed to set when using `AzureOpenAIChatCompletionClient`. Defaults to 0. This parameter is deprecated, empty chunks will be skipped.Yields:AsyncGenerator[Union[str, CreateResult], None]: A generator yielding the completion results as they are produced.@@ -717,6 +717,15 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):full_tool_calls: Dict[int, FunctionCall] = {}completion_tokens = 0logprobs: Optional[List[ChatCompletionTokenLogprob]] = None++ if max_consecutive_empty_chunk_tolerance != 0:+ warnings.warn(+ "The 'max_consecutive_empty_chunk_tolerance' parameter is deprecated and will be removed in the future releases. All of empty chunks will be skipped with a warning.",+ DeprecationWarning,+ stacklevel=2,+ )+ empty_chunk_warning_has_been_issued: bool = False+ empty_chunk_warning_threshold: int = 10empty_chunk_count = 0while True:@@ -726,16 +735,16 @@ class BaseOpenAIChatCompletionClient(ChatCompletionClient):cancellation_token.link_future(chunk_future)chunk = await chunk_future- # This is to address a bug in AzureOpenAIChatCompletionClient. OpenAIChatCompletionClient works fine.+ # Empty chunks has been observed when the endpoint is under heavy load.# https://github.com/microsoft/autogen/issues/4213if len(chunk.choices) == 0:empty_chunk_count += 1- if max_consecutive_empty_chunk_tolerance == 0:- raise ValueError(- "Consecutive empty chunks found. Change max_empty_consecutive_chunk_tolerance to increase empty chunk tolerance"+ if not empty_chunk_warning_has_been_issued and empty_chunk_count >= empty_chunk_warning_threshold:+ empty_chunk_warning_has_been_issued = True+ warnings.warn(+ f"Received more than {empty_chunk_warning_threshold} consecutive empty chunks. Empty chunks are being ignored.",+ stacklevel=2,)- elif empty_chunk_count >= max_consecutive_empty_chunk_tolerance:- raise ValueError("Exceeded the threshold of receiving consecutive empty chunks")continueelse:empty_chunk_count = 0