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]

Merge branch 'main' into litellm_responses_structured_output

Krish Dholakia94c1b21ae763
diff --cc tests/llm_responses_api_testing/base_responses_api.py
index f254a197aaf,d5d43e175c0..2939e884a56
--- a/tests/llm_responses_api_testing/base_responses_api.py
+++ b/tests/llm_responses_api_testing/base_responses_api.py
@@@ -529,136 -537,57 +529,191 @@@ class BaseResponsesAPITest(ABC)
validate_responses_api_response(final_response, final_chunk=True)
assert final_response.output is not None
+ @pytest.mark.asyncio
+ async def test_text_format_to_text_conversion(self):
+ """
+ Test that when text_format parameter is passed to litellm.aresponses,
+ it gets converted to text parameter in the raw API call to OpenAI.
+ """
+ from unittest.mock import AsyncMock, patch
+
+ from pydantic import BaseModel
+
+ class TestResponse(BaseModel):
+ """Test Pydantic model for structured output"""
+
+ answer: str
+ confidence: float
+
+ class MockResponse:
+ """Mock response class for testing"""
+
+ def __init__(self, json_data, status_code):
+ self._json_data = json_data
+ self.status_code = status_code
+ self.text = json.dumps(json_data)
+
+ def json(self):
+ return self._json_data
+
+ # Mock response from OpenAI
+ mock_response = {
+ "id": "resp_123",
+ "object": "response",
+ "created_at": 1741476542,
+ "status": "completed",
+ "model": "gpt-4o",
+ "output": [
+ {
+ "type": "message",
+ "id": "msg_123",
+ "status": "completed",
+ "role": "assistant",
+ "content": [
+ {
+ "type": "output_text",
+ "text": '{"answer": "Paris", "confidence": 0.95}',
+ "annotations": [],
+ }
+ ],
+ }
+ ],
+ "parallel_tool_calls": True,
+ "usage": {
+ "input_tokens": 10,
+ "output_tokens": 20,
+ "total_tokens": 30,
+ "output_tokens_details": {"reasoning_tokens": 0},
+ },
+ "text": {"format": {"type": "json_object"}},
+ "error": None,
+ "incomplete_details": None,
+ "instructions": None,
+ "metadata": {},
+ "temperature": 1.0,
+ "tool_choice": "auto",
+ "tools": [],
+ "top_p": 1.0,
+ "max_output_tokens": None,
+ "previous_response_id": None,
+ "reasoning": {"effort": None, "summary": None},
+ "truncation": "disabled",
+ "user": None,
+ }
+
+ base_completion_call_args = self.get_base_completion_call_args()
+
+ with patch(
+ "litellm.llms.custom_httpx.http_handler.AsyncHTTPHandler.post",
+ new_callable=AsyncMock,
+ ) as mock_post:
+ # Configure the mock to return our response
+ mock_post.return_value = MockResponse(mock_response, 200)
+
+ litellm._turn_on_debug()
+ litellm.set_verbose = True
+
+ # Call aresponses with text_format parameter
+ response = await litellm.aresponses(
+ input="What is the capital of France?",
+ text_format=TestResponse,
+ **base_completion_call_args,
+ )
+
+ # Verify the request was made correctly
+ mock_post.assert_called_once()
+ request_body = mock_post.call_args.kwargs["json"]
+ print("Request body:", json.dumps(request_body, indent=4))
+
+ # Validate that text_format was converted to text parameter
+ assert (
+ "text" in request_body
+ ), "text parameter should be present in request body"
+ assert (
+ "text_format" not in request_body
+ ), "text_format should not be in request body"
+
+ # Validate the text parameter structure
+ text_param = request_body["text"]
+ assert "format" in text_param, "text parameter should have format field"
+ assert (
+ text_param["format"]["type"] == "json_schema"
+ ), "format type should be json_schema"
+ assert "name" in text_param["format"], "format should have name field"
+ assert (
+ text_param["format"]["name"] == "TestResponse"
+ ), "format name should match Pydantic model name"
+ assert "schema" in text_param["format"], "format should have schema field"
+ assert "strict" in text_param["format"], "format should have strict field"
+
+ # Validate the schema structure
+ schema = text_param["format"]["schema"]
+ assert schema["type"] == "object", "schema type should be object"
+ assert "properties" in schema, "schema should have properties"
+ assert (
+ "answer" in schema["properties"]
+ ), "schema should have answer property"
+ assert (
+ "confidence" in schema["properties"]
+ ), "schema should have confidence property"
+
+ # Validate other request parameters
+ assert request_body["input"] == "What is the capital of France?"
+
+ # Validate the response
+ print("Response:", json.dumps(response, indent=4, default=str))
++
+ def test_openai_responses_api_dict_input_filtering(self):
+ """
+ Test that regular dict inputs with status fields are properly filtered
+ to replicate exclude_unset=True behavior for non-Pydantic objects.
+ """
+ from litellm.llms.openai.responses.transformation import OpenAIResponsesAPIConfig
+
+ # Test input with regular dict objects (like from JSON)
+ test_input = [
+ {
+ "role": "user",
+ "content": "test"
+ },
+ {
+ "id": "rs_123",
+ "summary": [{"text": "test", "type": "summary_text"}],
+ "type": "reasoning",
+ "content": None, # Should be filtered out
+ "encrypted_content": None, # Should be filtered out
+ "status": None # Should be filtered out
+ },
+ {
+ "arguments": "{}",
+ "call_id": "call_123",
+ "name": "get_today",
+ "type": "function_call",
+ "id": "fc_123",
+ "status": "completed" # Should be preserved (not a default field)
+ }
+ ]
+
+ config = OpenAIResponsesAPIConfig()
+ validated_input = config._validate_input_param(test_input)
+
+ # Verify the results
+ assert len(validated_input) == 3
+
+ # Check reasoning item (index 1)
+ reasoning_item = validated_input[1]
+ assert reasoning_item["type"] == "reasoning"
+ assert "status" not in reasoning_item, "status field should be filtered out from reasoning item"
+ assert "content" not in reasoning_item, "content field should be filtered out from reasoning item"
+ assert "encrypted_content" not in reasoning_item, "encrypted_content field should be filtered out from reasoning item"
+ assert "id" in reasoning_item, "id field should be preserved"
+ assert "summary" in reasoning_item, "summary field should be preserved"
+
+ # Check function call item (index 2)
+ function_call_item = validated_input[2]
+ assert function_call_item["type"] == "function_call"
+ assert "status" in function_call_item, "status field should be preserved in function call item"
+ assert function_call_item["status"] == "completed", "status value should be preserved"
+
+ print("✅ OpenAI Responses API dict input filtering test passed")
+