Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(perplexity): restore type='message' safety net for list inputs

Chesarsda76e1738a8e
diff --git a/litellm/llms/perplexity/responses/transformation.py b/litellm/llms/perplexity/responses/transformation.py
index 233b3684953..f365ef07a61 100644
--- a/litellm/llms/perplexity/responses/transformation.py
+++ b/litellm/llms/perplexity/responses/transformation.py
@@ -9,7 +9,7 @@ The only provider quirks:
Ref: https://docs.perplexity.ai/api-reference/responses-post
"""
-from typing import Dict, Optional, Union
+from typing import Any, Dict, List, Optional, Union
import httpx
@@ -58,6 +58,21 @@ class PerplexityResponsesConfig(OpenAIResponsesAPIConfig):
api_base = api_base or get_secret_str("PERPLEXITY_API_BASE") or "https://api.perplexity.ai"
return f"{api_base.rstrip('/')}/v1/responses"
+ def _ensure_message_type(
+ self, input: Union[str, ResponseInputParam]
+ ) -> Union[str, List[Dict[str, Any]]]:
+ """Ensure list input items have type='message' (required by Perplexity)."""
+ if isinstance(input, str):
+ return input
+ if isinstance(input, list):
+ result = []
+ for item in input:
+ if isinstance(item, dict) and "type" not in item:
+ item = {**item, "type": "message"}
+ result.append(item)
+ return result
+ return input
+
def transform_responses_api_request(
self,
model: str,
@@ -67,6 +82,7 @@ class PerplexityResponsesConfig(OpenAIResponsesAPIConfig):
headers: dict,
) -> Dict:
"""Handle preset/ model prefix: send as {"preset": name} instead of {"model": name}."""
+ input = self._ensure_message_type(input)
if model.startswith("preset/"):
input = self._validate_input_param(input)
data: Dict = {
diff --git a/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py b/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py
index 84299b5bb28..a3ec81c569c 100644
--- a/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py
+++ b/tests/test_litellm/llms/perplexity/responses/test_perplexity_responses_transformation.py
@@ -336,6 +336,85 @@ class TestPerplexityResponsesTransformation:
assert data["input"] == "What is AI?"
assert "temperature" in data
+ def test_preset_handling_list_input(self):
+ """Preset with list input preserves type field"""
+ config = PerplexityResponsesConfig()
+
+ list_input = [
+ {"type": "message", "role": "user", "content": "What is AI?"},
+ ]
+
+ data = config.transform_responses_api_request(
+ model="preset/pro-search",
+ input=list_input,
+ response_api_optional_request_params={"temperature": 0.7},
+ litellm_params={},
+ headers={},
+ )
+
+ assert data["preset"] == "pro-search"
+ assert isinstance(data["input"], list)
+ assert data["input"][0]["type"] == "message"
+ assert data["input"][0]["role"] == "user"
+
+ def test_non_preset_list_input(self):
+ """Non-preset with list input preserves type field"""
+ config = PerplexityResponsesConfig()
+
+ list_input = [
+ {"type": "message", "role": "user", "content": "Hello"},
+ ]
+
+ data = config.transform_responses_api_request(
+ model="openai/gpt-5.2",
+ input=list_input,
+ response_api_optional_request_params={},
+ litellm_params={},
+ headers={},
+ )
+
+ assert data["model"] == "openai/gpt-5.2"
+ assert isinstance(data["input"], list)
+ assert data["input"][0]["type"] == "message"
+
+ def test_list_input_adds_type_message_when_missing(self):
+ """Input items without type get type='message' added automatically"""
+ config = PerplexityResponsesConfig()
+
+ list_input = [
+ {"role": "user", "content": "Hello"},
+ ]
+
+ data = config.transform_responses_api_request(
+ model="openai/gpt-5.2",
+ input=list_input,
+ response_api_optional_request_params={},
+ litellm_params={},
+ headers={},
+ )
+
+ assert data["input"][0]["type"] == "message"
+ assert data["input"][0]["role"] == "user"
+ assert data["input"][0]["content"] == "Hello"
+
+ def test_list_input_preserves_existing_type(self):
+ """Input items that already have type are not modified"""
+ config = PerplexityResponsesConfig()
+
+ list_input = [
+ {"type": "function_call_output", "call_id": "123", "output": "{}"},
+ ]
+
+ data = config.transform_responses_api_request(
+ model="openai/gpt-5.2",
+ input=list_input,
+ response_api_optional_request_params={},
+ litellm_params={},
+ headers={},
+ )
+
+ assert data["input"][0]["type"] == "function_call_output"
+
def test_get_complete_url(self):
"""Correct endpoint URL"""
config = PerplexityResponsesConfig()