async def test_rag_with_malformed_search_results(self, mock_providers):
        """Test RAG behavior with malformed search results."""
        class RAGPromptBuilder:
            def __init__(self, providers):
                self.providers = providers

            async def build_prompt(self, query, search_results, system_prompt_template_id=None):
                # Handle malformed results by including whatever is available
                chunks = search_results.get("chunk_search_results", [])

                context = ""
                for chunk in chunks:
                    # Handle missing fields gracefully
                    text = chunk.get("text", "No text content")
                    context += text + "\n\n"

                return [
                    {"role": "system", "content": f"Context:\n{context}\n\nBased on the above context, answer the following question."},
                    {"role": "user", "content": query}
                ]

        # Create a RAG prompt builder
        builder = RAGPromptBuilder(providers=mock_providers)

        # Setup malformed search results (missing required fields)
        malformed_search_results = {
            "chunk_search_results": [
                {
                    # Missing chunk_id, document_id
                    "text": "Malformed result without required fields"
                    # Missing metadata
                }
            ]
        }

        # Call the build method with malformed results
        query = "What did Aristotle say about ethics?"
        messages = await builder.build_prompt(
            query=query,
            search_results=malformed_search_results,
            system_prompt_template_id="default_rag_prompt"
        )

        # Find the system message
        system_message = next((m for m in messages if m["role"] == "system"), None)

        # Check that the system message handles malformed results gracefully
        assert system_message is not None, "System message should be present even with malformed results"
        assert "Malformed result" in system_message["content"], \
               "The text content should still be included"
Test RAG behavior with malformed search results.
py/tests/unit/retrieval/test_rag_processing.py