mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-08 22:21:35 +00:00
fix(streaming): join block-list citation deltas without extra nesting
This commit is contained in:
parent
1754a0a33c
commit
9b1b8e7ea2
2 changed files with 28 additions and 1 deletions
|
|
@ -8605,6 +8605,12 @@ def _stream_builder_response_cost(response: ModelResponse, logging_obj: Optional
|
|||
return _stream_builder_model_map_cost(response)
|
||||
|
||||
|
||||
def _joined_streamed_citations(streamed_citations: "tuple[object, ...]") -> "list[object]":
|
||||
if all(isinstance(citation, list) for citation in streamed_citations):
|
||||
return list(streamed_citations) # mutable-ok: JSON list field
|
||||
return [list(streamed_citations)] # mutable-ok: JSON list field
|
||||
|
||||
|
||||
def _stream_builder_model_map_cost(response: ModelResponse) -> float | None:
|
||||
model_name: Final = getattr(response, "model", None)
|
||||
usage: Final = getattr(response, "usage", None)
|
||||
|
|
@ -8861,7 +8867,9 @@ def stream_chunk_builder(
|
|||
fields["citation"] for fields in provider_field_dicts if fields.get("citation") is not None
|
||||
)
|
||||
citation_fields: Final = (
|
||||
{"citations": [list(streamed_citations)]} if streamed_citations else {} # mutable-ok: JSON dict field
|
||||
{"citations": _joined_streamed_citations(streamed_citations)} # mutable-ok: JSON dict field
|
||||
if streamed_citations
|
||||
else {} # mutable-ok: JSON dict field
|
||||
)
|
||||
combined_provider_fields: Final = { # mutable-ok: Message.provider_specific_fields is a plain dict field
|
||||
key: value
|
||||
|
|
|
|||
|
|
@ -83,3 +83,22 @@ def test_stream_chunk_builder_without_citation_deltas_sets_no_citations_key():
|
|||
assert fields is not None
|
||||
assert "citations" not in fields
|
||||
assert fields["web_search_results"] == [{"url": "https://example.com"}]
|
||||
|
||||
|
||||
def test_stream_chunk_builder_keeps_block_list_citation_deltas_unnested():
|
||||
block_one: Final = [dict(_CITATION_ONE), dict(_CITATION_TWO)]
|
||||
block_two: Final = [dict(_CITATION_ONE)]
|
||||
chunks: Final = [
|
||||
_chunk(Delta(content="Green sky.", role="assistant")),
|
||||
_chunk(Delta(content="", provider_specific_fields={"citation": block_one})),
|
||||
_chunk(Delta(content="", provider_specific_fields={"citation": block_two})),
|
||||
_chunk(Delta(content=""), finish_reason="stop"),
|
||||
]
|
||||
|
||||
response: Final = stream_chunk_builder(chunks=chunks)
|
||||
|
||||
assert response is not None
|
||||
fields: Final = response.choices[0].message.provider_specific_fields
|
||||
assert fields is not None
|
||||
assert fields["citations"] == [block_one, block_two]
|
||||
assert "citation" not in fields
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue