From 1fb798f81d024a238a535ddf301f97c308f6856a Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Mon, 20 Oct 2025 21:23:22 +0530 Subject: [PATCH] (Bug) Fix JSON serialization error in Helicone logging by removing OpenTelemetry span from metadata (#15728) * remove span object from helicon metadata * Add test --- litellm/integrations/helicone.py | 5 +++ .../test_helicone_integration.py | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/litellm/integrations/helicone.py b/litellm/integrations/helicone.py index 79585a412b..198cbaf405 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 metadata diff --git a/tests/local_testing/test_helicone_integration.py b/tests/local_testing/test_helicone_integration.py index a128dd84c8..d825026ef4 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")