async def test_rag_with_llm_error_recovery(self, mock_providers, mock_search_results):
        """Test RAG recovery from LLM errors."""
        class RAGProcessorWithErrorRecovery:
            def __init__(self, providers):
                self.providers = providers
                self.prompt_builder = MagicMock()
                self.prompt_builder.build_prompt = AsyncMock(
                    return_value=[
                        {"role": "system", "content": "System prompt with context"},
                        {"role": "user", "content": "What did Aristotle say about ethics?"}
                    ]
                )

                # Configure the LLM mock to fail on first call, succeed on second
                self.providers.llm.aget_completion = AsyncMock(side_effect=[
                    Exception("LLM API error"),
                    {"choices": [{"message": {"content": "Fallback response after error"}}]}
                ])

            async def generate_with_error_recovery(self, query, search_results, **kwargs):
                # Build the prompt
                messages = await self.prompt_builder.build_prompt(
                    query=query,
                    search_results=search_results,
                    **kwargs
                )

                # Try with primary model
                try:
                    response = await self.providers.llm.aget_completion(
                        messages=messages,
                        model="primary_model"
                    )
                    return response["choices"][0]["message"]["content"]
                except Exception as e:
                    # On error, try with fallback model
                    response = await self.providers.llm.aget_completion(
                        messages=messages,
                        model="fallback_model"
                    )
                    return response["choices"][0]["message"]["content"]

        # Create the processor
        processor = RAGProcessorWithErrorRecovery(mock_providers)

        # Generate a response with error recovery
        query = "What did Aristotle say about ethics?"
        response = await processor.generate_with_error_recovery(
            query=query,
            search_results=mock_search_results
        )

        # Verify both LLM calls were made
        assert mock_providers.llm.aget_completion.call_count == 2

        # Check the second call used the fallback model
        second_call_kwargs = mock_providers.llm.aget_completion.call_args_list[1][1]
        assert second_call_kwargs["model"] == "fallback_model"

        # Check the response is from the fallback
        assert response == "Fallback response after error"
Test RAG recovery from LLM errors.
py/tests/unit/retrieval/test_rag_processing.py