From 5ffa43fb0c3d3f4fd99fa56106a2708c6b34e834 Mon Sep 17 00:00:00 2001 From: Dhanush Reddy Chilakala Date: Fri, 26 Jun 2026 00:36:00 -0500 Subject: [PATCH] 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). --- tests/test_litellm/integrations/test_langfuse_otel.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_litellm/integrations/test_langfuse_otel.py b/tests/test_litellm/integrations/test_langfuse_otel.py index 614fbaeac84..79607197d99 100644 --- a/tests/test_litellm/integrations/test_langfuse_otel.py +++ b/tests/test_litellm/integrations/test_langfuse_otel.py @@ -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."""