Skip to content

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]

[Bug]: Fix tests to reference moved attributes in `braintrust_logging` module (#13978)

Cole French669695550626
diff --git a/tests/test_litellm/integrations/test_braintrust_logging.py b/tests/test_litellm/integrations/test_braintrust_logging.py
index cca13b4e9e6..cb227148ed9 100644
--- a/tests/test_litellm/integrations/test_braintrust_logging.py
+++ b/tests/test_litellm/integrations/test_braintrust_logging.py
@@ -44,16 +44,19 @@ class TestBraintrustLogger(unittest.TestCase):
BraintrustLogger(api_key=None)
self.assertIn("Missing keys=['BRAINTRUST_API_KEY']", str(context.exception))
- @patch('litellm.integrations.braintrust_logging.global_braintrust_sync_http_handler')
- def test_log_success_event_with_default_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.HTTPHandler')
+ def test_log_success_event_with_default_span_name(self, MockHTTPHandler):
"""Test log_success_event uses default span name when not provided."""
- # Setup
- logger = BraintrustLogger(api_key="test-key")
- logger.default_project_id = "test-project-id"
-
+ # Mock HTTP response
mock_response = Mock()
mock_response.json.return_value = {"id": "test-project-id"}
+ mock_http_handler = Mock()
mock_http_handler.post.return_value = mock_response
+ MockHTTPHandler.return_value = mock_http_handler
+
+ # Setup
+ logger = BraintrustLogger(api_key="test-key")
+ logger.default_project_id = "test-project-id"
# Create a mock response object
message_mock = Mock()
@@ -62,6 +65,8 @@ class TestBraintrustLogger(unittest.TestCase):
choice_mock = Mock()
choice_mock.message = message_mock
choice_mock.dict = Mock(return_value={"message": {"content": "test"}})
+ # Mock the __getitem__ to support response_obj["choices"][0]["message"]
+ choice_mock.__getitem__ = Mock(return_value=message_mock)
response_obj = Mock(spec=litellm.ModelResponse)
response_obj.choices = [choice_mock]
@@ -90,16 +95,19 @@ class TestBraintrustLogger(unittest.TestCase):
json_data = call_args.kwargs['json']
self.assertEqual(json_data['events'][0]['span_attributes']['name'], 'Chat Completion')
- @patch('litellm.integrations.braintrust_logging.global_braintrust_sync_http_handler')
- def test_log_success_event_with_custom_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.HTTPHandler')
+ def test_log_success_event_with_custom_span_name(self, MockHTTPHandler):
"""Test log_success_event uses custom span name when provided."""
- # Setup
- logger = BraintrustLogger(api_key="test-key")
- logger.default_project_id = "test-project-id"
-
+ # Mock HTTP response
mock_response = Mock()
mock_response.json.return_value = {"id": "test-project-id"}
+ mock_http_handler = Mock()
mock_http_handler.post.return_value = mock_response
+ MockHTTPHandler.return_value = mock_http_handler
+
+ # Setup
+ logger = BraintrustLogger(api_key="test-key")
+ logger.default_project_id = "test-project-id"
# Create a mock response object
message_mock = Mock()
@@ -108,6 +116,7 @@ class TestBraintrustLogger(unittest.TestCase):
choice_mock = Mock()
choice_mock.message = message_mock
choice_mock.dict = Mock(return_value={"message": {"content": "test"}})
+ choice_mock.__getitem__ = Mock(return_value=message_mock)
response_obj = Mock(spec=litellm.ModelResponse)
response_obj.choices = [choice_mock]
@@ -135,16 +144,19 @@ class TestBraintrustLogger(unittest.TestCase):
json_data = call_args.kwargs['json']
self.assertEqual(json_data['events'][0]['span_attributes']['name'], 'Custom Operation')
- @patch('litellm.integrations.braintrust_logging.global_braintrust_http_handler')
- async def test_async_log_success_event_with_default_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.get_async_httpx_client')
+ async def test_async_log_success_event_with_default_span_name(self, mock_get_http_handler):
"""Test async_log_success_event uses default span name when not provided."""
- # Setup
- logger = BraintrustLogger(api_key="test-key")
- logger.default_project_id = "test-project-id"
-
+ # Mock async HTTP response
mock_response = Mock()
mock_response.json.return_value = {"id": "test-project-id"}
+ mock_http_handler = MagicMock()
mock_http_handler.post = MagicMock(return_value=mock_response)
+ mock_get_http_handler.return_value = mock_http_handler
+
+ # Setup
+ logger = BraintrustLogger(api_key="test-key")
+ logger.default_project_id = "test-project-id"
# Create a mock response object
message_mock = Mock()
@@ -153,6 +165,7 @@ class TestBraintrustLogger(unittest.TestCase):
choice_mock = Mock()
choice_mock.message = message_mock
choice_mock.dict = Mock(return_value={"message": {"content": "test"}})
+ choice_mock.__getitem__ = Mock(return_value=message_mock)
response_obj = Mock(spec=litellm.ModelResponse)
response_obj.choices = [choice_mock]
@@ -180,16 +193,19 @@ class TestBraintrustLogger(unittest.TestCase):
json_data = call_args.kwargs['json']
self.assertEqual(json_data['events'][0]['span_attributes']['name'], 'Chat Completion')
- @patch('litellm.integrations.braintrust_logging.global_braintrust_http_handler')
- async def test_async_log_success_event_with_custom_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.get_async_httpx_client')
+ async def test_async_log_success_event_with_custom_span_name(self, mock_get_http_handler):
"""Test async_log_success_event uses custom span name when provided."""
- # Setup
- logger = BraintrustLogger(api_key="test-key")
- logger.default_project_id = "test-project-id"
-
+ # Mock async HTTP response
mock_response = Mock()
mock_response.json.return_value = {"id": "test-project-id"}
+ mock_http_handler = MagicMock()
mock_http_handler.post = MagicMock(return_value=mock_response)
+ mock_get_http_handler.return_value = mock_http_handler
+
+ # Setup
+ logger = BraintrustLogger(api_key="test-key")
+ logger.default_project_id = "test-project-id"
# Create a mock response object
message_mock = Mock()
@@ -198,6 +214,7 @@ class TestBraintrustLogger(unittest.TestCase):
choice_mock = Mock()
choice_mock.message = message_mock
choice_mock.dict = Mock(return_value={"message": {"content": "test"}})
+ choice_mock.__getitem__ = Mock(return_value=message_mock)
response_obj = Mock(spec=litellm.ModelResponse)
response_obj.choices = [choice_mock]
@@ -225,16 +242,19 @@ class TestBraintrustLogger(unittest.TestCase):
json_data = call_args.kwargs['json']
self.assertEqual(json_data['events'][0]['span_attributes']['name'], 'Async Custom Operation')
- @patch('litellm.integrations.braintrust_logging.global_braintrust_sync_http_handler')
- def test_span_name_with_multiple_metadata_fields(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.HTTPHandler')
+ def test_span_name_with_multiple_metadata_fields(self, MockHTTPHandler):
"""Test that span_name works correctly alongside other metadata fields."""
- # Setup
- logger = BraintrustLogger(api_key="test-key")
- logger.default_project_id = "test-project-id"
-
+ # Mock HTTP response
mock_response = Mock()
mock_response.json.return_value = {"id": "test-project-id"}
+ mock_http_handler = Mock()
mock_http_handler.post.return_value = mock_response
+ MockHTTPHandler.return_value = mock_http_handler
+
+ # Setup
+ logger = BraintrustLogger(api_key="test-key")
+ logger.default_project_id = "test-project-id"
# Create a mock response object
message_mock = Mock()
@@ -243,6 +263,7 @@ class TestBraintrustLogger(unittest.TestCase):
choice_mock = Mock()
choice_mock.message = message_mock
choice_mock.dict = Mock(return_value={"message": {"content": "test"}})
+ choice_mock.__getitem__ = Mock(return_value=message_mock)
response_obj = Mock(spec=litellm.ModelResponse)
response_obj.choices = [choice_mock]
diff --git a/tests/test_litellm/integrations/test_braintrust_span_name.py b/tests/test_litellm/integrations/test_braintrust_span_name.py
index d3d98ea70af..10e512fc0ca 100644
--- a/tests/test_litellm/integrations/test_braintrust_span_name.py
+++ b/tests/test_litellm/integrations/test_braintrust_span_name.py
@@ -11,16 +11,18 @@ from litellm.integrations.braintrust_logging import BraintrustLogger
class TestBraintrustSpanName(unittest.TestCase):
"""Test custom span_name functionality in Braintrust logging."""
- @patch('litellm.integrations.braintrust_logging.global_braintrust_sync_http_handler')
- def test_default_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.HTTPHandler')
+ def test_default_span_name(self, MockHTTPHandler):
"""Test that default span name is 'Chat Completion' when not provided."""
+ # Mock HTTP response
+ mock_http_handler = Mock()
+ mock_http_handler.post.return_value = Mock()
+ MockHTTPHandler.return_value = mock_http_handler
+
# Setup
logger = BraintrustLogger(api_key="test-key")
logger.default_project_id = "test-project-id"
- # Mock HTTP response
- mock_http_handler.post.return_value = Mock()
-
# Create a properly structured mock response
response_obj = litellm.ModelResponse(
id="test-id",
@@ -52,16 +54,18 @@ class TestBraintrustSpanName(unittest.TestCase):
json_data = call_args.kwargs['json']
self.assertEqual(json_data['events'][0]['span_attributes']['name'], 'Chat Completion')
- @patch('litellm.integrations.braintrust_logging.global_braintrust_sync_http_handler')
- def test_custom_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.HTTPHandler')
+ def test_custom_span_name(self, MockHTTPHandler):
"""Test that custom span name is used when provided in metadata."""
+ # Mock HTTP response
+ mock_http_handler = Mock()
+ mock_http_handler.post.return_value = Mock()
+ MockHTTPHandler.return_value = mock_http_handler
+
# Setup
logger = BraintrustLogger(api_key="test-key")
logger.default_project_id = "test-project-id"
- # Mock HTTP response
- mock_http_handler.post.return_value = Mock()
-
# Create a properly structured mock response
response_obj = litellm.ModelResponse(
id="test-id",
@@ -93,16 +97,18 @@ class TestBraintrustSpanName(unittest.TestCase):
json_data = call_args.kwargs['json']
self.assertEqual(json_data['events'][0]['span_attributes']['name'], 'Custom Operation')
- @patch('litellm.integrations.braintrust_logging.global_braintrust_sync_http_handler')
- def test_span_name_with_other_metadata(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.HTTPHandler')
+ def test_span_name_with_other_metadata(self, MockHTTPHandler):
"""Test that span_name works alongside other metadata fields."""
+ # Mock HTTP response
+ mock_http_handler = Mock()
+ mock_http_handler.post.return_value = Mock()
+ MockHTTPHandler.return_value = mock_http_handler
+
# Setup
logger = BraintrustLogger(api_key="test-key")
logger.default_project_id = "test-project-id"
- # Mock HTTP response
- mock_http_handler.post.return_value = Mock()
-
# Create a properly structured mock response
response_obj = litellm.ModelResponse(
id="test-id",
@@ -153,16 +159,18 @@ class TestBraintrustSpanName(unittest.TestCase):
# Span name should be in span_attributes, not in metadata
self.assertIn('span_name', event_metadata) # span_name is also kept in metadata
- @patch('litellm.integrations.braintrust_logging.global_braintrust_http_handler')
- async def test_async_custom_span_name(self, mock_http_handler):
+ @patch('litellm.integrations.braintrust_logging.get_async_httpx_client')
+ async def test_async_custom_span_name(self, mock_get_http_handler):
"""Test async logging with custom span name."""
+ # Mock async HTTP response
+ mock_http_handler = MagicMock()
+ mock_http_handler.post = MagicMock(return_value=Mock())
+ mock_get_http_handler.return_value = mock_http_handler
+
# Setup
logger = BraintrustLogger(api_key="test-key")
logger.default_project_id = "test-project-id"
- # Mock async HTTP response
- mock_http_handler.post = MagicMock(return_value=Mock())
-
# Create a properly structured mock response
response_obj = litellm.ModelResponse(
id="test-id",