The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
(Bug) Fix JSON serialization error in Helicone logging by removing OpenTelemetry span from metadata (#15728)
Sameer Kankute1fb798f81d02
diff --git a/litellm/integrations/helicone.py b/litellm/integrations/helicone.pyindex 79585a412b3..198cbaf4058 100644--- a/litellm/integrations/helicone.py+++ b/litellm/integrations/helicone.py@@ -100,6 +100,11 @@ class HeliconeLogger:for header_key in proxy_headers:if header_key.startswith("helicone_"):metadata[header_key] = proxy_headers.get(header_key)++ # Remove OpenTelemetry span from metadata as it's not JSON serializable+ # The span is used internally for tracing but shouldn't be logged to external services+ if "litellm_parent_otel_span" in metadata:+ metadata.pop("litellm_parent_otel_span")return metadatadiff --git a/tests/local_testing/test_helicone_integration.py b/tests/local_testing/test_helicone_integration.pyindex a128dd84c86..d825026ef4f 100644--- a/tests/local_testing/test_helicone_integration.py+++ b/tests/local_testing/test_helicone_integration.py@@ -123,3 +123,42 @@ async def test_helicone_logging_metadata():print(response)time.sleep(3)+++def test_helicone_removes_otel_span_from_metadata():+ """+ Test that HeliconeLogger removes litellm_parent_otel_span from metadata+ to prevent JSON serialization errors.+ """+ from litellm.integrations.helicone import HeliconeLogger+ from unittest.mock import MagicMock++ # Create a mock span object (similar to what OpenTelemetry would create)+ mock_span = MagicMock()+ mock_span.__class__.__name__ = "_Span"++ # Create metadata with the problematic span object+ metadata = {+ "user_id": "test_user",+ "request_id": "test_request_123",+ "litellm_parent_otel_span": mock_span, # This would cause JSON serialization error+ "other_metadata": "some_value"+ }++ # Create HeliconeLogger instance+ logger = HeliconeLogger()++ # Test the add_metadata_from_header method+ litellm_params = {"proxy_server_request": {"headers": {}}}+ result_metadata = logger.add_metadata_from_header(litellm_params, metadata)++ # Verify that litellm_parent_otel_span was removed+ assert "litellm_parent_otel_span" not in result_metadata+ assert "user_id" in result_metadata+ assert "request_id" in result_metadata+ assert "other_metadata" in result_metadata+ assert result_metadata["user_id"] == "test_user"+ assert result_metadata["request_id"] == "test_request_123"+ assert result_metadata["other_metadata"] == "some_value"++ print("✅ Test passed: litellm_parent_otel_span was successfully removed from metadata")