|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +from any_llm.providers.fireworks.utils import extract_reasoning_from_response |
| 4 | +from any_llm.types.completion import Reasoning |
| 5 | +from any_llm.types.responses import Response |
| 6 | + |
| 7 | + |
| 8 | +def test_extract_reasoning_from_response_with_think_tags() -> None: |
| 9 | + """Test that <think> content is correctly extracted into reasoning field.""" |
| 10 | + # Create a mock Response with <think> tags in content |
| 11 | + mock_content = Mock() |
| 12 | + mock_content.text = "<think>This is my reasoning process</think>This is the actual response" |
| 13 | + |
| 14 | + mock_output_item = Mock() |
| 15 | + mock_output_item.content = [mock_content] |
| 16 | + |
| 17 | + mock_response = Mock(spec=Response) |
| 18 | + mock_response.output = [mock_output_item] |
| 19 | + mock_response.reasoning = None |
| 20 | + |
| 21 | + result = extract_reasoning_from_response(mock_response) |
| 22 | + |
| 23 | + assert result.reasoning is not None |
| 24 | + assert isinstance(result.reasoning, Reasoning) |
| 25 | + assert result.reasoning.content == "This is my reasoning process" |
| 26 | + assert mock_content.text == "This is the actual response" |
| 27 | + |
| 28 | + |
| 29 | +def test_extract_reasoning_from_response_without_think_tags() -> None: |
| 30 | + """Test that responses without <think> tags are returned unchanged.""" |
| 31 | + mock_content = Mock() |
| 32 | + mock_content.text = "This is just a regular response" |
| 33 | + |
| 34 | + mock_output_item = Mock() |
| 35 | + mock_output_item.content = [mock_content] |
| 36 | + |
| 37 | + mock_response = Mock(spec=Response) |
| 38 | + mock_response.output = [mock_output_item] |
| 39 | + mock_response.reasoning = None |
| 40 | + |
| 41 | + result = extract_reasoning_from_response(mock_response) |
| 42 | + |
| 43 | + assert result.reasoning is None |
| 44 | + assert mock_content.text == "This is just a regular response" |
| 45 | + |
| 46 | + |
| 47 | +def test_extract_reasoning_from_response_empty_reasoning() -> None: |
| 48 | + """Test that empty reasoning content is handled correctly.""" |
| 49 | + mock_content = Mock() |
| 50 | + mock_content.text = "<think></think>This is the actual response" |
| 51 | + |
| 52 | + mock_output_item = Mock() |
| 53 | + mock_output_item.content = [mock_content] |
| 54 | + |
| 55 | + mock_response = Mock(spec=Response) |
| 56 | + mock_response.output = [mock_output_item] |
| 57 | + mock_response.reasoning = None |
| 58 | + |
| 59 | + result = extract_reasoning_from_response(mock_response) |
| 60 | + |
| 61 | + assert result.reasoning is None |
| 62 | + assert mock_content.text == "This is the actual response" |
| 63 | + |
| 64 | + |
| 65 | +def test_extract_reasoning_from_response_empty_output() -> None: |
| 66 | + """Test that responses with empty output are handled gracefully.""" |
| 67 | + mock_response = Mock(spec=Response) |
| 68 | + mock_response.output = [] |
| 69 | + mock_response.reasoning = None |
| 70 | + |
| 71 | + result = extract_reasoning_from_response(mock_response) |
| 72 | + |
| 73 | + assert result.reasoning is None |
| 74 | + assert result == mock_response |
0 commit comments