@@ -418,7 +418,7 @@ def test_span_attributes(self, langfuse_client, memory_exporter):
418418 # Verify attribute values
419419 assert input_data == {"prompt" : "Test prompt" }
420420 assert output_data == {"response" : "Updated response" }
421- assert metadata_data == "test-session"
421+ assert metadata_data == ' "test-session"'
422422 assert attributes [LangfuseOtelSpanAttributes .OBSERVATION_LEVEL ] == "INFO"
423423 assert (
424424 attributes [LangfuseOtelSpanAttributes .OBSERVATION_STATUS_MESSAGE ]
@@ -614,7 +614,7 @@ def test_complex_scenario(self, langfuse_client, memory_exporter):
614614 proc_metadata = proc ["attributes" ][
615615 f"{ LangfuseOtelSpanAttributes .OBSERVATION_METADATA } .step"
616616 ]
617- assert proc_metadata == "processing"
617+ assert proc_metadata == ' "processing"'
618618
619619 # Parse input/output JSON
620620 llm_input = json .loads (
@@ -1574,11 +1574,11 @@ def test_complex_metadata_serialization(self):
15741574 )
15751575 assert (
15761576 simple_result [f"{ LangfuseOtelSpanAttributes .OBSERVATION_METADATA } .key1" ]
1577- == "value1"
1577+ == ' "value1"'
15781578 )
15791579 assert (
15801580 simple_result [f"{ LangfuseOtelSpanAttributes .OBSERVATION_METADATA } .key2" ]
1581- == 123
1581+ == " 123"
15821582 )
15831583
15841584 # Test case 3: Nested dict (will be flattened in current implementation)
@@ -1631,7 +1631,7 @@ def test_complex_metadata_serialization(self):
16311631
16321632 # The nested structures are serialized as JSON strings
16331633 assert json .loads (complex_result [level1_key ]) == complex_dict ["level1" ]
1634- assert complex_result [sibling_key ] == "value"
1634+ assert complex_result [sibling_key ] == ' "value"'
16351635
16361636 def test_nested_metadata_updates (self ):
16371637 """Test that nested metadata updates don't overwrite unrelated keys."""
@@ -1986,6 +1986,108 @@ def update_random_metadata(thread_id):
19861986 assert "version" in system_data
19871987 assert "features" in system_data
19881988
1989+ def test_metadata_values_are_json_encoded (self , langfuse_client , memory_exporter ):
1990+ """Every per-key metadata value is sent as a JSON-encoded string."""
1991+ from datetime import timezone
1992+
1993+ prefix = LangfuseOtelSpanAttributes .OBSERVATION_METADATA
1994+ with langfuse_client .start_as_current_observation (
1995+ name = "json-metadata-span" ,
1996+ metadata = {
1997+ "string" : "hello" ,
1998+ "numeric_string" : "123" ,
1999+ "bool_string" : "true" ,
2000+ "int" : 5 ,
2001+ "bool" : True ,
2002+ "float" : 1.5 ,
2003+ "big_int" : 2 ** 70 ,
2004+ "nan" : float ("nan" ),
2005+ "list" : [1 , "a" , None ],
2006+ "nested" : {"a" : {"b" : [1 , 2 ]}, "c" : "d" },
2007+ "datetime" : datetime (2024 , 1 , 2 , 3 , 4 , 5 , tzinfo = timezone .utc ),
2008+ },
2009+ ):
2010+ pass
2011+
2012+ attributes = self .get_spans_by_name (memory_exporter , "json-metadata-span" )[0 ][
2013+ "attributes"
2014+ ]
2015+
2016+ expected = {
2017+ "string" : '"hello"' ,
2018+ "numeric_string" : '"123"' ,
2019+ "bool_string" : '"true"' ,
2020+ "int" : "5" ,
2021+ "bool" : "true" ,
2022+ "float" : "1.5" ,
2023+ "big_int" : '"1180591620717411303424"' ,
2024+ "nan" : '"NaN"' ,
2025+ "list" : '[1, "a", null]' ,
2026+ "nested" : '{"a": {"b": [1, 2]}, "c": "d"}' ,
2027+ "datetime" : '"2024-01-02T03:04:05Z"' ,
2028+ }
2029+ for key , value in expected .items ():
2030+ assert attributes [f"{ prefix } .{ key } " ] == value , key
2031+
2032+ # Decoding restores the original type, so strings and numbers stay distinct.
2033+ assert json .loads (attributes [f"{ prefix } .numeric_string" ]) == "123"
2034+ assert json .loads (attributes [f"{ prefix } .int" ]) == 5
2035+ assert json .loads (attributes [f"{ prefix } .bool" ]) is True
2036+
2037+ def test_big_int_metadata_value_survives_otlp_encoding (
2038+ self , langfuse_client , memory_exporter
2039+ ):
2040+ """Ints beyond int64 must not make the OTLP encoder drop the key."""
2041+ from opentelemetry .exporter .otlp .proto .common .trace_encoder import (
2042+ encode_spans ,
2043+ )
2044+
2045+ with langfuse_client .start_as_current_observation (
2046+ name = "big-int-span" , metadata = {"big" : 2 ** 70 , "small" : 1 }
2047+ ):
2048+ pass
2049+
2050+ spans = [
2051+ s for s in memory_exporter .get_finished_spans () if s .name == "big-int-span"
2052+ ]
2053+ request = encode_spans (spans )
2054+ encoded = {
2055+ kv .key : kv .value .string_value
2056+ for kv in request .resource_spans [0 ].scope_spans [0 ].spans [0 ].attributes
2057+ }
2058+
2059+ prefix = LangfuseOtelSpanAttributes .OBSERVATION_METADATA
2060+ assert encoded [f"{ prefix } .big" ] == '"1180591620717411303424"'
2061+ assert encoded [f"{ prefix } .small" ] == "1"
2062+
2063+ def test_none_metadata_value_keeps_earlier_value_on_update (
2064+ self , langfuse_client , memory_exporter
2065+ ):
2066+ """None values are skipped, so they don't overwrite earlier keys."""
2067+ prefix = LangfuseOtelSpanAttributes .OBSERVATION_METADATA
2068+ with langfuse_client .start_as_current_observation (
2069+ name = "none-metadata-span" , metadata = {"keep" : "first" , "other" : 1 }
2070+ ) as span :
2071+ span .update (metadata = {"keep" : None , "other" : 2 })
2072+
2073+ attributes = self .get_spans_by_name (memory_exporter , "none-metadata-span" )[0 ][
2074+ "attributes"
2075+ ]
2076+
2077+ assert attributes [f"{ prefix } .keep" ] == '"first"'
2078+ assert attributes [f"{ prefix } .other" ] == "2"
2079+
2080+ def test_flatten_metadata_trace_prefix_uses_same_encoding (self ):
2081+ """The trace prefix variant encodes values the same way."""
2082+ from langfuse ._client .attributes import _flatten_and_serialize_metadata
2083+
2084+ result = _flatten_and_serialize_metadata (
2085+ {"s" : "x" , "i" : 1 , "none" : None }, "trace"
2086+ )
2087+
2088+ prefix = LangfuseOtelSpanAttributes .TRACE_METADATA
2089+ assert result == {f"{ prefix } .s" : '"x"' , f"{ prefix } .i" : "1" }
2090+
19892091
19902092class TestMultiProjectSetup (TestOTelBase ):
19912093 """Tests for multi-project setup within the same process.
0 commit comments