mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
Merge 0b5b6ab3f5 into c7c895db45
This commit is contained in:
commit
2c58b3739b
2 changed files with 164 additions and 18 deletions
|
|
@ -490,9 +490,10 @@ def _fold_choices_into_one(
|
|||
choices: Sequence["Choices"],
|
||||
tool_calls: Sequence[Mapping[str, object]],
|
||||
trailing_reasoning_content: str | None,
|
||||
trailing_reasoning_item: _BuiltReasoningItem | None,
|
||||
tool_search_items: Sequence[Mapping[str, object]],
|
||||
trailing_reasoning_item: _BuiltReasoningItem | None = None,
|
||||
tool_search_items: Sequence[Mapping[str, object]] = (),
|
||||
structured_output: bool = False,
|
||||
trailing_reasoning_items: Sequence[_BuiltReasoningItem] = (),
|
||||
) -> "Choices":
|
||||
"""Clients that only read choices[0] must still see every text and tool call.
|
||||
|
||||
|
|
@ -507,9 +508,13 @@ def _fold_choices_into_one(
|
|||
offsets: Final = (0, *accumulate(len(text) for text in texts))
|
||||
annotations: Final = tuple(_shifted_annotations(content_messages, offsets))
|
||||
reasoning_contents: Final = (*(_message_reasoning_content(m) for m in messages), trailing_reasoning_content)
|
||||
accumulated_trailing_items: Final = (
|
||||
*trailing_reasoning_items,
|
||||
*(() if trailing_reasoning_item is None else (trailing_reasoning_item,)),
|
||||
)
|
||||
reasoning_items: Final = (
|
||||
*chain.from_iterable(_message_reasoning_items(m) for m in messages),
|
||||
*(() if trailing_reasoning_item is None else (trailing_reasoning_item,)),
|
||||
*accumulated_trailing_items,
|
||||
)
|
||||
return Choices(
|
||||
message=Message(
|
||||
|
|
@ -526,6 +531,24 @@ def _fold_choices_into_one(
|
|||
)
|
||||
|
||||
|
||||
def _append_reasoning_if_present(
|
||||
item: object,
|
||||
pending: list[_BuiltReasoningItem], # mutable-ok: accumulator
|
||||
current_content: str | None,
|
||||
) -> tuple[bool, str | None]:
|
||||
"""Extract reasoning item if present, append to pending, and update concatenated summary."""
|
||||
reasoning_item: Final = _reasoning_item_from_output_item(item)
|
||||
if reasoning_item is None:
|
||||
return False, current_content
|
||||
pending.append(reasoning_item)
|
||||
summary_texts: Final = [s["text"] for s in reasoning_item["summary"] if s.get("text")]
|
||||
if not summary_texts:
|
||||
return True, current_content
|
||||
step_text: Final = " ".join(summary_texts)
|
||||
merged_content: Final = f"{current_content} {step_text}".strip() if current_content else step_text
|
||||
return True, merged_content
|
||||
|
||||
|
||||
def _responses_event_type(chunk: Mapping[str, object]) -> object:
|
||||
event_type: Final = chunk.get("type")
|
||||
return event_type.value if isinstance(event_type, ResponsesAPIStreamEvents) else event_type
|
||||
|
|
@ -1181,7 +1204,6 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
from openai.types.responses import (
|
||||
ResponseFunctionToolCall,
|
||||
ResponseOutputMessage,
|
||||
ResponseReasoningItem,
|
||||
)
|
||||
|
||||
try:
|
||||
|
|
@ -1196,7 +1218,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
choices: Final[list[Choices]] = []
|
||||
index = 0
|
||||
reasoning_content: str | None = None
|
||||
pending_reasoning_item: _BuiltReasoningItem | None = None
|
||||
pending_reasoning_items: Final[list[_BuiltReasoningItem]] = [] # mutable-ok: accumulator
|
||||
|
||||
# Collect all tool calls to put them in a single choice
|
||||
# (Chat Completions API expects all tool calls in one message)
|
||||
|
|
@ -1204,13 +1226,11 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
tool_call_index = 0
|
||||
|
||||
for item in output_items:
|
||||
if isinstance(item, ResponseReasoningItem):
|
||||
pending_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"))
|
||||
is_reasoning, reasoning_content = _append_reasoning_if_present(
|
||||
item, pending_reasoning_items, reasoning_content
|
||||
)
|
||||
if is_reasoning:
|
||||
continue
|
||||
|
||||
elif isinstance(item, ResponseOutputMessage):
|
||||
for content in item.content:
|
||||
|
|
@ -1225,10 +1245,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 +1257,7 @@ class LiteLLMResponsesTransformationHandler(CompletionTransformationBridge):
|
|||
)
|
||||
|
||||
reasoning_content = None # flush
|
||||
pending_reasoning_item = None # flush
|
||||
pending_reasoning_items.clear() # flush
|
||||
index += 1
|
||||
|
||||
elif isinstance(item, ResponseFunctionToolCall):
|
||||
|
|
@ -1305,7 +1322,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,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -6060,3 +6060,132 @@ 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"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"raw_output_items, expected_reasoning_ids, expected_summary_text, target_type",
|
||||
[
|
||||
pytest.param(
|
||||
[
|
||||
{
|
||||
"type": "reasoning",
|
||||
"id": "rs_1",
|
||||
"summary": [{"type": "summary_text", "text": "Step 1"}],
|
||||
"encrypted_content": "enc1",
|
||||
},
|
||||
{
|
||||
"type": "reasoning",
|
||||
"id": "rs_2",
|
||||
"summary": [{"type": "summary_text", "text": "Step 2"}],
|
||||
"encrypted_content": "enc2",
|
||||
},
|
||||
{
|
||||
"type": "function_call",
|
||||
"id": "fc_1",
|
||||
"call_id": "call_1",
|
||||
"name": "search",
|
||||
"arguments": "{}",
|
||||
"status": "completed",
|
||||
},
|
||||
],
|
||||
["rs_1", "rs_2"],
|
||||
"Step 1 Step 2",
|
||||
"tool_calls",
|
||||
id="multiple_reasoning_items_before_tool_call",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{
|
||||
"type": "reasoning",
|
||||
"id": "rs_alpha",
|
||||
"summary": [{"type": "summary_text", "text": "Analysis"}],
|
||||
"encrypted_content": "enc_a",
|
||||
},
|
||||
{
|
||||
"type": "reasoning",
|
||||
"id": "rs_beta",
|
||||
"summary": [{"type": "summary_text", "text": "Conclusion"}],
|
||||
"encrypted_content": "enc_b",
|
||||
},
|
||||
],
|
||||
["rs_alpha", "rs_beta"],
|
||||
"Analysis Conclusion",
|
||||
"message",
|
||||
id="multiple_reasoning_items_before_output_message",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{
|
||||
"type": "reasoning",
|
||||
"id": "rs_raw_1",
|
||||
"summary": [{"type": "summary_text", "text": "Raw Step 1"}],
|
||||
"encrypted_content": "enc_raw",
|
||||
},
|
||||
{
|
||||
"type": "message",
|
||||
"role": "assistant",
|
||||
"content": [{"type": "output_text", "text": "Raw response text"}],
|
||||
},
|
||||
],
|
||||
["rs_raw_1"],
|
||||
"Raw Step 1",
|
||||
"raw_dict_message",
|
||||
id="reasoning_items_before_raw_dict_message",
|
||||
),
|
||||
pytest.param(
|
||||
[
|
||||
{
|
||||
"type": "function_call",
|
||||
"id": "fc_solo",
|
||||
"call_id": "call_solo",
|
||||
"name": "lookup",
|
||||
"arguments": "{}",
|
||||
"status": "completed",
|
||||
}
|
||||
],
|
||||
None,
|
||||
None,
|
||||
"tool_calls",
|
||||
id="zero_reasoning_items_returns_none",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_convert_response_output_to_choices_preserves_all_reasoning_items(
|
||||
raw_output_items: list[dict[str, object]],
|
||||
expected_reasoning_ids: list[str] | None,
|
||||
expected_summary_text: str | None,
|
||||
target_type: str,
|
||||
) -> None:
|
||||
"""Verify non-streaming Responses API bridge retains all reasoning items across message and tool call turns."""
|
||||
from openai.types.responses import ResponseOutputMessage
|
||||
from openai.types.responses.response_output_message import ResponseOutputText
|
||||
|
||||
items_to_pass: Final[list[object]] = list(raw_output_items) # mutable-ok: fixture setup
|
||||
if target_type == "message":
|
||||
items_to_pass.append(
|
||||
ResponseOutputMessage(
|
||||
id="msg_1",
|
||||
role="assistant",
|
||||
status="completed",
|
||||
type="message",
|
||||
content=[ResponseOutputText(annotations=[], text="Done", type="output_text", logprobs=[])],
|
||||
)
|
||||
)
|
||||
|
||||
handler: Final = LiteLLMResponsesTransformationHandler()
|
||||
choices: Final = handler._convert_response_output_to_choices(
|
||||
items_to_pass,
|
||||
handle_raw_dict_callback=handler._handle_raw_dict_response_item,
|
||||
)
|
||||
|
||||
assert len(choices) == 1
|
||||
msg = choices[0].message
|
||||
reasoning_items = getattr(msg, "reasoning_items", None)
|
||||
reasoning_content = getattr(msg, "reasoning_content", None)
|
||||
if expected_reasoning_ids is None:
|
||||
assert reasoning_items is None
|
||||
assert reasoning_content is None
|
||||
else:
|
||||
assert reasoning_items is not None
|
||||
assert [item["id"] for item in reasoning_items] == expected_reasoning_ids
|
||||
assert reasoning_content == expected_summary_text
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue