test(langfuse): assert sha256 trace-id contract for non-hex proxy metadata

The repro asserted the exported OTEL span-context trace id equals the raw
external id global_20260225143025_ord2468, which can never hold: OTEL trace
ids are 32 lowercase hex chars. Langfuse derives a trace's identity from the
OTEL span-context trace id, and its SDK maps arbitrary external ids onto a
valid trace id via sha256, so assert the span context carries
sha256(id).digest()[:16].hex() (computed independently in the test).
This commit is contained in:
Dhanush Reddy Chilakala 2026-06-26 00:36:00 -05:00
parent e318c6982c
commit 5ffa43fb0c
No known key found for this signature in database
GPG key ID: 51C52D74B4BAB2DC

View file

@ -245,7 +245,16 @@ class TestLangfuseOtelIntegration:
), "Mismatch between expected and actual OTEL attribute mapping."
def test_openai_extra_body_trace_id_controls_langfuse_otel_trace_identity(self):
"""A non-hex ``metadata.trace_id`` from ``extra_body`` must drive the
exported OTEL span-context trace id (Langfuse derives a trace's identity
from it), deterministically hashed the same way ``langfuse.create_trace_id``
does so the caller can reproduce the id from their external id."""
import hashlib
trace_id = "global_20260225143025_ord2468"
expected_otel_trace_id = hashlib.sha256(trace_id.encode("utf-8")).digest()[
:16
].hex()
generation_name = "ishaan-test-generation"
exporter = InMemorySpanExporter()
logger = LangfuseOtelLogger(
@ -286,7 +295,7 @@ class TestLangfuseOtelIntegration:
)
assert generation_span.attributes.get("langfuse.trace.id") == trace_id
assert format(generation_span.context.trace_id, "032x") == trace_id
assert format(generation_span.context.trace_id, "032x") == expected_otel_trace_id
def test_set_langfuse_specific_attributes_with_content(self):
"""Test that _set_langfuse_specific_attributes correctly sets observation.output with regular content response."""