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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Merge branch 'main' into litellm_fix_streaming_test
Sameer Kankuteff7bb598244b
diff --cc tests/llm_responses_api_testing/test_base_responses_api_streaming_iterator.pyindex 63f98f53d71,a0abe9d3a12..b81161881ea--- a/tests/llm_responses_api_testing/test_base_responses_api_streaming_iterator.py+++ b/tests/llm_responses_api_testing/test_base_responses_api_streaming_iterator.py@@@ -272,30 -277,84 +277,35 @@@ class TestBaseResponsesAPIStreamingIterresponse=mock_response,model="gpt-4",responses_api_provider_config=mock_config,- logging_obj=mock_logging_obj+ logging_obj=mock_logging_obj,+ litellm_metadata={"model_info": {"id": "model_123"}},+ custom_llm_provider="openai")-- # Mock _handle_failure to track if it's called- original_handle_failure = iterator._handle_failure- with patch.object(iterator, '_handle_failure', wraps=original_handle_failure) as mock_handle_failure:- # Prepare valid JSON chunk that will trigger transform_streaming_response- test_chunk_data = {- "type": "response.output_text.delta",- "delta": "Hello"++ # Create a ResponseCompletedEvent with tool_choice that has model_dump+ mock_completed_response = Mock()+ mock_completed_response.model_dump.return_value = {+ "type": "response.completed",+ "response": {+ "id": "resp_123",+ "output": [{"type": "function_call", "name": "search_web"}],+ "tool_choice": {"type": "function", "name": "search_web"}}-- # _process_chunk should raise the exception and call _handle_failure once- with pytest.raises(ValueError) as exc_info:- iterator._process_chunk(json.dumps(test_chunk_data))-- # Verify the exception was raised- assert str(exc_info.value) == "Test exception in transform"-- # Verify _handle_failure was called exactly once- assert mock_handle_failure.call_count == 1-- # Verify that calling _handle_failure again doesn't trigger duplicate handlers- # due to the _failure_handled flag- iterator._handle_failure(test_exception)- # Should still be 2 calls total (the second call is prevented by the flag)- assert mock_handle_failure.call_count == 2+ }+ # model_validate should return a new mock (the copy)+ type(mock_completed_response).model_validate = Mock(return_value=Mock())++ iterator.completed_response = mock_completed_response++ # This should NOT raise an exception+ # Previously it would fail with: TypeError: cannot pickle 'ValidatorIterator'+ # Mock asyncio.create_task and executor.submit since we're not in async context+ with patch('asyncio.create_task') as mock_create_task, \+ patch('litellm.responses.streaming_iterator.executor') as mock_executor:+ try:+ iterator._handle_logging_completed_response()+ except TypeError as e:+ if "pickle" in str(e):+ pytest.fail(f"_handle_logging_completed_response failed with pickle error: {e}")+ raise+- # Verify model_dump was called (our fix uses this instead of deepcopy)- mock_completed_response.model_dump.assert_called_once()- def test_process_chunk_exception_does_not_call_handle_failure(self):- """- Test that _process_chunk raises exceptions without calling _handle_failure.-- This ensures _handle_failure is only called once in the outer exception handler- (in __next__ or __anext__), preventing duplicate failure logging.-- Previously, _handle_failure was called both in _process_chunk and in the outer- exception handler, causing duplicate logs. This test verifies the fix.- """- # Mock dependencies- mock_response = Mock()- mock_response.headers = {}- mock_logging_obj = Mock(spec=LiteLLMLoggingObj)- mock_logging_obj.model_call_details = {"litellm_params": {}}- mock_config = Mock(spec=BaseResponsesAPIConfig)-- # Set up the mock transform method to raise an exception- test_exception = ValueError("Test exception in transform")- mock_config.transform_streaming_response.side_effect = test_exception-- # Create the iterator instance- iterator = BaseResponsesAPIStreamingIterator(- response=mock_response,- model="gpt-4",- responses_api_provider_config=mock_config,- logging_obj=mock_logging_obj- )-- # Mock _handle_failure to track if it's called- with patch.object(iterator, '_handle_failure') as mock_handle_failure:- # Prepare valid JSON chunk that will trigger transform_streaming_response- test_chunk_data = {- "type": "response.output_text.delta",- "delta": "Hello"- }-- # _process_chunk should raise the exception without calling _handle_failure- with pytest.raises(ValueError) as exc_info:- iterator._process_chunk(json.dumps(test_chunk_data))-- # Verify the exception was raised- assert str(exc_info.value) == "Test exception in transform"-- # Verify _handle_failure was NOT called in _process_chunk- # It should only be called by the outer exception handler in __next__/__anext__- mock_handle_failure.assert_not_called()