From e33457d9263373e5ebba9b9cd68cb40b5e8db9f7 Mon Sep 17 00:00:00 2001 From: bunnysayzz Date: Wed, 30 Sep 2026 04:06:34 +0530 Subject: [PATCH] fix(streaming): keep the upper bound on generic chunk fields --- litellm/litellm_core_utils/streaming_handler.py | 2 +- .../litellm_core_utils/test_streaming_overhead.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.py index 6e6a0cb3b65..147f7d5423e 100644 --- a/litellm/litellm_core_utils/streaming_handler.py +++ b/litellm/litellm_core_utils/streaming_handler.py @@ -2460,7 +2460,7 @@ def generic_chunk_has_all_required_fields(chunk: dict) -> bool: :param chunk: The dictionary to check. :return: True if all required fields are present, False otherwise. """ - return all(key in chunk for key in _GCHUNK_REQUIRED_FIELDS) + return _GCHUNK_REQUIRED_FIELDS <= chunk.keys() <= _GCHUNK_FIELDS def convert_generic_chunk_to_model_response_stream( diff --git a/tests/unit/litellm_core_utils/test_streaming_overhead.py b/tests/unit/litellm_core_utils/test_streaming_overhead.py index 96011624a38..2a6af87cf28 100644 --- a/tests/unit/litellm_core_utils/test_streaming_overhead.py +++ b/tests/unit/litellm_core_utils/test_streaming_overhead.py @@ -127,18 +127,20 @@ def test_gchunk_fields_is_frozenset(): def test_generic_chunk_has_all_required_fields_uses_module_constant(monkeypatch): - """generic_chunk_has_all_required_fields must use _GCHUNK_REQUIRED_FIELDS. + """generic_chunk_has_all_required_fields must use the module constants. - The check semantics: every required GChunk field must be present in `chunk`. - A chunk missing a required field is not a usable generic chunk. + The check semantics: every required GChunk field must be present in `chunk` + and every key must be a known GChunk field. Unknown keys and missing + required fields both reject the chunk. """ valid_chunk = _make_generic_chunk("hello") assert generic_chunk_has_all_required_fields(valid_chunk) is True - # Extra unknown keys don't matter, only that all required fields are present. + # A dict with an extra unknown key should return False — the unknown key + # is not a GChunk field, so the chunk is not a pure GChunk. extra_key_chunk = dict(valid_chunk) extra_key_chunk["unknown_extra_key"] = "value" - assert generic_chunk_has_all_required_fields(extra_key_chunk) is True + assert generic_chunk_has_all_required_fields(extra_key_chunk) is False # A dict missing required fields is rejected, even if every key it has is valid. partial_chunk = {"text": "hi", "is_finished": False}