fix(streaming): keep the upper bound on generic chunk fields

This commit is contained in:
bunnysayzz 2026-09-30 04:06:34 +05:30
parent dbe3094391
commit e33457d926
2 changed files with 8 additions and 6 deletions

View file

@ -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(

View file

@ -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}