async def test_citation_handling_with_empty_response():
    """Test how the agent handles responses with no citations."""
    # Create a custom R2RStreamingAgent with no citations

    # Custom agent class for testing empty citations
    class EmptyResponseAgent(MockR2RStreamingAgent):
        async def arun(
            self,
            system_instruction: str = None,
            messages: list[Message] = None,
            *args,
            **kwargs,
        ) -> AsyncGenerator[str, None]:
            """Custom arun with no citations in the response."""
            await self._setup(system_instruction)

            if messages:
                for m in messages:
                    await self.conversation.add_message(m)

            # Initialize citation tracker
            citation_tracker = CitationTracker()

            # Empty response with no citations
            response_content = "This is a response with no citations."

            # Yield an initial message event with the start of the text
            yield self._format_sse_event("message", {"content": response_content})

            # No citation spans to extract
            citation_spans = extract_citation_spans(response_content)

            # Should be empty
            assert len(citation_spans) == 0, "No citation spans should be found"

            # Add assistant message to conversation (with no citation metadata)
            await self.conversation.add_message(
                Message(
                    role="assistant",
                    content=response_content,
                    metadata={"citations": []}
                )
            )

            # Create and emit final answer event
            final_evt_payload = {
                "id": "msg_final",
                "object": "agent.final_answer",
                "generated_answer": response_content,
                "citations": []
            }

            yield self._format_sse_event("agent.final_answer", final_evt_payload)
            yield "event: done\ndata: {}\n\n"

    # Create the agent with empty citation response
    config = MagicMock()
    config.stream = True

    llm_provider = MockLLMProvider(
        response_content="This is a response with no citations.",
        citations=[]
    )

    db_provider = MockDatabaseProvider()

    # Create the custom agent
    agent = EmptyResponseAgent(
        database_provider=db_provider,
        llm_provider=llm_provider,
        config=config,
        rag_generation_config=GenerationConfig(model="test/model")
    )

    # Test a simple query
    messages = [Message(role="user", content="Query with no citations")]

    # Run the agent
    stream = agent.arun(messages=messages)
    output = await collect_stream_output(stream)

    # Verify no citation events were emitted
    citation_events = [line for line in output if 'event: citation' in line]
    assert len(citation_events) == 0, "No citation events should be emitted"

    # Parse the final answer event to check citations
    final_answer_events = [line for line in output if 'event: agent.final_answer' in line]
    assert len(final_answer_events) > 0, "Final answer event should be emitted"

    data_part = final_answer_events[0].split('data: ')[1] if 'data: ' in final_answer_events[0] else ""

    # Parse final answer data
    try:
        data = json.loads(data_part)
        assert 'citations' in data, "Final answer event should include citations field"
        assert len(data['citations']) == 0, "Citations list should be empty"
    except json.JSONDecodeError:
        assert False, "Final answer event data should be valid JSON"
Test how the agent handles responses with no citations.
py/tests/unit/agent/test_agent_citations.py