litellm/tests/unit/interactions/test_litellm_responses_bridge.py
Mateo Wang 3f38730d2c
refactor(typing): clear about 1,100 basedpyright Any errors across 303 files (#49986)
* refactor(typing): clear basedpyright Any errors (wave 1)

* refactor(typing): clear basedpyright Any errors (wave 2)

* refactor(typing): clear basedpyright Any errors (wave 3)

* refactor(typing): clear basedpyright Any errors (wave 4)

* refactor(typing): clear basedpyright Any errors (wave 5)

* refactor(typing): clear basedpyright Any errors (wave 6)

* fix(typing): keep the cached-plan fallback's direct query_first calls so span naming resolves

* fix(logging): keep callbacks running when litellm_params is a params model

should_run_callback and the sync success and failure handlers read litellm_params through any object with a get method again, as they did before this branch, so a logging hook that returns a GenericLiteLLMParams no longer stops sync logging for that request

Adds behavior tests for the changed lines codecov listed as not run

* fix: read the regional uplift for every data residency again and reuse one dict TypeAdapter

The typed lookup only read the eu and us multipliers, so a deployment that set regional_processing_uplift_multiplier_fedramp stopped applying it. /model/update and the model write paths now share one module-level TypeAdapter instead of building one per call.

* fix: bill regional uplift multipliers float() accepts, bytes included

* fix: raise the in operator's own error from one shared json_contains, and name the bad uplift type
2026-11-06 13:31:15 -08:00

171 lines
7 KiB
Python

"""
Tests for LiteLLM Responses bridge provider.
Inherits from BaseInteractionsTest to run the same test suite against
the litellm_responses bridge provider, which calls litellm.responses() internally.
"""
from collections.abc import Mapping
from datetime import datetime, timezone
from typing import Final
import pytest
from pydantic import ValidationError
from litellm.interactions.litellm_responses_transformation.transformation import (
LiteLLMResponsesInteractionsConfig,
)
from litellm.types.interactions import Turn
from litellm.types.llms.openai import ResponsesAPIResponse
class TestBridgeInputTransformation:
"""Regression tests for translating Interactions input into Responses API input.
The bridge used to pass Google content parts through raw ({"type": "text"}),
which the Responses API rejects with a 400, and it dropped the role encoded
in step types and in the legacy "model" turn role.
"""
def test_step_input_maps_roles_and_content_types(self):
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
[
{"type": "user_input", "content": [{"type": "text", "text": "I like apples."}]},
{"type": "model_output", "content": [{"type": "text", "text": "I like oranges."}]},
{"type": "user_input", "content": [{"type": "text", "text": "What did you say?"}]},
]
)
assert transformed == [
{"role": "user", "content": [{"type": "input_text", "text": "I like apples."}]},
{"role": "assistant", "content": [{"type": "output_text", "text": "I like oranges."}]},
{"role": "user", "content": [{"type": "input_text", "text": "What did you say?"}]},
]
def test_legacy_turn_input_maps_model_role_to_assistant(self):
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
[
{"role": "user", "content": [{"type": "text", "text": "I like apples."}]},
{"role": "model", "content": [{"type": "text", "text": "I like oranges."}]},
]
)
assert transformed == [
{"role": "user", "content": [{"type": "input_text", "text": "I like apples."}]},
{"role": "assistant", "content": [{"type": "output_text", "text": "I like oranges."}]},
]
def test_turn_pydantic_model_with_string_content(self):
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
[Turn(role="model", content="I like oranges.")]
)
assert transformed == [
{"role": "assistant", "content": [{"type": "output_text", "text": "I like oranges."}]}
]
def test_string_input_passes_through(self):
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input("Hello")
assert transformed == "Hello"
def test_content_list_input_becomes_single_user_message(self):
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
[{"type": "text", "text": "Hello"}, "world"]
)
assert transformed == [
{
"role": "user",
"content": [
{"type": "input_text", "text": "Hello"},
{"type": "input_text", "text": "world"},
],
}
]
def test_non_text_content_passes_through_unchanged(self):
image_part = {"type": "image", "data": "base64data", "mime_type": "image/png"}
transformed = LiteLLMResponsesInteractionsConfig._transform_interactions_input_to_responses_input(
[{"type": "user_input", "content": [image_part]}]
)
assert transformed == [{"role": "user", "content": [image_part]}]
def _responses_api_response(status: str, usage: Mapping[str, int] | None) -> ResponsesAPIResponse:
return ResponsesAPIResponse(
id="resp_123",
created_at=1700000000,
model="gpt-4o",
object="response",
status=status,
output=[
{
"type": "message",
"id": "msg_1",
"role": "assistant",
"status": "completed",
"content": [{"type": "output_text", "text": "Hello there", "annotations": []}],
}
],
usage=usage,
)
class TestBridgeResponseTransformation:
@pytest.mark.parametrize(
("status", "usage", "model", "expected_model", "expected_usage"),
[
(
"completed",
{"input_tokens": 3, "output_tokens": 4, "total_tokens": 7},
None,
"gpt-4o",
{"total_input_tokens": 3, "total_output_tokens": 4},
),
("in_progress", None, "bridge-model", "bridge-model", None),
],
)
def test_responses_response_becomes_interaction(
self,
status: str,
usage: Mapping[str, int] | None,
model: str | None,
expected_model: str,
expected_usage: Mapping[str, int] | None,
):
interaction: Final = LiteLLMResponsesInteractionsConfig.transform_responses_response_to_interactions_response(
_responses_api_response(status, usage), model
)
assert interaction.id == "resp_123"
assert interaction.object == "interaction"
assert interaction.status == status
assert interaction.model == expected_model
assert interaction.outputs == [{"type": "text", "text": "Hello there"}]
assert interaction.steps == [{"type": "model_output", "content": [{"type": "text", "text": "Hello there"}]}]
assert interaction.usage == expected_usage
assert interaction.created is not None
assert datetime.fromisoformat(interaction.created).timestamp() == 1700000000
assert interaction.updated == interaction.created
@pytest.mark.parametrize(
("created_at", "expected_created"),
[
(datetime(2026, 1, 2, 3, 4, 5, tzinfo=timezone.utc), "2026-01-02T03:04:05+00:00"),
(None, None),
],
ids=["datetime", "missing"],
)
def test_responses_response_created_at_that_is_not_a_timestamp(
self, created_at: datetime | None, expected_created: str | None
):
reply: Final = _responses_api_response("completed", None).model_copy(update={"created_at": created_at})
interaction: Final = LiteLLMResponsesInteractionsConfig.transform_responses_response_to_interactions_response(
reply, None
)
assert interaction.created == expected_created
assert interaction.updated == expected_created
def test_responses_response_with_a_status_that_is_not_text_is_refused(self):
reply: Final = _responses_api_response("completed", None).model_copy(update={"status": 7})
with pytest.raises(ValidationError):
LiteLLMResponsesInteractionsConfig.transform_responses_response_to_interactions_response(reply, None)