mirror of
https://github.com/BerriAI/litellm.git
synced 2026-12-06 02:09:04 +00:00
* fix(friendliai): drop litellm-internal assistant fields before sending Claude Code replays thinking blocks on every turn after the first. The Anthropic adapter turns them into thinking_blocks plus reasoning_content on the chat assistant message, and FriendliAI rejects any message key it does not know, so multi-turn Claude Code sessions on friendliai/ models failed with a 500 after the first tool call FriendliAI accepts reasoning_content and GLM reads it back, so only thinking_blocks and provider_specific_fields are dropped. together_ai already had this exact helper as a private function, so it moves to common_utils next to strip_litellm_internal_message_fields and both providers share it Related to #32188 * test(friendliai): cover a replayed tool call turn The regression fixture is now the shape Claude Code replays after a tool call: an assistant turn with thinking_blocks, tool_calls and no content, followed by the matching tool result. The test asserts both survive the sync and async request paths unchanged * ci: retrigger checks * ci: retrigger benchmarks after runner timeout * chore(friendliai): mark inherited list signatures as mutable-ok The type-discipline gate on main now caps LIT001 at the merge-base count, so the _transform_messages overloads need the same inherited-contract marker together_ai uses for its identical override * ci(test-unit): add tests/unit/llms/friendliai to the llms-providers shard * test(friendliai): check the outbound FriendliAI body through the proxy for chat and messages replays Both replay shapes go through a real proxy to a scripted FriendliAI upstream, and the test reads the request body FriendliAI received. It fails at the merge base, where thinking_blocks and provider_specific_fields reach the upstream, and passes with the field stripping in place. --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
122 lines
5.4 KiB
Python
122 lines
5.4 KiB
Python
import json
|
|
from typing import Final
|
|
|
|
from integration._support.client import Gateway
|
|
from integration._support.wire import Reply, Request, wire_server
|
|
from pydantic import JsonValue, TypeAdapter
|
|
|
|
_MODEL: Final = "zai-org/GLM-5.3"
|
|
_API_KEY: Final = "synthetic-friendliai-key"
|
|
_PROMPT: Final = "What is the weather in Paris? Use the tool."
|
|
_REASONING: Final = "The user wants the weather in Paris, so call get_weather."
|
|
_TOOL_RESULT: Final = "18C and sunny"
|
|
_ANSWER: Final = "It is 18C and sunny in Paris."
|
|
_LITELLM_ONLY_FIELDS: Final = frozenset({"thinking_blocks", "reasoning_items", "provider_specific_fields"})
|
|
_JSON_OBJECT: Final = TypeAdapter(dict[str, JsonValue])
|
|
_MESSAGES: Final = TypeAdapter(tuple[dict[str, JsonValue], ...])
|
|
_CALL: Final[JsonValue] = {
|
|
"id": "call_paris",
|
|
"type": "function",
|
|
"function": {"name": "get_weather", "arguments": '{"city": "Paris"}'},
|
|
}
|
|
_PARAMETERS: Final[JsonValue] = {
|
|
"type": "object",
|
|
"properties": {"city": {"type": "string"}},
|
|
"required": ["city"],
|
|
}
|
|
|
|
|
|
def _respond(request: Request) -> Reply:
|
|
assert (request.method, request.target) == ("POST", "/chat/completions")
|
|
assert request.headers["authorization"] == f"Bearer {_API_KEY}"
|
|
return Reply(
|
|
body=json.dumps(
|
|
{
|
|
"id": "friendliai-replay",
|
|
"object": "chat.completion",
|
|
"created": 1,
|
|
"model": _MODEL,
|
|
"choices": [
|
|
{"index": 0, "message": {"role": "assistant", "content": _ANSWER}, "finish_reason": "stop"}
|
|
],
|
|
"usage": {"prompt_tokens": 40, "completion_tokens": 9, "total_tokens": 49},
|
|
}
|
|
).encode()
|
|
)
|
|
|
|
|
|
def _sent_messages(requests: tuple[Request, ...]) -> tuple[dict[str, JsonValue], ...]:
|
|
assert len(requests) == 1
|
|
body: Final = _JSON_OBJECT.validate_json(requests[0].body)
|
|
assert body["model"] == _MODEL
|
|
return _MESSAGES.validate_python(body["messages"])
|
|
|
|
|
|
def test_chat_replay_of_returned_assistant_message_reaches_friendliai_without_litellm_fields(gateway: Gateway) -> None:
|
|
with wire_server(_respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=f"friendliai/{_MODEL}", api_base=wire.url, api_key=_API_KEY)
|
|
response: Final = gateway.request(
|
|
"POST",
|
|
"/v1/chat/completions",
|
|
{
|
|
"model": model,
|
|
"messages": [
|
|
{"role": "user", "content": _PROMPT},
|
|
{
|
|
"role": "assistant",
|
|
"content": None,
|
|
"tool_calls": [{**_CALL, "provider_specific_fields": {"thought_signature": "sig"}}],
|
|
"reasoning_content": _REASONING,
|
|
"thinking_blocks": [{"type": "thinking", "thinking": _REASONING, "signature": "sig"}],
|
|
"provider_specific_fields": {"reasoning": _REASONING},
|
|
},
|
|
{
|
|
"role": "tool",
|
|
"tool_call_id": "call_paris",
|
|
"content": _TOOL_RESULT,
|
|
"provider_specific_fields": {},
|
|
},
|
|
],
|
|
"tools": [{"type": "function", "function": {"name": "get_weather", "parameters": _PARAMETERS}}],
|
|
},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert _sent_messages(wire.drain()) == (
|
|
{"role": "user", "content": _PROMPT},
|
|
{"role": "assistant", "tool_calls": [_CALL], "reasoning_content": _REASONING},
|
|
{"role": "tool", "tool_call_id": "call_paris", "content": _TOOL_RESULT},
|
|
)
|
|
|
|
|
|
def test_messages_replay_of_thinking_and_tool_use_reaches_friendliai_without_litellm_fields(gateway: Gateway) -> None:
|
|
with wire_server(_respond) as wire, gateway.scenario() as scenario:
|
|
model: Final = scenario.model(model=f"friendliai/{_MODEL}", api_base=wire.url, api_key=_API_KEY)
|
|
response: Final = gateway.request(
|
|
"POST",
|
|
"/v1/messages",
|
|
{
|
|
"model": model,
|
|
"max_tokens": 256,
|
|
"messages": [
|
|
{"role": "user", "content": _PROMPT},
|
|
{
|
|
"role": "assistant",
|
|
"content": [
|
|
{"type": "thinking", "thinking": _REASONING, "signature": "sig"},
|
|
{"type": "tool_use", "id": "call_paris", "name": "get_weather", "input": {"city": "Paris"}},
|
|
],
|
|
},
|
|
{
|
|
"role": "user",
|
|
"content": [{"type": "tool_result", "tool_use_id": "call_paris", "content": _TOOL_RESULT}],
|
|
},
|
|
],
|
|
"tools": [{"name": "get_weather", "input_schema": _PARAMETERS}],
|
|
},
|
|
)
|
|
assert response.status_code == 200, response.text
|
|
assert _JSON_OBJECT.validate_json(response.content)["content"] == [{"type": "text", "text": _ANSWER}]
|
|
messages: Final = _sent_messages(wire.drain())
|
|
assert [sorted(_LITELLM_ONLY_FIELDS & message.keys()) for message in messages] == [[]] * len(messages)
|
|
assistant: Final = next(message for message in messages if message["role"] == "assistant")
|
|
assert assistant["reasoning_content"] == _REASONING
|