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]

feat(integrations): allow setting of braintrust callback base url (#13368)

Edward D'Amato30fc5b871ce5
diff --git a/docs/my-website/docs/observability/braintrust.md b/docs/my-website/docs/observability/braintrust.md
index 79f3cf13be2..eb26680b18a 100644
--- a/docs/my-website/docs/observability/braintrust.md
+++ b/docs/my-website/docs/observability/braintrust.md
@@ -15,6 +15,7 @@ import os
# set env
os.environ["BRAINTRUST_API_KEY"] = ""
+os.environ["BRAINTRUST_API_BASE"] = "https://api.braintrustdata.com/v1"
os.environ['OPENAI_API_KEY']=""
# set braintrust as a callback, litellm will send the data to braintrust
@@ -35,6 +36,7 @@ response = litellm.completion(
```env
BRAINTRUST_API_KEY=""
+BRAINTRUST_API_BASE="https://api.braintrustdata.com/v1"
```
2. Add braintrust to callbacks
@@ -157,6 +159,8 @@ For more examples, [**Click Here**](../proxy/user_keys.md#chatcompletions)
</TabItem>
</Tabs>
+You can use `BRAINTRUST_API_BASE` to point to your self-hosted Braintrust data plane. Read more about this [here](https://www.braintrust.dev/docs/guides/self-hosting).
+
## Full API Spec
Here's everything you can pass in metadata for a braintrust request
diff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.md
index c8aee990415..3b903935a04 100644
--- a/docs/my-website/docs/proxy/config_settings.md
+++ b/docs/my-website/docs/proxy/config_settings.md
@@ -369,6 +369,7 @@ router_settings:
| BEDROCK_MAX_POLICY_SIZE | Maximum size for Bedrock policy. Default is 75
| BERRISPEND_ACCOUNT_ID | Account ID for BerriSpend service
| BRAINTRUST_API_KEY | API key for Braintrust integration
+| BRAINTRUST_API_BASE | Base URL for Braintrust API. Default is https://api.braintrustdata.com/v1
| CACHED_STREAMING_CHUNK_DELAY | Delay in seconds for cached streaming chunks. Default is 0.02
| CIRCLE_OIDC_TOKEN | OpenID Connect token for CircleCI
| CIRCLE_OIDC_TOKEN_V2 | Version 2 of the OpenID Connect token for CircleCI
diff --git a/litellm/integrations/braintrust_logging.py b/litellm/integrations/braintrust_logging.py
index c68674f77ba..8149a6131e8 100644
--- a/litellm/integrations/braintrust_logging.py
+++ b/litellm/integrations/braintrust_logging.py
@@ -42,7 +42,7 @@ class BraintrustLogger(CustomLogger):
) -> None:
super().__init__()
self.validate_environment(api_key=api_key)
- self.api_base = api_base or API_BASE
+ self.api_base = api_base or os.getenv("BRAINTRUST_API_BASE") or API_BASE
self.default_project_id = None
self.api_key: str = api_key or os.getenv("BRAINTRUST_API_KEY") # type: ignore
self.headers = {
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index b69d27775c1..17257e09c5f 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -2210,7 +2210,7 @@ class AllCallbacks(LiteLLMPydanticObjectBase):
braintrust: CallbackOnUI = CallbackOnUI(
litellm_callback_name="braintrust",
- litellm_callback_params=["BRAINTRUST_API_KEY"],
+ litellm_callback_params=["BRAINTRUST_API_KEY","BRAINTRUST_API_BASE"],
ui_callback_name="Braintrust",
)
diff --git a/tests/logging_callback_tests/test_unit_tests_init_callbacks.py b/tests/logging_callback_tests/test_unit_tests_init_callbacks.py
index 3c9d31890c7..a1760ca6371 100644
--- a/tests/logging_callback_tests/test_unit_tests_init_callbacks.py
+++ b/tests/logging_callback_tests/test_unit_tests_init_callbacks.py
@@ -33,6 +33,7 @@ expected_env_vars = {
"LAGO_API_BASE": "mock_base",
"LAGO_API_EVENT_CODE": "mock_event_code",
"OPENMETER_API_KEY": "openmeter_api_key",
+ "BRAINTRUST_API_BASE": "braintrust_api_base",
"BRAINTRUST_API_KEY": "braintrust_api_key",
"GALILEO_API_KEY": "galileo_api_key",
"LITERAL_API_KEY": "literal_api_key",
diff --git a/tests/test_litellm/integrations/test_braintrust_logging.py b/tests/test_litellm/integrations/test_braintrust_logging.py
new file mode 100644
index 00000000000..5ae40e82760
--- /dev/null
+++ b/tests/test_litellm/integrations/test_braintrust_logging.py
@@ -0,0 +1,43 @@
+import os
+import unittest
+from unittest.mock import patch
+
+from litellm.integrations.braintrust_logging import BraintrustLogger
+
+class TestBraintrustLogger(unittest.TestCase):
+ @patch.dict(os.environ, {"BRAINTRUST_API_KEY": "test-env-api-key"})
+ @patch.dict(os.environ, {"BRAINTRUST_API_BASE": "https://test-env-api.com/v1"})
+ def test_init_with_env_var(self):
+ """Test BraintrustLogger initialization with environment variable."""
+ logger = BraintrustLogger()
+ self.assertEqual(logger.api_key, "test-env-api-key")
+ self.assertEqual(logger.api_base, "https://test-env-api.com/v1")
+ self.assertEqual(logger.headers["Authorization"], "Bearer test-env-api-key")
+ self.assertEqual(logger.headers["Content-Type"], "application/json")
+
+ def test_init_with_explicit_params(self):
+ """Test BraintrustLogger initialization with explicit parameters."""
+ logger = BraintrustLogger(api_key="explicit-key", api_base="https://custom-api.com/v1")
+ self.assertEqual(logger.api_key, "explicit-key")
+ self.assertEqual(logger.api_base, "https://custom-api.com/v1")
+ self.assertEqual(logger.headers["Authorization"], "Bearer explicit-key")
+
+ @patch.dict(os.environ, {}, clear=True)
+ def test_init_missing_api_key(self):
+ """Test BraintrustLogger initialization fails without API key."""
+ with self.assertRaises(Exception) as context:
+ BraintrustLogger()
+ self.assertIn("Missing keys=['BRAINTRUST_API_KEY']", str(context.exception))
+
+ def test_validate_environment_with_api_key(self):
+ """Test validate_environment method with valid API key."""
+ logger = BraintrustLogger(api_key="test-key")
+ # Should not raise an exception
+ logger.validate_environment(api_key="test-key")
+
+ def test_validate_environment_missing_api_key(self):
+ """Test validate_environment method with missing API key."""
+ with patch.dict(os.environ, {}, clear=True):
+ with self.assertRaises(Exception) as context:
+ BraintrustLogger(api_key=None)
+ self.assertIn("Missing keys=['BRAINTRUST_API_KEY']", str(context.exception))
\ No newline at end of file