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
test_arize_set_attributes
Ishaan Jaff3e0df0fc1ee5
diff --git a/tests/local_testing/test_arize_ai.py b/tests/local_testing/test_arize_ai.pyindex b68981ffd58..9e668140d06 100644--- a/tests/local_testing/test_arize_ai.py+++ b/tests/local_testing/test_arize_ai.py@@ -8,7 +8,6 @@ from dotenv import load_dotenvimport litellmfrom litellm._logging import verbose_logger, verbose_proxy_logger-from litellm.integrations._types.open_inference import SpanAttributesfrom litellm.integrations.arize.arize import ArizeConfig, ArizeLoggerload_dotenv()@@ -61,40 +60,3 @@ def test_get_arize_config_with_endpoints(mock_env_vars, monkeypatch):config = ArizeLogger.get_arize_config()assert config.endpoint == "grpc://test.endpoint"assert config.protocol == "otlp_grpc"---def test_arize_set_attributes():- """- Test setting attributes for Arize- """- from unittest.mock import MagicMock- from litellm.types.utils import ModelResponse-- span = MagicMock()- kwargs = {- "role": "user",- "content": "simple arize test",- "model": "gpt-4o",- "messages": [{"role": "user", "content": "basic arize test"}],- "standard_logging_object": {"model_parameters": {"user": "test_user"}, "metadata": {"key": "value", "key2": None}},- }- response_obj = ModelResponse(usage={"total_tokens": 100, "completion_tokens": 60, "prompt_tokens": 40},- choices=[Choices(message={"role": "assistant", "content": "response content"})])-- ArizeLogger.set_arize_attributes(span, kwargs, response_obj)-- assert span.set_attribute.call_count == 14- span.set_attribute.assert_any_call(SpanAttributes.METADATA, json.dumps({"key": "value", "key2": None}))- span.set_attribute.assert_any_call(SpanAttributes.LLM_MODEL_NAME, "gpt-4o")- span.set_attribute.assert_any_call(SpanAttributes.OPENINFERENCE_SPAN_KIND, "LLM")- span.set_attribute.assert_any_call(SpanAttributes.INPUT_VALUE, "basic arize test")- span.set_attribute.assert_any_call("llm.input_messages.0.message.role", "user")- span.set_attribute.assert_any_call("llm.input_messages.0.message.content", "basic arize test")- span.set_attribute.assert_any_call(SpanAttributes.LLM_INVOCATION_PARAMETERS, '{"user": "test_user"}')- span.set_attribute.assert_any_call(SpanAttributes.USER_ID, "test_user")- span.set_attribute.assert_any_call(SpanAttributes.OUTPUT_VALUE, "response content")- span.set_attribute.assert_any_call("llm.output_messages.0.message.role", "assistant")- span.set_attribute.assert_any_call("llm.output_messages.0.message.content", "response content")- span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_TOTAL, 100)- span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_COMPLETION, 60)- span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_PROMPT, 40)diff --git a/tests/logging_callback_tests/test_arize_logging.py b/tests/logging_callback_tests/test_arize_logging.pyindex 27f067d7281..2a3d4523da7 100644--- a/tests/logging_callback_tests/test_arize_logging.py+++ b/tests/logging_callback_tests/test_arize_logging.py@@ -2,15 +2,17 @@ import osimport sysimport timefrom unittest.mock import Mock, patch-+import jsonfrom litellm.main import completionimport opentelemetry.exporter.otlp.proto.grpc.trace_exportersys.path.insert(0, os.path.abspath("../..")) # Adds the parent directory to the system-path-+from litellm.integrations._types.open_inference import SpanAttributes+from litellm.integrations.arize.arize import ArizeConfig, ArizeLoggerimport litellm+from litellm.types.utils import Choicesdef test_arize_callback():@@ -21,16 +23,16 @@ def test_arize_callback():# Set the batch span processor to quickly flush after a span has been added# This is to ensure that the span is exported before the test ends- os.environ["OTEL_BSP_MAX_QUEUE_SIZE"] = "1"- os.environ["OTEL_BSP_MAX_EXPORT_BATCH_SIZE"] = "1"- os.environ["OTEL_BSP_SCHEDULE_DELAY_MILLIS"] = "1"- os.environ["OTEL_BSP_EXPORT_TIMEOUT_MILLIS"] = "5"+ os.environ["OTEL_BSP_MAX_QUEUE_SIZE"] = "1"+ os.environ["OTEL_BSP_MAX_EXPORT_BATCH_SIZE"] = "1"+ os.environ["OTEL_BSP_SCHEDULE_DELAY_MILLIS"] = "1"+ os.environ["OTEL_BSP_EXPORT_TIMEOUT_MILLIS"] = "5"with patch.object(opentelemetry.exporter.otlp.proto.grpc.trace_exporter.OTLPSpanExporter,- 'export',- new=Mock()- ) as patched_export:+ "export",+ new=Mock(),+ ) as patched_export:completion(model="openai/test-model",messages=[{"role": "user", "content": "arize test content"}],@@ -38,5 +40,57 @@ def test_arize_callback():mock_response="hello there!",)- time.sleep(1) # Wait for the batch span processor to flush+ time.sleep(1) # Wait for the batch span processor to flushassert patched_export.called+++def test_arize_set_attributes():+ """+ Test setting attributes for Arize+ """+ from unittest.mock import MagicMock+ from litellm.types.utils import ModelResponse++ span = MagicMock()+ kwargs = {+ "role": "user",+ "content": "simple arize test",+ "model": "gpt-4o",+ "messages": [{"role": "user", "content": "basic arize test"}],+ "standard_logging_object": {+ "model_parameters": {"user": "test_user"},+ "metadata": {"key": "value", "key2": None},+ },+ }+ response_obj = ModelResponse(+ usage={"total_tokens": 100, "completion_tokens": 60, "prompt_tokens": 40},+ choices=[Choices(message={"role": "assistant", "content": "response content"})],+ )++ ArizeLogger.set_arize_attributes(span, kwargs, response_obj)++ assert span.set_attribute.call_count == 14+ span.set_attribute.assert_any_call(+ SpanAttributes.METADATA, json.dumps({"key": "value", "key2": None})+ )+ span.set_attribute.assert_any_call(SpanAttributes.LLM_MODEL_NAME, "gpt-4o")+ span.set_attribute.assert_any_call(SpanAttributes.OPENINFERENCE_SPAN_KIND, "LLM")+ span.set_attribute.assert_any_call(SpanAttributes.INPUT_VALUE, "basic arize test")+ span.set_attribute.assert_any_call("llm.input_messages.0.message.role", "user")+ span.set_attribute.assert_any_call(+ "llm.input_messages.0.message.content", "basic arize test"+ )+ span.set_attribute.assert_any_call(+ SpanAttributes.LLM_INVOCATION_PARAMETERS, '{"user": "test_user"}'+ )+ span.set_attribute.assert_any_call(SpanAttributes.USER_ID, "test_user")+ span.set_attribute.assert_any_call(SpanAttributes.OUTPUT_VALUE, "response content")+ span.set_attribute.assert_any_call(+ "llm.output_messages.0.message.role", "assistant"+ )+ span.set_attribute.assert_any_call(+ "llm.output_messages.0.message.content", "response content"+ )+ span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_TOTAL, 100)+ span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_COMPLETION, 60)+ span.set_attribute.assert_any_call(SpanAttributes.LLM_TOKEN_COUNT_PROMPT, 40)