This commit is contained in:
Caesar / 谢佳辰 · Jiachen Xie 2026-11-06 01:18:53 +00:00 • committed by GitHub
commit 548a9b9054
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 13 deletions

View file

@ -287,6 +287,14 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
"arguments": json.dumps(first.get("input", {})),
},
)
if btype == "text":
return (
{
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": first.get("text", "")}],
},
)
return ()
def translate_messages_to_responses_input(
@ -440,19 +448,6 @@ class LiteLLMAnthropicToResponsesAPIAdapter:
for _, group in groupby(enumerate(blocks), key=self._assistant_block_group_key)
for item in self._assistant_group_to_input_items(tuple(block for _, block in group))
)
asst_parts: list[dict[str, object]] = [ # mutable-ok: API message payload
{"type": "output_text", "text": block.get("text", "")}
for block in blocks
if block.get("type") == "text"
]
if asst_parts:
input_items.append(
{
"type": "message",
"role": "assistant",
"content": asst_parts,
}
)
return input_items

View file

@ -671,6 +671,51 @@ class TestTranslateMessagesToResponsesInput:
assert result[0]["summary"] == [{"type": "summary_text", "text": "Before the call."}]
assert result[2]["summary"] == [{"type": "summary_text", "text": "After the call."}]
def test_assistant_text_before_and_after_tool_use_preserves_order(self):
"""Text around tool_use stays in block order, not replayed after (#45470)."""
messages: Final = [
{"role": "user", "content": "weather in Paris?"},
{
"role": "assistant",
"content": [
{"type": "text", "text": "Let me check."},
{
"type": "tool_use",
"id": "toolu_1",
"name": "get_weather",
"input": {"city": "Paris"},
},
{"type": "text", "text": "(after)"},
],
},
{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "toolu_1", "content": "sunny"}
],
},
]
result: Final = _translate_messages(messages)
assert [item["type"] for item in result] == [
"message",
"message",
"function_call",
"message",
"function_call_output",
]
assert result[1] == {
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": "Let me check."}],
}
assert result[2]["type"] == "function_call"
assert result[2]["call_id"] == "toolu_1"
assert result[3] == {
"type": "message",
"role": "assistant",
"content": [{"type": "output_text", "text": "(after)"}],
}
def test_thinking_and_text_stay_separate(self):
"""The visible answer stays the only thing in the assistant message."""
messages = [