mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
Merge 8df99d83d3 into 776f8bc309
This commit is contained in:
commit
c588dbf789
7 changed files with 87 additions and 8 deletions
|
|
@ -569,6 +569,15 @@ def _prompt_management_sees_a_provisional_message_list(
|
|||
kwargs.pop(CARRY_UNMATCHED_MESSAGE_POINTS, None)
|
||||
|
||||
|
||||
def _normalize_responses_api_string_input(input: str | ResponseInputParam) -> ResponseInputParam:
|
||||
"""Return list-form ``input``; OpenAI's Responses API accepts a plain string, but provider transforms
|
||||
that only handle the array shape turn it into a spurious client-facing 400 (#41963).
|
||||
"""
|
||||
if isinstance(input, str):
|
||||
return cast(ResponseInputParam, [{"role": "user", "content": input}])
|
||||
return input
|
||||
|
||||
|
||||
@client
|
||||
async def aresponses(
|
||||
input: str | ResponseInputParam,
|
||||
|
|
@ -1232,6 +1241,9 @@ def responses(
|
|||
#########################################################
|
||||
input, tools = _apply_managed_file_id_mapping(input=input, tools=tools, kwargs=kwargs, local_vars=local_vars)
|
||||
|
||||
input = _normalize_responses_api_string_input(input) # rebind-ok: dispatch reads this name
|
||||
local_vars["input"] = input
|
||||
|
||||
#########################################################
|
||||
# Native MCP Responses API
|
||||
#########################################################
|
||||
|
|
|
|||
|
|
@ -227,7 +227,8 @@ def _assert_native_responses_request(route, expected_url, expected_model):
|
|||
assert f"{request.url.scheme}://{request.url.host}{request.url.path}" == expected_url
|
||||
assert request.headers["api-key"] == "fake-key"
|
||||
assert body["model"] == expected_model
|
||||
assert body["input"] == "What is the weather in SF?"
|
||||
# string input is normalized to the canonical list before provider dispatch
|
||||
assert body["input"] == [{"role": "user", "content": "What is the weather in SF?"}]
|
||||
assert "messages" not in body
|
||||
assert body["reasoning"] == {"effort": "high"}
|
||||
assert body["tools"] == [WEATHER_TOOL]
|
||||
|
|
|
|||
|
|
@ -321,7 +321,7 @@ def test_responses_call_keeps_a_developer_item_with_non_text_parts_in_place_as_a
|
|||
)
|
||||
|
||||
|
||||
def test_responses_call_forwards_string_input_and_instructions_unchanged() -> None:
|
||||
def test_responses_call_normalizes_string_input_and_forwards_instructions_unchanged() -> None:
|
||||
client: Final = _mock_http_client(_fireworks_response("accounts/fireworks/models/kimi-k3"))
|
||||
with patch(HTTPX_CLIENT_FACTORY, return_value=client):
|
||||
litellm.responses(
|
||||
|
|
@ -332,7 +332,8 @@ def test_responses_call_forwards_string_input_and_instructions_unchanged() -> No
|
|||
)
|
||||
_, _, body = _sent_request(client)
|
||||
assert body["instructions"] == "Answer with exactly one word."
|
||||
assert body["input"] == "What is the capital of France?"
|
||||
# string input is normalized to the canonical list before provider dispatch
|
||||
assert body["input"] == [{"role": "user", "content": "What is the capital of France?"}]
|
||||
|
||||
|
||||
def test_transform_request_forwards_non_string_instructions_and_input_untouched() -> None:
|
||||
|
|
|
|||
|
|
@ -1,12 +1,16 @@
|
|||
import inspect
|
||||
from collections.abc import Awaitable, Callable, Mapping
|
||||
from typing import Final, cast # noqa: TID251 # narrows legacy callable signatures for inspect
|
||||
from typing import Any, Final, cast # noqa: TID251 # narrows legacy callable signatures for inspect
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
import litellm
|
||||
from litellm.responses import dispatch as responses_dispatch
|
||||
from litellm.responses import main as python_responses
|
||||
from litellm.responses.main import (
|
||||
_normalize_responses_api_string_input, # pyright: ignore[reportPrivateUsage] # regression target
|
||||
)
|
||||
from litellm.responses.dispatch import (
|
||||
_ADISPATCH, # pyright: ignore[reportPrivateUsage] # tests configured dispatch
|
||||
_DISPATCH, # pyright: ignore[reportPrivateUsage] # tests configured dispatch
|
||||
|
|
@ -320,3 +324,54 @@ def test_responses_with_retries_uses_the_dispatch_entrypoint(monkeypatch: pytest
|
|||
assert result is expected
|
||||
assert calls[0]["num_retries"] == 0
|
||||
assert calls[0]["max_retries"] == 0
|
||||
|
||||
class TestNormalizeResponsesApiStringInputForIssue41963:
|
||||
"""Regression tests for https://github.com/BerriAI/litellm/issues/41963."""
|
||||
|
||||
def test_string_input_becomes_canonical_list(self) -> None:
|
||||
assert _normalize_responses_api_string_input("Reply with exactly ROUTE_OK.") == [
|
||||
{"role": "user", "content": "Reply with exactly ROUTE_OK."}
|
||||
]
|
||||
|
||||
def test_empty_string_is_normalized(self) -> None:
|
||||
assert _normalize_responses_api_string_input("") == [{"role": "user", "content": ""}]
|
||||
|
||||
def test_list_input_passes_through_untouched(self) -> None:
|
||||
original: Final = [{"role": "user", "content": "Reply with exactly ROUTE_OK."}]
|
||||
assert _normalize_responses_api_string_input(original) == original
|
||||
|
||||
def test_native_provider_receives_normalized_list_for_string_input(self) -> None:
|
||||
captured: Final[dict[str, Any]] = {}
|
||||
|
||||
def capture_handler(**kwargs: Any) -> None:
|
||||
captured.update(kwargs)
|
||||
|
||||
with patch("litellm.responses.main.base_llm_http_handler") as mock_handler: # test-quality-ok: the dispatch seam is the behavior under test; no DI seam exposes what input the provider receives
|
||||
mock_handler.response_api_handler.side_effect = capture_handler
|
||||
python_responses.responses(
|
||||
input="Reply with exactly ROUTE_OK.",
|
||||
model="gpt-4o",
|
||||
litellm_logging_obj=MagicMock(),
|
||||
api_key="sk-test",
|
||||
)
|
||||
|
||||
assert captured["input"] == [{"role": "user", "content": "Reply with exactly ROUTE_OK."}]
|
||||
|
||||
def test_native_provider_receives_list_content_intact_for_list_input(self) -> None:
|
||||
original: Final = [{"role": "user", "content": "Reply with exactly ROUTE_OK."}]
|
||||
captured: Final[dict[str, Any]] = {}
|
||||
|
||||
def capture_handler(**kwargs: Any) -> None:
|
||||
captured.update(kwargs)
|
||||
|
||||
with patch("litellm.responses.main.base_llm_http_handler") as mock_handler: # test-quality-ok: same dispatch seam as the string-input case
|
||||
mock_handler.response_api_handler.side_effect = capture_handler
|
||||
python_responses.responses(
|
||||
input=original,
|
||||
model="gpt-4o",
|
||||
litellm_logging_obj=MagicMock(),
|
||||
api_key="sk-test",
|
||||
)
|
||||
|
||||
assert isinstance(captured["input"], list)
|
||||
assert captured["input"] == original
|
||||
|
|
|
|||
|
|
@ -94,6 +94,9 @@ async def test_aresponses_context_management_and_shell_request_body_matches_expe
|
|||
assert the httpx POST request body matches the expected JSON.
|
||||
"""
|
||||
expected_body = _load_expected_body("context_management_and_shell.json")
|
||||
call_input = expected_body["input"]
|
||||
# string input is normalized to the canonical list before dispatch
|
||||
expected_body["input"] = [{"role": "user", "content": call_input}]
|
||||
|
||||
with patch(
|
||||
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
||||
|
|
@ -103,7 +106,7 @@ async def test_aresponses_context_management_and_shell_request_body_matches_expe
|
|||
|
||||
await litellm.aresponses(
|
||||
model="openai/gpt-4o",
|
||||
input=expected_body["input"],
|
||||
input=call_input,
|
||||
context_management=expected_body["context_management"],
|
||||
tools=expected_body["tools"],
|
||||
tool_choice=expected_body["tool_choice"],
|
||||
|
|
@ -121,6 +124,9 @@ async def test_aresponses_azure_shell_tool_request_body_matches_expected():
|
|||
assert the httpx POST request body carries the shell tool verbatim.
|
||||
"""
|
||||
expected_body = _load_expected_body("azure_shell_tool.json")
|
||||
call_input = expected_body["input"]
|
||||
# string input is normalized to the canonical list before dispatch
|
||||
expected_body["input"] = [{"role": "user", "content": call_input}]
|
||||
|
||||
with patch(
|
||||
"litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
|
||||
|
|
@ -135,7 +141,7 @@ async def test_aresponses_azure_shell_tool_request_body_matches_expected():
|
|||
api_base="https://fake-resource.openai.azure.com",
|
||||
api_key="fake-api-key",
|
||||
api_version="2025-03-01-preview",
|
||||
input=expected_body["input"],
|
||||
input=call_input,
|
||||
tools=expected_body["tools"],
|
||||
tool_choice=expected_body["tool_choice"],
|
||||
max_output_tokens=expected_body["max_output_tokens"],
|
||||
|
|
|
|||
|
|
@ -141,7 +141,8 @@ async def test_opt_in_forwards_responses_natively():
|
|||
assert chat_route.call_count == 0
|
||||
request = responses_route.calls.last.request
|
||||
assert request.headers["authorization"] == "Bearer sk-backend"
|
||||
assert json.loads(request.content)["input"] == "hi"
|
||||
# string input is normalized to the canonical list before native forwarding
|
||||
assert json.loads(request.content)["input"] == [{"role": "user", "content": "hi"}]
|
||||
assert isinstance(result, ResponsesAPIResponse)
|
||||
assert result.output[0].content[0].text == "native"
|
||||
|
||||
|
|
|
|||
|
|
@ -200,7 +200,10 @@ class TestTextFormatConversion:
|
|||
), "schema should have confidence property"
|
||||
|
||||
# Validate other request parameters
|
||||
assert captured_request["input"] == "What is the capital of France?"
|
||||
# string input is normalized to the canonical list before dispatch
|
||||
assert captured_request["input"] == [
|
||||
{"role": "user", "content": "What is the capital of France?"}
|
||||
]
|
||||
|
||||
# Validate the response
|
||||
print("Response:", json.dumps(response, indent=4, default=str))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue