@@ -163,3 +163,42 @@ class BraintrustEnv:
163163 ALL_PUBLISH_PAYLOADS_DIR = EnvVar ("BRAINTRUST_ALL_PUBLISH_PAYLOADS_DIR" , EnvParser .STRING )
164164 DISABLE_ATEXIT_FLUSH = EnvVar ("BRAINTRUST_DISABLE_ATEXIT_FLUSH" , EnvParser .BOOL )
165165 OTEL_COMPAT = EnvVar ("BRAINTRUST_OTEL_COMPAT" , EnvParser .BOOL )
166+ # Opt out of the default OpenTelemetry-compatible hex span/trace IDs and use
167+ # legacy UUID-based IDs (and V3 span-component export) instead.
168+ LEGACY_UUID_IDS = EnvVar ("BRAINTRUST_LEGACY_UUID_IDS" , EnvParser .BOOL )
169+
170+
171+ class BraintrustEnvError (Exception ):
172+ """Raised when Braintrust environment variables are configured inconsistently."""
173+
174+
175+ def use_legacy_uuid_ids () -> bool :
176+ """Return True if the SDK should generate legacy UUID-based span/trace IDs.
177+
178+ The default is OpenTelemetry-compatible hex IDs (16-byte trace id / 8-byte
179+ span id) with V4 span-component export. Setting BRAINTRUST_LEGACY_UUID_IDS
180+ opts back into UUID IDs with V3 export.
181+
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().
186+ """
187+ validate_id_config ()
188+ if BraintrustEnv .OTEL_COMPAT .get (False ):
189+ 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+ )
0 commit comments