mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
Merge 99a08e8563 into c068d1350b
This commit is contained in:
commit
664546da64
2 changed files with 180 additions and 11 deletions
|
|
@ -490,7 +490,7 @@ def _fold_choices_into_one(
|
|||
choices: Sequence["Choices"],
|
||||
tool_calls: Sequence[Mapping[str, object]],
|
||||
trailing_reasoning_content: str | None,
|
||||
trailing_reasoning_item: _BuiltReasoningItem | None,
|
||||
trailing_reasoning_items: Sequence[_BuiltReasoningItem],
|
||||
tool_search_items: Sequence[Mapping[str, object]],
|
||||
structured_output: bool = False,
|
||||
) -> "Choices":
|
||||
|
|
@ -509,7 +509,7 @@ def _fold_choices_into_one(
|
|||
reasoning_contents: Final = (*(_message_reasoning_content(m) for m in messages), trailing_reasoning_content)
|
||||
reasoning_items: Final = (
|
||||
*chain.from_iterable(_message_reasoning_items(m) for m in messages),
|
||||
*(() if trailing_reasoning_item is None else (trailing_reasoning_item,)),
|
||||
*trailing_reasoning_items,
|
||||
)
|
||||
return Choices(
|
||||
message=Message(
|
||||
|
|
@ -1196,7 +1196,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
choices: Final[list[Choices]] = []
|
||||
index = 0
|
||||
reasoning_content: str | None = None
|
||||
pending_reasoning_item: _BuiltReasoningItem | None = None
|
||||
pending_reasoning_items: tuple[_BuiltReasoningItem, ...] = ()
|
||||
|
||||
# Collect all tool calls to put them in a single choice
|
||||
# (Chat Completions API expects all tool calls in one message)
|
||||
|
|
@ -1205,12 +1205,22 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
|
||||
for item in output_items:
|
||||
if isinstance(item, ResponseReasoningItem):
|
||||
pending_reasoning_item = _build_reasoning_item(
|
||||
built_reasoning_item = _build_reasoning_item(
|
||||
item_id=item.id,
|
||||
encrypted_content=getattr(item, "encrypted_content", None),
|
||||
summary_raw=item.summary,
|
||||
)
|
||||
reasoning_content = " ".join(s["text"] for s in pending_reasoning_item["summary"] if s.get("text"))
|
||||
# The Responses API can emit several reasoning items before the next
|
||||
# message / tool call, and each flush below consumes every item seen
|
||||
# since the previous flush.
|
||||
pending_reasoning_items = (*pending_reasoning_items, built_reasoning_item)
|
||||
# ``reasoning_content`` deliberately keeps its pre-existing meaning -
|
||||
# the summary text of the most recent reasoning item - even though
|
||||
# ``reasoning_items`` now carries all of them. Widening it to join
|
||||
# every item's summary would change what every existing consumer of
|
||||
# ``reasoning_content`` sees, which is a different fix from "stop
|
||||
# dropping reasoning items" and belongs in its own change.
|
||||
reasoning_content = " ".join(s["text"] for s in built_reasoning_item["summary"] if s.get("text"))
|
||||
|
||||
elif isinstance(item, ResponseOutputMessage):
|
||||
for content in item.content:
|
||||
|
|
@ -1225,10 +1235,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
content=response_text if response_text else "",
|
||||
reasoning_content=reasoning_content,
|
||||
annotations=annotations,
|
||||
reasoning_items=cast(
|
||||
list[ChatCompletionReasoningItem] | None,
|
||||
([pending_reasoning_item] if pending_reasoning_item is not None else None),
|
||||
),
|
||||
reasoning_items=_as_chat_reasoning_items(pending_reasoning_items),
|
||||
)
|
||||
|
||||
choices.append(
|
||||
|
|
@ -1240,7 +1247,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
)
|
||||
|
||||
reasoning_content = None # flush
|
||||
pending_reasoning_item = None # flush
|
||||
pending_reasoning_items = () # flush
|
||||
index += 1
|
||||
|
||||
elif isinstance(item, ResponseFunctionToolCall):
|
||||
|
|
@ -1305,7 +1312,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
choices=choices,
|
||||
tool_calls=accumulated_tool_calls,
|
||||
trailing_reasoning_content=reasoning_content,
|
||||
trailing_reasoning_item=pending_reasoning_item,
|
||||
trailing_reasoning_items=pending_reasoning_items,
|
||||
tool_search_items=tool_search_items,
|
||||
structured_output=structured_output,
|
||||
)
|
||||
|
|
@ -2124,6 +2131,35 @@ class OpenAiResponsesToChatCompletionStreamIterator(BaseModelResponseIterator):
|
|||
)
|
||||
else:
|
||||
raise ValueError(f"Chat provider: Invalid text delta {parsed_chunk}")
|
||||
elif event_type == ResponsesAPIStreamEvents.OUTPUT_TEXT_ANNOTATION_ADDED: # pyright: ignore[reportUnknownMemberType] # the member post-dates the pinned openai stubs, it exists at runtime
|
||||
# A url_citation / file_citation arrived for the in-flight message.
|
||||
# Emit it on the delta exactly once in Chat Completions format -
|
||||
# this is the only event that carries it, so later
|
||||
# output_item.done / response.completed events must not repeat it
|
||||
# or accumulating clients would see duplicates (issue #43817).
|
||||
raw_annotation: Final = parsed_chunk.get("annotation", None) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the parsed chunk is an untyped event payload
|
||||
annotation: Final = LiteLLMResponsesTransformationHandler._convert_annotations_to_chat_format( # pyright: ignore[reportPrivateUsage, reportUnknownMemberType, reportUnknownVariableType] # cross-class reuse of the existing converter; the annotation payload is untyped, and renaming a protected helper is a wider change than this fix
|
||||
[raw_annotation] if raw_annotation is not None else None
|
||||
)
|
||||
if annotation:
|
||||
return ModelResponseStream( # pyright: ignore[reportUnknownVariableType] # annotation carries untyped payload values through to the Delta
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
index=0,
|
||||
delta=Delta(annotations=annotation),
|
||||
finish_reason=None,
|
||||
)
|
||||
]
|
||||
)
|
||||
return ModelResponseStream( # pyright: ignore[reportUnknownVariableType] # same untyped annotation payload
|
||||
choices=[
|
||||
StreamingChoices(
|
||||
index=0,
|
||||
delta=Delta(),
|
||||
finish_reason=None,
|
||||
)
|
||||
]
|
||||
)
|
||||
elif event_type == "response.reasoning_summary_text.delta":
|
||||
content_part = parsed_chunk.get("delta", None)
|
||||
if content_part:
|
||||
|
|
|
|||
|
|
@ -6078,3 +6078,136 @@ def test_stream_structured_output_drops_text_after_first_message_item(
|
|||
content: Final = "".join(chunk.choices[0].delta.content or "" for chunk in chunks)
|
||||
assert content == expected_content
|
||||
assert chunks[-1].choices[0].finish_reason == "stop"
|
||||
|
||||
|
||||
def test_convert_response_output_keeps_every_reasoning_item_before_a_message():
|
||||
"""Regression for issue #43620 (stream=False): the Responses API can emit several
|
||||
reasoning items before the assistant message, and the bridge used to overwrite
|
||||
its pending reasoning item per item, so only the last one reached
|
||||
message.reasoning_items. Every item must survive the flush."""
|
||||
from openai.types.responses import ResponseOutputMessage, ResponseOutputText, ResponseReasoningItem
|
||||
from litellm.completion_extras.litellm_responses_transformation.transformation import (
|
||||
LiteLLMResponsesTransformationHandler,
|
||||
)
|
||||
|
||||
rs_1 = ResponseReasoningItem(type="reasoning", id="rs_1", summary=[], encrypted_content="enc1")
|
||||
rs_2 = ResponseReasoningItem(type="reasoning", id="rs_2", summary=[], encrypted_content="enc2")
|
||||
message = ResponseOutputMessage(
|
||||
type="message",
|
||||
id="msg_1",
|
||||
role="assistant",
|
||||
status="completed",
|
||||
content=[ResponseOutputText(type="output_text", text="hello", annotations=[])],
|
||||
)
|
||||
|
||||
choices = LiteLLMResponsesTransformationHandler._convert_response_output_to_choices([rs_1, rs_2, message])
|
||||
|
||||
assert len(choices) == 1
|
||||
reasoning_items = choices[0].message.reasoning_items
|
||||
assert reasoning_items is not None
|
||||
assert [item["id"] for item in reasoning_items] == ["rs_1", "rs_2"]
|
||||
|
||||
|
||||
def test_convert_response_output_keeps_every_reasoning_item_before_a_tool_call():
|
||||
"""Regression for issue #43620 (stream=False): [reasoning rs_1, reasoning rs_2,
|
||||
function_call] must merge both reasoning items into the trailing tool_calls
|
||||
choice instead of keeping only rs_2."""
|
||||
from openai.types.responses import ResponseFunctionToolCall, ResponseReasoningItem
|
||||
|
||||
from litellm.completion_extras.litellm_responses_transformation.transformation import (
|
||||
LiteLLMResponsesTransformationHandler,
|
||||
)
|
||||
|
||||
rs_1 = ResponseReasoningItem(type="reasoning", id="rs_1", summary=[], encrypted_content="enc1")
|
||||
rs_2 = ResponseReasoningItem(type="reasoning", id="rs_2", summary=[], encrypted_content="enc2")
|
||||
function_call = ResponseFunctionToolCall(
|
||||
type="function_call",
|
||||
id="fc_1",
|
||||
call_id="call_1",
|
||||
name="get_weather",
|
||||
arguments="{}",
|
||||
status="completed",
|
||||
)
|
||||
|
||||
choices = LiteLLMResponsesTransformationHandler._convert_response_output_to_choices([rs_1, rs_2, function_call])
|
||||
|
||||
assert len(choices) == 1
|
||||
choice = choices[0]
|
||||
assert choice.finish_reason == "tool_calls"
|
||||
reasoning_items = choice.message.reasoning_items
|
||||
assert reasoning_items is not None
|
||||
assert [item["id"] for item in reasoning_items] == ["rs_1", "rs_2"]
|
||||
assert choice.message.tool_calls[0].id == "fc_1"
|
||||
|
||||
|
||||
def test_stream_translator_emits_url_citation_annotations_exactly_once():
|
||||
"""Regression for issue #43817: the Responses API delivers annotations as
|
||||
response.output_text.annotation.added events, and the Chat Completions
|
||||
stream bridge dropped them, so a streamed reply never carried the
|
||||
url_citation that the same reply with stream=False reports on
|
||||
message.annotations. The annotation must ride exactly one delta — the
|
||||
terminal response.completed event repeats it in the response payload and
|
||||
re-emitting it there would duplicate it for accumulating clients."""
|
||||
from litellm.completion_extras.litellm_responses_transformation.transformation import (
|
||||
OpenAiResponsesToChatCompletionStreamIterator,
|
||||
)
|
||||
|
||||
citation = {
|
||||
"type": "url_citation",
|
||||
"url": "https://a.example",
|
||||
"title": "A",
|
||||
"start_index": 0,
|
||||
"end_index": 5,
|
||||
}
|
||||
reply = {
|
||||
"id": "resp_1",
|
||||
"object": "response",
|
||||
"created_at": 0,
|
||||
"status": "completed",
|
||||
"model": "gpt-5-mini",
|
||||
"output": [
|
||||
{
|
||||
"type": "message",
|
||||
"id": "msg_1",
|
||||
"role": "assistant",
|
||||
"status": "completed",
|
||||
"content": [
|
||||
{
|
||||
"type": "output_text",
|
||||
"text": "Sunny in Paris.",
|
||||
"annotations": [citation],
|
||||
}
|
||||
],
|
||||
}
|
||||
],
|
||||
}
|
||||
iterator = OpenAiResponsesToChatCompletionStreamIterator(streaming_response=None, sync_stream=True)
|
||||
events = [
|
||||
{"type": "response.created", "response": {**reply, "status": "in_progress", "output": []}},
|
||||
{
|
||||
"type": "response.output_text.delta",
|
||||
"output_index": 0,
|
||||
"item_id": "msg_1",
|
||||
"content_index": 0,
|
||||
"delta": "Sunny in Paris.",
|
||||
},
|
||||
{
|
||||
"type": "response.output_text.annotation.added",
|
||||
"output_index": 0,
|
||||
"item_id": "msg_1",
|
||||
"content_index": 0,
|
||||
"annotation_index": 0,
|
||||
"annotation": citation,
|
||||
},
|
||||
{"type": "response.completed", "response": reply},
|
||||
]
|
||||
|
||||
chunks = [iterator.chunk_parser(event) for event in events]
|
||||
|
||||
carried = [
|
||||
annotation
|
||||
for chunk in chunks
|
||||
for choice in chunk.choices
|
||||
for annotation in (getattr(choice.delta, "annotations", None) or [])
|
||||
]
|
||||
assert carried == [citation]
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue