Skip to content

Commit 8e49cec

Browse files
committed
fixup
1 parent c1112b2 commit 8e49cec

5 files changed

Lines changed: 27 additions & 55 deletions

File tree

py/src/braintrust/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,6 @@ def is_equal(expected, output):
5151
# Check env var at import time for auto-instrumentation
5252
import os
5353

54-
from .env import validate_id_config as _validate_id_config
55-
56-
57-
# Fail fast on mutually-exclusive ID configuration (e.g. BRAINTRUST_OTEL_COMPAT
58-
# together with BRAINTRUST_LEGACY_UUID_IDS) as early as possible on import.
59-
_validate_id_config()
60-
6154

6255
if os.getenv("BRAINTRUST_INSTRUMENT_THREADS", "").lower() in ("true", "1", "yes"):
6356
try:

py/src/braintrust/context.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from dataclasses import dataclass
77
from typing import Any
88

9-
from .env import BraintrustEnv, validate_id_config
9+
from .env import BraintrustEnv
1010

1111

1212
@dataclass
@@ -120,10 +120,6 @@ def get_context_manager() -> ContextManager:
120120
Braintrust-only context manager by default.
121121
"""
122122

123-
# Fail fast if the ID configuration is inconsistent (belt-and-suspenders for
124-
# the import-time check, in case env vars were mutated after import).
125-
validate_id_config()
126-
127123
# Check if OTEL should be explicitly enabled via environment variable
128124
if BraintrustEnv.OTEL_COMPAT.get(False):
129125
try:

py/src/braintrust/env.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import io
2+
import logging
23
import math
34
import os
45
import shlex
@@ -8,6 +9,9 @@
89
from typing import TypeVar, cast
910

1011

12+
_logger = logging.getLogger(__name__)
13+
14+
1115
T = TypeVar("T")
1216
EnvValue = bool | float | int | str
1317
_Parser = Callable[[str], EnvValue | None]
@@ -168,37 +172,23 @@ class BraintrustEnv:
168172
LEGACY_UUID_IDS = EnvVar("BRAINTRUST_LEGACY_UUID_IDS", EnvParser.BOOL)
169173

170174

171-
class BraintrustEnvError(Exception):
172-
"""Raised when Braintrust environment variables are configured inconsistently."""
173-
174-
175175
def use_legacy_uuid_ids() -> bool:
176176
"""Return True if the SDK should generate legacy UUID-based span/trace IDs.
177177
178178
The default is OpenTelemetry-compatible hex IDs (16-byte trace id / 8-byte
179179
span id) with V4 span-component export. Setting BRAINTRUST_LEGACY_UUID_IDS
180180
opts back into UUID IDs with V3 export.
181181
182-
Note: BRAINTRUST_OTEL_COMPAT (which selects the OpenTelemetry context
183-
manager) requires hex IDs, so it always forces hex regardless of this
184-
function. The mutually-exclusive combination is rejected by
185-
validate_id_config().
182+
BRAINTRUST_OTEL_COMPAT (which selects the OpenTelemetry context manager)
183+
requires hex IDs, so it always wins: if both it and BRAINTRUST_LEGACY_UUID_IDS
184+
are set, legacy IDs are disabled and a warning is logged.
186185
"""
187-
validate_id_config()
186+
legacy = BraintrustEnv.LEGACY_UUID_IDS.get(False)
188187
if BraintrustEnv.OTEL_COMPAT.get(False):
188+
if legacy:
189+
_logger.warning(
190+
"BRAINTRUST_LEGACY_UUID_IDS is ignored because BRAINTRUST_OTEL_COMPAT "
191+
"requires OpenTelemetry-compatible hex span IDs. Using hex IDs."
192+
)
189193
return False
190-
return BraintrustEnv.LEGACY_UUID_IDS.get(False)
191-
192-
193-
def validate_id_config() -> None:
194-
"""Fail fast on mutually-exclusive ID configuration.
195-
196-
BRAINTRUST_OTEL_COMPAT requires OpenTelemetry-compatible hex IDs, while
197-
BRAINTRUST_LEGACY_UUID_IDS forces legacy UUID IDs. They cannot both be set.
198-
"""
199-
if BraintrustEnv.OTEL_COMPAT.get(False) and BraintrustEnv.LEGACY_UUID_IDS.get(False):
200-
raise BraintrustEnvError(
201-
"BRAINTRUST_OTEL_COMPAT and BRAINTRUST_LEGACY_UUID_IDS are mutually "
202-
"exclusive: OTEL compatibility requires hex span IDs, but legacy mode "
203-
"forces UUID span IDs. Unset one of them."
204-
)
194+
return legacy

py/src/braintrust/test_env.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22

33
from .env import (
44
BraintrustEnv,
5-
BraintrustEnvError,
65
EnvParser,
76
EnvVar,
87
parse_bool,
98
parse_float,
109
parse_int,
1110
parse_string,
1211
use_legacy_uuid_ids,
13-
validate_id_config,
1412
)
1513

1614

@@ -168,20 +166,13 @@ def test_otel_compat_forces_hex(self, monkeypatch):
168166
monkeypatch.delenv("BRAINTRUST_LEGACY_UUID_IDS", raising=False)
169167
assert use_legacy_uuid_ids() is False
170168

171-
def test_conflicting_flags_raise(self, monkeypatch):
172-
monkeypatch.setenv("BRAINTRUST_OTEL_COMPAT", "true")
173-
monkeypatch.setenv("BRAINTRUST_LEGACY_UUID_IDS", "true")
174-
with pytest.raises(BraintrustEnvError):
175-
validate_id_config()
176-
with pytest.raises(BraintrustEnvError):
177-
use_legacy_uuid_ids()
169+
def test_conflicting_flags_otel_wins_with_warning(self, monkeypatch, caplog):
170+
# OTEL_COMPAT wins over LEGACY_UUID_IDS: legacy is disabled (hex IDs) and
171+
# a warning is logged rather than raising.
172+
import logging
178173

179-
def test_non_conflicting_flags_pass(self, monkeypatch):
180-
# Each flag alone is fine.
181174
monkeypatch.setenv("BRAINTRUST_OTEL_COMPAT", "true")
182-
monkeypatch.delenv("BRAINTRUST_LEGACY_UUID_IDS", raising=False)
183-
validate_id_config()
184-
185-
monkeypatch.delenv("BRAINTRUST_OTEL_COMPAT", raising=False)
186175
monkeypatch.setenv("BRAINTRUST_LEGACY_UUID_IDS", "true")
187-
validate_id_config()
176+
with caplog.at_level(logging.WARNING):
177+
assert use_legacy_uuid_ids() is False
178+
assert any("BRAINTRUST_LEGACY_UUID_IDS" in r.message for r in caplog.records)

py/src/braintrust/test_id_gen.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,13 @@ def test_id_get_env_var_legacy_uuid(reset_id_generator_state):
9898
assert_func(generator.get_trace_id())
9999

100100

101-
def test_otel_compat_and_legacy_uuid_conflict_raises(reset_id_generator_state):
101+
def test_otel_compat_wins_over_legacy_uuid(reset_id_generator_state):
102+
# When both are set, OTEL_COMPAT wins: hex IDs are used (no raise).
102103
os.environ["BRAINTRUST_OTEL_COMPAT"] = "true"
103104
os.environ["BRAINTRUST_LEGACY_UUID_IDS"] = "true"
104-
with pytest.raises(Exception):
105-
id_gen.get_id_generator()
105+
generator = id_gen.get_id_generator()
106+
assert generator.share_root_span_id() is False
107+
_assert_is_hex(generator.get_span_id())
106108

107109

108110
def _is_hex(s):

0 commit comments

Comments
 (0)