From 07e42bc871a8a9557e271605038edcc97d48bcf8 Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Sat, 25 Apr 2026 17:49:34 -0400 Subject: [PATCH 1/8] fix arize obsv bugs --- litellm/integrations/arize/_utils.py | 556 ++++++++++++++++++-- litellm/integrations/arize/arize_phoenix.py | 47 +- 2 files changed, 558 insertions(+), 45 deletions(-) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index 8dfaa8b1425a..e40089a8e6f4 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -1,6 +1,9 @@ import json +import hashlib +import os from typing import TYPE_CHECKING, Any, Dict, Optional, Type +from pydantic import BaseModel from typing_extensions import override from litellm._logging import verbose_logger @@ -14,15 +17,19 @@ if TYPE_CHECKING: from opentelemetry.trace import Span from litellm.integrations._types.open_inference import ( - MessageAttributes, - ImageAttributes, - SpanAttributes, AudioAttributes, EmbeddingAttributes, + ImageAttributes, + MessageAttributes, + MessageContentAttributes, OpenInferenceSpanKindValues, + SpanAttributes, ) +DEFAULT_MAX_INLINE_IMAGE_BYTES = 32 * 1024 + + class ArizeOTELAttributes(BaseLLMObsOTELAttributes): @staticmethod @override @@ -92,17 +99,113 @@ def set_response_output_messages(span: "Span", response_obj): def _set_response_attributes(span: "Span", response_obj): """Helper to set response output and token usage attributes on span.""" + # Pydantic responses (ImageResponse, ResponsesAPIResponse, ...) need to be + # dict-coerced so the dict-keyed setters below see the data. + if isinstance(response_obj, BaseModel): + try: + response_obj = response_obj.model_dump() + except Exception: + return + if not hasattr(response_obj, "get"): return _set_choice_outputs(span, response_obj, MessageAttributes, SpanAttributes) - _set_image_outputs(span, response_obj, ImageAttributes, SpanAttributes) _set_audio_outputs(span, response_obj, AudioAttributes, SpanAttributes) _set_embedding_outputs(span, response_obj, EmbeddingAttributes, SpanAttributes) + _set_image_outputs( + span, + response_obj, + MessageAttributes, + MessageContentAttributes, + ImageAttributes, + SpanAttributes, + ) _set_structured_outputs(span, response_obj, MessageAttributes, SpanAttributes) _set_usage_outputs(span, response_obj, SpanAttributes) +def _set_image_outputs( + span: "Span", + response_obj, + msg_attrs, + content_attrs, + image_attrs, + span_attrs, +): + """ + Render generated images on the LLM span. + + Phoenix renders an image inline when the assistant message has an + ``image``-type content entry whose ``image.url`` is either a public URL or + a ``data:image/;base64,<...>`` URI. Mirrors the structure produced + by ``openinference.instrumentation.ImageMessageContent``. + + Iterates ``response_obj["data"]`` (image-gen / image-edit shape). Skips + items that lack both ``url`` and ``b64_json`` so embedding payloads (which + also live under ``data``) aren't matched. + """ + data = response_obj.get("data") + if not isinstance(data, list) or not data: + return + + role_set = False + content_idx = 0 + for image_item in data: + if isinstance(image_item, BaseModel): + image_item = image_item.model_dump() + if not hasattr(image_item, "get"): + continue + + if not image_item.get("url") and not image_item.get("b64_json"): + continue # not an image item (could be embedding) + + image_url, mime, omission_notice = _get_image_trace_payload( + image_item, response_obj + ) + if not image_url and not omission_notice: + continue + + if not role_set: + safe_set_attribute( + span, + f"{span_attrs.LLM_OUTPUT_MESSAGES}.0.{msg_attrs.MESSAGE_ROLE}", + "assistant", + ) + # First image also drives the span's top-level Output preview. + if image_url: + safe_set_attribute(span, span_attrs.OUTPUT_VALUE, image_url) + safe_set_attribute(span, span_attrs.OUTPUT_MIME_TYPE, mime) + else: + safe_set_attribute(span, span_attrs.OUTPUT_VALUE, omission_notice) + safe_set_attribute(span, span_attrs.OUTPUT_MIME_TYPE, "text/plain") + role_set = True + + prefix = ( + f"{span_attrs.LLM_OUTPUT_MESSAGES}.0." + f"{msg_attrs.MESSAGE_CONTENTS}.{content_idx}" + ) + if image_url: + safe_set_attribute( + span, f"{prefix}.{content_attrs.MESSAGE_CONTENT_TYPE}", "image" + ) + safe_set_attribute( + span, + f"{prefix}.{content_attrs.MESSAGE_CONTENT_IMAGE}.{image_attrs.IMAGE_URL}", + image_url, + ) + elif omission_notice: + safe_set_attribute( + span, f"{prefix}.{content_attrs.MESSAGE_CONTENT_TYPE}", "text" + ) + safe_set_attribute( + span, + f"{prefix}.{content_attrs.MESSAGE_CONTENT_TEXT}", + omission_notice, + ) + content_idx += 1 + + def _set_choice_outputs(span: "Span", response_obj, msg_attrs, span_attrs): for idx, choice in enumerate(response_obj.get("choices", [])): response_message = choice.get("message", {}) @@ -124,22 +227,6 @@ def _set_choice_outputs(span: "Span", response_obj, msg_attrs, span_attrs): ) -def _set_image_outputs(span: "Span", response_obj, image_attrs, span_attrs): - images = response_obj.get("data", []) - for i, image in enumerate(images): - img_url = image.get("url") - if img_url is None and image.get("b64_json"): - img_url = f"data:image/png;base64,{image.get('b64_json')}" - - if not img_url: - continue - - if i == 0: - safe_set_attribute(span, span_attrs.OUTPUT_VALUE, img_url) - - safe_set_attribute(span, f"{image_attrs.IMAGE_URL}.{i}", img_url) - - def _set_audio_outputs(span: "Span", response_obj, audio_attrs, span_attrs): audio = response_obj.get("audio", []) for i, audio_item in enumerate(audio): @@ -194,25 +281,37 @@ def _set_structured_outputs(span: "Span", response_obj, msg_attrs, span_attrs): output_items = response_obj.get("output", []) for i, item in enumerate(output_items): prefix = f"{span_attrs.LLM_OUTPUT_MESSAGES}.{i}" - if not hasattr(item, "type"): + + # Items can arrive as Pydantic models (SDK path) or dicts (proxy path). + # Read both via a uniform accessor so dict-shaped output[] arrays from + # Responses API don't get silently skipped. + def _get(obj, key, default=None): + if hasattr(obj, "get"): + return obj.get(key, default) + return getattr(obj, key, default) + + item_type = _get(item, "type") + if item_type is None: continue - item_type = item.type - if item_type == "reasoning" and hasattr(item, "summary"): - for summary in item.summary: - if hasattr(summary, "text"): - safe_set_attribute( - span, - f"{prefix}.{msg_attrs.MESSAGE_REASONING_SUMMARY}", - summary.text, - ) - elif item_type == "message" and hasattr(item, "content"): + if item_type == "reasoning": + summary = _get(item, "summary") + if isinstance(summary, list): + for s in summary: + text = _get(s, "text") + if text: + safe_set_attribute( + span, + f"{prefix}.{msg_attrs.MESSAGE_REASONING_SUMMARY}", + text, + ) + elif item_type == "message": + content_list = _get(item, "content") or [] message_content = "" - content_list = item.content - if content_list and len(content_list) > 0: + if content_list: first_content = content_list[0] - message_content = getattr(first_content, "text", "") - message_role = getattr(item, "role", "assistant") + message_content = _get(first_content, "text", "") or "" + message_role = _get(item, "role", "assistant") or "assistant" safe_set_attribute(span, span_attrs.OUTPUT_VALUE, message_content) safe_set_attribute( span, f"{prefix}.{msg_attrs.MESSAGE_CONTENT}", message_content @@ -419,6 +518,388 @@ def set_attributes( span.record_exception(e) +def _resolve_image_mime_type(image, response_obj) -> str: + """ + Pick the MIME type for an image in a generation/edit response. + + Phoenix's image renderer keys off the data-URI mime, so a wrong value + (e.g. ``image/png`` for a JPEG) breaks inline preview. Resolve in order: + per-image ``mime_type`` → per-image ``output_format`` → response + ``output_format`` → ``image/png``. + """ + mime: Optional[str] = None + if hasattr(image, "get"): + mime = image.get("mime_type") or image.get("output_format") + if not mime and hasattr(response_obj, "get"): + mime = response_obj.get("output_format") + if not mime: + return "image/png" + mime = str(mime).lower() + if mime == "jpg": + mime = "jpeg" + if "/" not in mime: + mime = f"image/{mime}" + return mime + + +def _estimate_b64_decoded_bytes(b64_payload: str) -> int: + """Estimate decoded byte size from base64 payload without decoding.""" + payload = b64_payload.strip() + padding = payload.count("=") + return max(0, (len(payload) * 3) // 4 - padding) + + +def _get_max_inline_image_bytes() -> Optional[int]: + """ + Resolve max inline bytes for image payloads. + + - Default is 32KB to keep Phoenix spans exportable. + - Set LITELLM_ARIZE_MAX_INLINE_IMAGE_BYTES<=0 to disable the cap. + """ + raw_value = os.getenv("LITELLM_ARIZE_MAX_INLINE_IMAGE_BYTES") + if raw_value is None or raw_value == "": + return DEFAULT_MAX_INLINE_IMAGE_BYTES + try: + parsed = int(raw_value) + if parsed <= 0: + return None + return parsed + except (TypeError, ValueError): + return DEFAULT_MAX_INLINE_IMAGE_BYTES + + +def _get_image_trace_payload( + image_item, response_obj +) -> tuple[Optional[str], str, Optional[str]]: + """ + Build image payload tuple: + (image_url_or_data_uri, mime_type, omission_notice). + """ + url = image_item.get("url") + mime = _resolve_image_mime_type(image_item, response_obj) + if url: + return url, mime, None + + b64 = image_item.get("b64_json") + if not b64: + return None, mime, None + + max_inline_bytes = _get_max_inline_image_bytes() + if max_inline_bytes is not None: + decoded_bytes = _estimate_b64_decoded_bytes(str(b64)) + if decoded_bytes > max_inline_bytes: + digest = hashlib.sha256(str(b64).encode("utf-8")).hexdigest()[:12] + omission_notice = ( + f"[image omitted from trace: {decoded_bytes} bytes exceeds " + f"{max_inline_bytes} byte inline limit, sha256={digest}]" + ) + return None, mime, omission_notice + + return f"data:{mime};base64,{b64}", mime, None + + +def _extract_responses_api_text(output_items) -> Optional[str]: + """Concatenate ``output[*].content[*].text`` for Responses API message items.""" + if not isinstance(output_items, list) or not output_items: + return None + + def _g(obj, key, default=None): + if hasattr(obj, "get"): + return obj.get(key, default) + return getattr(obj, key, default) + + texts = [] + for item in output_items: + if _g(item, "type") != "message": + continue + content_list = _g(item, "content") + if not isinstance(content_list, list): + continue + for c in content_list: + text = _g(c, "text") + if text: + texts.append(text) + return "\n\n".join(texts) if texts else None + + +def _coerce_text(value: Any) -> Optional[str]: + """Reduce a heterogeneous prompt/response value to a renderable string.""" + if value is None: + return None + if isinstance(value, str): + return value + if isinstance(value, (list, tuple)): + # Embedding `input` is often List[str]; show the first entry plus a + # count rather than serialising every vector input. + text_items = [item for item in value if isinstance(item, str)] + if text_items: + head = text_items[0] + if len(text_items) == 1: + return head + return f"{head}\n\n[+ {len(text_items) - 1} more]" + return safe_dumps(value) + if isinstance(value, dict): + return safe_dumps(value) + return str(value) + + +def _extract_chain_input( + kwargs, standard_logging_payload: Optional[StandardLoggingPayload] +) -> tuple[Optional[str], Optional[str]]: + """ + - LLM chat history → last user message content + - LLM completion / image gen → ``prompt`` + - Embedding → ``input`` (string or list of strings) + - Reranker / Retriever → ``query`` (+ document count) + - Tool / MCP → ``{name, arguments}`` JSON + - Agent (a2a / responses) → ``input`` or last message + - Guardrail → ``text`` + - Anything else → ``standard_logging_payload["messages"]`` as JSON + """ + # 1. Tool calls — distinct shape, render as JSON arguments + tool_name = kwargs.get("name") or kwargs.get("tool_name") + tool_args = kwargs.get("arguments") or kwargs.get("tool_arguments") + if tool_name and tool_args is not None: + return ( + safe_dumps({"name": tool_name, "arguments": tool_args}), + "application/json", + ) + + # 2. Chat-style messages (LLM, agents that use messages) + messages = kwargs.get("messages") + if isinstance(messages, list) and messages: + last = messages[-1] + if isinstance(last, dict): + content = last.get("content") + text = _coerce_text(content) + if text: + return text, None + + # 3. Reranker / Retriever — query plus optional document count summary + query = kwargs.get("query") + if query: + text = _coerce_text(query) + documents = kwargs.get("documents") + if isinstance(documents, list) and documents: + text = f"{text}\n\n[+ {len(documents)} documents]" + return text, None + + # 4. Single-text inputs (embedding/image/completion/guardrail) + for field in ("input", "prompt", "text"): + text = _coerce_text(kwargs.get(field)) + if text: + return text, None + + # 5. Universal fallback — every call type populates this + if isinstance(standard_logging_payload, dict): + msgs = standard_logging_payload.get("messages") + text = _coerce_text(msgs) + if text: + mime = "application/json" if isinstance(msgs, (list, dict)) else None + return text, mime + + return None, None + + +def _extract_chain_output( + response_obj, standard_logging_payload: Optional[StandardLoggingPayload] = None +) -> tuple[Optional[str], Optional[str]]: + """ + Build ``(output.value, output.mime_type)`` for the parent CHAIN span. + + Tries the rich response_obj shapes first, then falls back to + ``standard_logging_payload["response"]`` so TOOL / AGENT / RETRIEVER / + RERANKER / GUARDRAIL spans still get a meaningful output. + + - Chat → first choice message content (or tool_calls JSON if no content) + - Image gen → first image URL or ``data:`` URI (matches Phoenix's renderer) + - Embedding → ``" embeddings"`` summary + - Tool result → ``content`` text or full result JSON + - Reranker → ``results`` JSON + - Anything else → ``standard_logging_payload["response"]`` + """ + + if isinstance(response_obj, BaseModel): + try: + response_obj = response_obj.model_dump() + except Exception: + pass + + response_obj_dict = response_obj if isinstance(response_obj, dict) else None + if response_obj_dict is not None: + # 1a. Responses API — output[*].content[*].text on message items. + # Matches what _set_structured_outputs writes on the LLM child span, + # so the parent CHAIN renders the same readable assistant text. + responses_text = _extract_responses_api_text(response_obj_dict.get("output")) + if responses_text: + return responses_text, None + + # 1. Chat completion choices + choices = response_obj_dict.get("choices") or [] + if isinstance(choices, list) and choices: + first = choices[0] + message = first.get("message") if hasattr(first, "get") else None + if isinstance(message, dict): + content = _coerce_text(message.get("content")) + if content: + return content, None + # Tool-calling assistant turn — no content but tool_calls present + tool_calls = message.get("tool_calls") + if tool_calls: + return safe_dumps(tool_calls), "application/json" + + # 2. Image / Embedding ``data`` array + data = response_obj_dict.get("data") + if isinstance(data, list) and data: + first = data[0] + if isinstance(first, BaseModel): + first = first.model_dump() + if isinstance(first, dict): + url = first.get("url") + if url: + return url, _resolve_image_mime_type(first, response_obj_dict) + + b64 = first.get("b64_json") + if b64: + image_url, mime, omission_notice = _get_image_trace_payload( + first, response_obj_dict + ) + if image_url: + return image_url, mime + if omission_notice: + return omission_notice, "text/plain" + + if first.get("embedding") is not None: + return f"{len(data)} embeddings", "text/plain" + + # 3. Tool / MCP result shapes — content array, output_text, result + for field in ("output_text", "result"): + text = _coerce_text(response_obj_dict.get(field)) + if text: + mime = ( + "application/json" + if isinstance(response_obj_dict.get(field), (list, dict)) + else None + ) + return text, mime + + content = response_obj_dict.get("content") + if content: + return _coerce_text(content), ( + "application/json" if isinstance(content, (list, dict)) else None + ) + + # 4. Reranker results + results = response_obj_dict.get("results") + if results: + return safe_dumps(results), "application/json" + + # 5. Universal fallback for any call type + if isinstance(standard_logging_payload, dict): + resp = standard_logging_payload.get("response") + text = _coerce_text(resp) + if text: + mime = "application/json" if isinstance(resp, (list, dict)) else None + return text, mime + + return None, None + + +def set_parent_span_attributes(span: "Span", kwargs, response_obj): + """ + set parent level span attr (model, call type) to prevent token / cost duplication from child spans + """ + try: + litellm_params = kwargs.get("litellm_params", {}) or {} + standard_logging_payload: Optional[StandardLoggingPayload] = kwargs.get( + "standard_logging_object" + ) + + safe_set_attribute( + span, + SpanAttributes.OPENINFERENCE_SPAN_KIND, + OpenInferenceSpanKindValues.CHAIN.value, + ) + + if kwargs.get("model"): + safe_set_attribute(span, SpanAttributes.LLM_MODEL_NAME, kwargs.get("model")) + + safe_set_attribute( + span, + SpanAttributes.LLM_PROVIDER, + litellm_params.get("custom_llm_provider", "Unknown"), + ) + + if standard_logging_payload is not None: + call_type = standard_logging_payload.get("call_type") + if call_type: + safe_set_attribute(span, "llm.request.type", call_type) + + metadata = standard_logging_payload.get("metadata") + _set_metadata_attributes(span, metadata, SpanAttributes) + + model_params = standard_logging_payload.get("model_parameters") or {} + user_id = ( + model_params.get("user") if isinstance(model_params, dict) else None + ) + if user_id is not None: + safe_set_attribute(span, SpanAttributes.USER_ID, user_id) + + optional_params = _sanitize_optional_params(kwargs.get("optional_params")) + safe_set_attribute( + span, "llm.is_streaming", str(optional_params.get("stream", False)) + ) + + # pass provider / litellm call id + response_id = _resolve_response_id(response_obj, standard_logging_payload) + if response_id is not None: + safe_set_attribute(span, "llm.response.id", response_id) + + # sanitize input / output for parent span + input_value, input_mime = _extract_chain_input(kwargs, standard_logging_payload) + if input_value: + safe_set_attribute(span, SpanAttributes.INPUT_VALUE, input_value) + if input_mime: + safe_set_attribute(span, SpanAttributes.INPUT_MIME_TYPE, input_mime) + + output_value, output_mime = _extract_chain_output( + response_obj, standard_logging_payload + ) + if output_value: + safe_set_attribute(span, SpanAttributes.OUTPUT_VALUE, output_value) + if output_mime: + safe_set_attribute(span, SpanAttributes.OUTPUT_MIME_TYPE, output_mime) + + except Exception as e: + verbose_logger.error( + f"[Arize/Phoenix] Failed to set parent span attributes: {e}" + ) + if hasattr(span, "record_exception"): + span.record_exception(e) + + +def _resolve_response_id( + response_obj, standard_logging_payload: Optional[StandardLoggingPayload] +) -> Optional[str]: + """ + for completions / responses endpoint, pass the response id issued by provider. + (chatcmpl-*) + other endpoints (embeddings, images) pass the standard x-litellm-call-id + to match litellm UI logs with phoenix + """ + if isinstance(response_obj, dict): + provider_id = response_obj.get("id") + if provider_id: + return str(provider_id) + + if isinstance(standard_logging_payload, dict): + litellm_call_id = standard_logging_payload.get("litellm_call_id") + if litellm_call_id: + return str(litellm_call_id) + + return None + + def _sanitize_optional_params(optional_params: Optional[dict]) -> dict: if not isinstance(optional_params, dict): return {} @@ -483,8 +964,9 @@ def _set_request_attributes( if optional_params.get("user"): safe_set_attribute(span, "llm.user", optional_params.get("user")) - if response_obj and response_obj.get("id"): - safe_set_attribute(span, "llm.response.id", response_obj.get("id")) + response_id = _resolve_response_id(response_obj, standard_logging_payload) + if response_id is not None: + safe_set_attribute(span, "llm.response.id", response_id) if response_obj and response_obj.get("model"): safe_set_attribute(span, "llm.response.model", response_obj.get("model")) diff --git a/litellm/integrations/arize/arize_phoenix.py b/litellm/integrations/arize/arize_phoenix.py index b8cd04836c32..df941419849b 100644 --- a/litellm/integrations/arize/arize_phoenix.py +++ b/litellm/integrations/arize/arize_phoenix.py @@ -5,6 +5,7 @@ from litellm.integrations.arize import _utils from litellm.integrations.arize._utils import ArizeOTELAttributes from litellm.types.integrations.arize_phoenix import ArizePhoenixConfig +from opentelemetry import trace as _trace if TYPE_CHECKING: from opentelemetry.sdk.trace import TracerProvider @@ -87,6 +88,18 @@ def _init_otel_logger_on_litellm_proxy(self): """ pass + async def async_post_call_success_hook( + self, + data, + user_api_key_dict, + response, + ): + """ + skipping this hook prevents both the orphan and the duplicate guardrail spans. + already handled in global tracer provider + """ + return response + def set_attributes(self, span: Span, kwargs, response_obj: Optional[Any]): ArizePhoenixLogger.set_arize_phoenix_attributes(span, kwargs, response_obj) return @@ -221,15 +234,26 @@ def _handle_success(self, kwargs, response_obj, start_time, end_time): # Raw-request sub-span (if enabled) — must be created before # ending the parent span so the hierarchy is valid. self._maybe_log_raw_request(kwargs, response_obj, start_time, end_time, span) + + # Guardrail context: in proxy mode it's a sibling of litellm_request + # under litellm_proxy_request. In SDK mode there is no proxy parent, + # so we parent it to the litellm_request span to avoid an orphan. + guardrail_ctx = ( + ctx if parent_span is not None else _trace.set_span_in_context(span) + ) + span.end(end_time=self._to_ns(end_time)) - # Guardrail span - self._create_guardrail_span(kwargs=kwargs, context=ctx) + # Guardrail span (always parented — no orphan roots) + self._create_guardrail_span(kwargs=kwargs, context=guardrail_ctx) - # Annotate and close our proxy parent span + # Annotate and close our proxy parent span. + # Only session/request metadata goes on the parent — the child span + # already carries the full LLM payload (messages, tokens, response). + # Duplicating them here double-counts tokens. if parent_span is not None: parent_span.set_status(Status(StatusCode.OK)) - self.set_attributes(parent_span, kwargs, response_obj) + _utils.set_parent_span_attributes(parent_span, kwargs, response_obj) parent_span.end(end_time=self._to_ns(end_time)) # Metrics & cost recording @@ -263,15 +287,22 @@ def _handle_failure(self, kwargs, response_obj, start_time, end_time): span.set_status(Status(StatusCode.ERROR)) self.set_attributes(span, kwargs, response_obj) self._record_exception_on_span(span=span, kwargs=kwargs) + + # See _handle_success for guardrail context rationale. + guardrail_ctx = ( + ctx if parent_span is not None else _trace.set_span_in_context(span) + ) + span.end(end_time=self._to_ns(end_time)) - # Guardrail span - self._create_guardrail_span(kwargs=kwargs, context=ctx) + # Guardrail span (always parented, no orphan roots) + self._create_guardrail_span(kwargs=kwargs, context=guardrail_ctx) - # Annotate and close our proxy parent span + # Annotate and close our proxy parent span (see _handle_success for + # why only parent-scoped attributes go here). if parent_span is not None: parent_span.set_status(Status(StatusCode.ERROR)) - self.set_attributes(parent_span, kwargs, response_obj) + _utils.set_parent_span_attributes(parent_span, kwargs, response_obj) self._record_exception_on_span(span=parent_span, kwargs=kwargs) parent_span.end(end_time=self._to_ns(end_time)) From d778be1f24fa94aa7688f45a6c919b71b545e1cb Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Sat, 25 Apr 2026 21:27:45 -0400 Subject: [PATCH 2/8] fix --- litellm/integrations/arize/_utils.py | 19 ++++++++++--------- litellm/integrations/arize/arize_phoenix.py | 5 ++++- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index e40089a8e6f4..3ad7dda0796c 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -277,19 +277,20 @@ def _set_embedding_outputs(span: "Span", response_obj, embedding_attrs, span_att ) +# Items can arrive as Pydantic models (SDK path) or dicts (proxy path). +# Read both via a uniform accessor so dict-shaped output[] arrays from +# Responses API don't get silently skipped. +def _get(obj, key, default=None): + if hasattr(obj, "get"): + return obj.get(key, default) + return getattr(obj, key, default) + + def _set_structured_outputs(span: "Span", response_obj, msg_attrs, span_attrs): output_items = response_obj.get("output", []) for i, item in enumerate(output_items): prefix = f"{span_attrs.LLM_OUTPUT_MESSAGES}.{i}" - # Items can arrive as Pydantic models (SDK path) or dicts (proxy path). - # Read both via a uniform accessor so dict-shaped output[] arrays from - # Responses API don't get silently skipped. - def _get(obj, key, default=None): - if hasattr(obj, "get"): - return obj.get(key, default) - return getattr(obj, key, default) - item_type = _get(item, "type") if item_type is None: continue @@ -718,7 +719,7 @@ def _extract_chain_output( - Reranker → ``results`` JSON - Anything else → ``standard_logging_payload["response"]`` """ - + if isinstance(response_obj, BaseModel): try: response_obj = response_obj.model_dump() diff --git a/litellm/integrations/arize/arize_phoenix.py b/litellm/integrations/arize/arize_phoenix.py index df941419849b..563d1b493c17 100644 --- a/litellm/integrations/arize/arize_phoenix.py +++ b/litellm/integrations/arize/arize_phoenix.py @@ -5,7 +5,6 @@ from litellm.integrations.arize import _utils from litellm.integrations.arize._utils import ArizeOTELAttributes from litellm.types.integrations.arize_phoenix import ArizePhoenixConfig -from opentelemetry import trace as _trace if TYPE_CHECKING: from opentelemetry.sdk.trace import TracerProvider @@ -238,6 +237,8 @@ def _handle_success(self, kwargs, response_obj, start_time, end_time): # Guardrail context: in proxy mode it's a sibling of litellm_request # under litellm_proxy_request. In SDK mode there is no proxy parent, # so we parent it to the litellm_request span to avoid an orphan. + from opentelemetry import trace as _trace + guardrail_ctx = ( ctx if parent_span is not None else _trace.set_span_in_context(span) ) @@ -289,6 +290,8 @@ def _handle_failure(self, kwargs, response_obj, start_time, end_time): self._record_exception_on_span(span=span, kwargs=kwargs) # See _handle_success for guardrail context rationale. + from opentelemetry import trace as _trace + guardrail_ctx = ( ctx if parent_span is not None else _trace.set_span_in_context(span) ) From caca527cf898d696a0b960f4264e3a8b24b6e8ff Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Mon, 27 Apr 2026 21:54:43 -0400 Subject: [PATCH 3/8] added --- litellm/integrations/arize/_utils.py | 18 +++++ litellm/integrations/arize/arize_phoenix.py | 82 +++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index 3ad7dda0796c..ca9d432b4daa 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -938,6 +938,24 @@ def _set_request_attributes( if kwargs.get("model"): safe_set_attribute(span, span_attrs.LLM_MODEL_NAME, kwargs.get("model")) + # Track fallback context: which attempt number in the fallback chain + fallback_attempt = kwargs.get("fallback_depth") + if fallback_attempt is not None: + safe_set_attribute(span, "llm.fallback.attempt_number", fallback_attempt) + # Original model that was attempted before fallback + original_model = kwargs.get("original_model") + if original_model: + safe_set_attribute(span, "llm.fallback.original_model", original_model) + # Error that triggered fallback + original_exception = kwargs.get("original_exception") + if original_exception is not None: + status_code = getattr(original_exception, "status_code", None) + if status_code is not None: + safe_set_attribute(span, "llm.fallback.error_status_code", status_code) + exception_class = getattr(original_exception, "__class__.__name__", None) + if exception_class: + safe_set_attribute(span, "llm.fallback.error_class", exception_class) + safe_set_attribute( span, "llm.request.type", standard_logging_payload.get("call_type") ) diff --git a/litellm/integrations/arize/arize_phoenix.py b/litellm/integrations/arize/arize_phoenix.py index 563d1b493c17..7e9b8a4fdd00 100644 --- a/litellm/integrations/arize/arize_phoenix.py +++ b/litellm/integrations/arize/arize_phoenix.py @@ -392,3 +392,85 @@ async def async_health_check(self): "status": "healthy", "message": "Arize-Phoenix credentials are configured properly", } + + async def log_success_fallback_event( + self, + original_model_group: str, + kwargs: dict, + original_exception: Exception, + ): + """ + Log a successful fallback event by creating a span that captures the fallback chain. + + When a fallback succeeds, this creates a span showing: + - Which model originally failed (original_model_group) + - Which model it fell back to (from kwargs["model"]) + - The error that triggered the fallback + """ + from opentelemetry.trace import Status, StatusCode + + original_model = original_model_group + fallback_model = kwargs.get("model") + fallback_depth = kwargs.get("fallback_depth", 1) + + status_code = getattr(original_exception, "status_code", None) + exception_class = original_exception.__class__.__name__ + + span_name = f"fallback: {original_model} -> {fallback_model}" + span = self.tracer.start_span(name=span_name) + + def _safe_set(s, k, v): + if hasattr(s, "set_attribute"): + s.set_attribute(k, v) + + _safe_set(span, "llm.fallback.event_type", "success") + _safe_set(span, "llm.fallback.original_model", original_model) + _safe_set(span, "llm.fallback.fallback_model", fallback_model) + _safe_set(span, "llm.fallback.attempt_number", fallback_depth) + if status_code is not None: + _safe_set(span, "llm.fallback.error_status_code", status_code) + _safe_set(span, "llm.fallback.error_class", exception_class) + + span.set_status(Status(StatusCode.OK)) + span.end() + + async def log_failure_fallback_event( + self, + original_model_group: str, + kwargs: dict, + original_exception: Exception, + ): + """ + Log a failed fallback event by creating a span that captures the failure. + + When all fallbacks fail, this creates a span showing: + - Which model originally failed + - Which fallback models were attempted + - The chain of errors + """ + from opentelemetry.trace import Status, StatusCode + + original_model = original_model_group + fallback_attempted = kwargs.get("model") + max_fallbacks = kwargs.get("max_fallbacks", 0) + + exception_class = original_exception.__class__.__name__ + status_code = getattr(original_exception, "status_code", None) + + span_name = f"fallback_failed: {original_model}" + span = self.tracer.start_span(name=span_name) + + def _safe_set(s, k, v): + if hasattr(s, "set_attribute"): + s.set_attribute(k, v) + + _safe_set(span, "llm.fallback.event_type", "failure") + _safe_set(span, "llm.fallback.original_model", original_model) + _safe_set(span, "llm.fallback.attempted_model", fallback_attempted) + _safe_set(span, "llm.fallback.max_fallbacks", max_fallbacks) + if status_code is not None: + _safe_set(span, "llm.fallback.error_status_code", status_code) + _safe_set(span, "llm.fallback.error_class", exception_class) + + span.set_status(Status(StatusCode.ERROR)) + span.end() From c49b3e7378cf9ce61321d8cf0884e6a16a5da827 Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Tue, 28 Apr 2026 09:24:09 -0400 Subject: [PATCH 4/8] add fallback traces --- litellm/integrations/arize/arize_phoenix.py | 225 ++++++++++++++------ litellm/router.py | 68 +++++- 2 files changed, 220 insertions(+), 73 deletions(-) diff --git a/litellm/integrations/arize/arize_phoenix.py b/litellm/integrations/arize/arize_phoenix.py index 7e9b8a4fdd00..9d3f3c614e50 100644 --- a/litellm/integrations/arize/arize_phoenix.py +++ b/litellm/integrations/arize/arize_phoenix.py @@ -154,28 +154,144 @@ def _get_dynamic_project_name(kwargs) -> Optional[str]: return None + # ------------------------------------------------------------------ + # Router-level parent span — one per logical user request so all + # retry + fallback attempt spans nest under a single root in Phoenix. + # The span object is stored on the logger instance (never in kwargs / + # metadata) to avoid JSON-serialisation errors when request data is + # forwarded to providers. + # ------------------------------------------------------------------ + + def _router_span_registry(self) -> dict: + if not hasattr(self, "_router_parent_spans"): + self._router_parent_spans: dict = {} + return self._router_parent_spans + + @staticmethod + def _router_call_id(kwargs) -> Optional[str]: + call_id = kwargs.get("litellm_trace_id") or kwargs.get("litellm_call_id") + if call_id: + return str(call_id) + litellm_params = kwargs.get("litellm_params") or {} + call_id = litellm_params.get("litellm_trace_id") or litellm_params.get( + "litellm_call_id" + ) + return str(call_id) if call_id else None + + def start_router_parent_span(self, kwargs: dict) -> None: + import uuid + + from opentelemetry import trace + from opentelemetry.trace import SpanKind + + call_id = self._router_call_id(kwargs) + if call_id is None: + call_id = str(uuid.uuid4()) + kwargs["litellm_trace_id"] = call_id + + registry = self._router_span_registry() + if call_id in registry: + return + + # Evict stale ended entries (span=None) to bound registry size. + if len(registry) > 200: + stale = [k for k, v in registry.items() if v[0] is None] + for k in stale: + del registry[k] + + litellm_params = kwargs.get("litellm_params") or {} + proxy_server_request = litellm_params.get("proxy_server_request") or {} + headers = proxy_server_request.get("headers") or {} + traceparent_ctx = ( + self.get_traceparent_from_header(headers=headers) + if headers.get("traceparent") + else None + ) + + kind = SpanKind.SERVER if proxy_server_request else SpanKind.INTERNAL + + span = self.tracer.start_span( + name="litellm_proxy_request", + context=traceparent_ctx, + kind=kind, + ) + span.set_attribute("openinference.span.kind", "CHAIN") + span.set_attribute("llm.request.model", str(kwargs.get("model", ""))) + + ctx = trace.set_span_in_context(span) + registry[call_id] = (span, ctx) + + def end_router_parent_span( + self, kwargs: dict, exception: Optional[BaseException] = None + ) -> None: + from opentelemetry.trace import Status, StatusCode + + call_id = self._router_call_id(kwargs) + if call_id is None: + return + + registry = self._router_span_registry() + entry = registry.get(call_id) + if entry is None: + return + + span, ctx = entry + if span is None: + return # already ended + + if exception is not None: + span.set_status(Status(StatusCode.ERROR)) + try: + span.record_exception(exception) + except Exception: + pass + else: + span.set_status(Status(StatusCode.OK)) + + span.end() + + # Keep ctx in registry so async success/failure callbacks that fire + # after this finally-block can still attach child spans to the same + # trace. An ended span's context (trace_id + span_id) remains valid + # for parenting. Stale entries are evicted in start_router_parent_span. + registry[call_id] = (None, ctx) + + def _get_router_parent_ctx(self, kwargs): + """Return (ctx, None) if a router parent exists for this call, else (None, None).""" + registry = getattr(self, "_router_parent_spans", None) + if not registry: + return None, None + call_id = self._router_call_id(kwargs) + if not call_id: + return None, None + entry = registry.get(call_id) + if entry is None: + return None, None + _span, ctx = entry + return ctx, None + def _get_phoenix_context(self, kwargs): """ Build a trace context for Phoenix's dedicated TracerProvider. - The base ``_get_span_context`` returns parent spans from the global - TracerProvider (the ``otel`` callback). Those spans live on a - *different* TracerProvider, so they won't appear in Phoenix — using - them as parents just creates broken links. - - Instead we: - 1. Honour an incoming ``traceparent`` HTTP header (distributed tracing). - 2. In proxy mode, create our *own* parent span on Phoenix's tracer - so the hierarchy is visible end-to-end inside Phoenix. - 3. In SDK (non-proxy) mode, just return (None, None) for a root span. + Priority: + 1. Router parent span (covers all retries + fallbacks under one root). + 2. Incoming ``traceparent`` header (distributed tracing). + 3. Proxy mode — create a per-call ``litellm_proxy_request`` parent. + 4. SDK mode — root span, no parent. """ from opentelemetry import trace + # 1. Router parent already open for this call — attach as child. + ctx, _ = self._get_router_parent_ctx(kwargs) + if ctx is not None: + return ctx, None + litellm_params = kwargs.get("litellm_params", {}) or {} proxy_server_request = litellm_params.get("proxy_server_request", {}) or {} headers = proxy_server_request.get("headers", {}) or {} - # Propagate distributed trace context if the caller sent a traceparent + # 2. Propagate distributed trace context if the caller sent a traceparent traceparent_ctx = ( self.get_traceparent_from_header(headers=headers) if headers.get("traceparent") @@ -185,8 +301,7 @@ def _get_phoenix_context(self, kwargs): is_proxy_mode = bool(proxy_server_request) if is_proxy_mode: - # Create a parent span on Phoenix's own tracer so both parent - # and child are exported to Phoenix. + # 3. Create a per-call parent on Phoenix's own tracer. start_time_val = kwargs.get("start_time", kwargs.get("api_call_start_time")) parent_span = self.tracer.start_span( name="litellm_proxy_request", @@ -199,7 +314,7 @@ def _get_phoenix_context(self, kwargs): ctx = trace.set_span_in_context(parent_span) return ctx, parent_span - # SDK mode — no parent span needed + # 4. SDK mode — no parent span needed return traceparent_ctx, None def _handle_success(self, kwargs, response_obj, start_time, end_time): @@ -399,38 +514,34 @@ async def log_success_fallback_event( kwargs: dict, original_exception: Exception, ): - """ - Log a successful fallback event by creating a span that captures the fallback chain. - - When a fallback succeeds, this creates a span showing: - - Which model originally failed (original_model_group) - - Which model it fell back to (from kwargs["model"]) - - The error that triggered the fallback - """ from opentelemetry.trace import Status, StatusCode - original_model = original_model_group fallback_model = kwargs.get("model") fallback_depth = kwargs.get("fallback_depth", 1) - status_code = getattr(original_exception, "status_code", None) exception_class = original_exception.__class__.__name__ - span_name = f"fallback: {original_model} -> {fallback_model}" - span = self.tracer.start_span(name=span_name) - - def _safe_set(s, k, v): - if hasattr(s, "set_attribute"): - s.set_attribute(k, v) - - _safe_set(span, "llm.fallback.event_type", "success") - _safe_set(span, "llm.fallback.original_model", original_model) - _safe_set(span, "llm.fallback.fallback_model", fallback_model) - _safe_set(span, "llm.fallback.attempt_number", fallback_depth) + ctx, _ = self._get_router_parent_ctx(kwargs) + span = self.tracer.start_span( + name=f"fallback: {original_model_group} -> {fallback_model}", + context=ctx, + ) + span.set_attribute("llm.fallback.event_type", "success") + span.set_attribute("llm.fallback.original_model", str(original_model_group)) + span.set_attribute("llm.fallback.fallback_model", str(fallback_model)) + span.set_attribute("llm.fallback.attempt_number", fallback_depth) + span.set_attribute("llm.fallback.error_class", exception_class) if status_code is not None: - _safe_set(span, "llm.fallback.error_status_code", status_code) - _safe_set(span, "llm.fallback.error_class", exception_class) - + span.set_attribute("llm.fallback.error_status_code", int(status_code)) + span.add_event( + "fallback_triggered", + attributes={ + "trigger.model": str(original_model_group), + "trigger.error_class": exception_class, + "trigger.status_code": str(status_code or ""), + "fallback.model": str(fallback_model), + }, + ) span.set_status(Status(StatusCode.OK)) span.end() @@ -440,37 +551,25 @@ async def log_failure_fallback_event( kwargs: dict, original_exception: Exception, ): - """ - Log a failed fallback event by creating a span that captures the failure. - - When all fallbacks fail, this creates a span showing: - - Which model originally failed - - Which fallback models were attempted - - The chain of errors - """ from opentelemetry.trace import Status, StatusCode - original_model = original_model_group fallback_attempted = kwargs.get("model") max_fallbacks = kwargs.get("max_fallbacks", 0) - - exception_class = original_exception.__class__.__name__ status_code = getattr(original_exception, "status_code", None) + exception_class = original_exception.__class__.__name__ - span_name = f"fallback_failed: {original_model}" - span = self.tracer.start_span(name=span_name) - - def _safe_set(s, k, v): - if hasattr(s, "set_attribute"): - s.set_attribute(k, v) - - _safe_set(span, "llm.fallback.event_type", "failure") - _safe_set(span, "llm.fallback.original_model", original_model) - _safe_set(span, "llm.fallback.attempted_model", fallback_attempted) - _safe_set(span, "llm.fallback.max_fallbacks", max_fallbacks) + ctx, _ = self._get_router_parent_ctx(kwargs) + span = self.tracer.start_span( + name=f"fallback_failed: {original_model_group}", + context=ctx, + ) + span.set_attribute("llm.fallback.event_type", "failure") + span.set_attribute("llm.fallback.original_model", str(original_model_group)) + span.set_attribute("llm.fallback.attempted_model", str(fallback_attempted)) + span.set_attribute("llm.fallback.max_fallbacks", int(max_fallbacks)) + span.set_attribute("llm.fallback.error_class", exception_class) if status_code is not None: - _safe_set(span, "llm.fallback.error_status_code", status_code) - _safe_set(span, "llm.fallback.error_class", exception_class) - + span.set_attribute("llm.fallback.error_status_code", int(status_code)) + span.record_exception(original_exception) span.set_status(Status(StatusCode.ERROR)) span.end() diff --git a/litellm/router.py b/litellm/router.py index b78298ff84c8..3db4a1d1d036 100644 --- a/litellm/router.py +++ b/litellm/router.py @@ -5608,6 +5608,13 @@ async def async_function_with_fallbacks(self, *args, **kwargs): mock_timeout = kwargs.pop("mock_timeout", None) + # Only the outermost call opens the parent span — recursive fallback + # calls (fallback_depth > 0) attach under the same parent. + is_outer_call = kwargs.get("fallback_depth", 0) == 0 + if is_outer_call: + self._start_router_parent_spans(kwargs) + + final_exception: Optional[BaseException] = None try: self._handle_mock_testing_fallbacks( kwargs=kwargs, @@ -5631,16 +5638,57 @@ async def async_function_with_fallbacks(self, *args, **kwargs): ) return response except Exception as e: - return await self.async_function_with_fallbacks_common_utils( - e, - disable_fallbacks, - fallbacks, - context_window_fallbacks, - content_policy_fallbacks, - model_group, - args, - kwargs, - ) + try: + return await self.async_function_with_fallbacks_common_utils( + e, + disable_fallbacks, + fallbacks, + context_window_fallbacks, + content_policy_fallbacks, + model_group, + args, + kwargs, + ) + except BaseException as fe: + final_exception = fe + raise + finally: + if is_outer_call: + self._end_router_parent_spans(kwargs, exception=final_exception) + + @staticmethod + def _start_router_parent_spans(kwargs: dict) -> None: + from litellm.litellm_core_utils.litellm_logging import _in_memory_loggers + + for logger in _in_memory_loggers: + fn = getattr(logger, "start_router_parent_span", None) + if callable(fn): + try: + fn(kwargs) + except Exception as e: + verbose_router_logger.debug( + "start_router_parent_span failed on %s: %s", + type(logger).__name__, + e, + ) + + @staticmethod + def _end_router_parent_spans( + kwargs: dict, exception: Optional[BaseException] = None + ) -> None: + from litellm.litellm_core_utils.litellm_logging import _in_memory_loggers + + for logger in _in_memory_loggers: + fn = getattr(logger, "end_router_parent_span", None) + if callable(fn): + try: + fn(kwargs, exception=exception) + except Exception as e: + verbose_router_logger.debug( + "end_router_parent_span failed on %s: %s", + type(logger).__name__, + e, + ) def _handle_mock_testing_fallbacks( self, From d02e852631b7f0feb9628eb998afec42b10a464c Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Tue, 28 Apr 2026 09:58:27 -0400 Subject: [PATCH 5/8] fix lint --- litellm/integrations/arize/_utils.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index ca9d432b4daa..aeb6347ff434 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -2,7 +2,6 @@ import hashlib import os from typing import TYPE_CHECKING, Any, Dict, Optional, Type - from pydantic import BaseModel from typing_extensions import override @@ -777,18 +776,12 @@ def _extract_chain_output( for field in ("output_text", "result"): text = _coerce_text(response_obj_dict.get(field)) if text: - mime = ( - "application/json" - if isinstance(response_obj_dict.get(field), (list, dict)) - else None - ) + mime = "application/json" or None return text, mime content = response_obj_dict.get("content") if content: - return _coerce_text(content), ( - "application/json" if isinstance(content, (list, dict)) else None - ) + return _coerce_text(content), ("application/json" or None) # 4. Reranker results results = response_obj_dict.get("results") From f2cae338a18685a68353322010c5434d7bec43ba Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Tue, 28 Apr 2026 14:39:50 -0400 Subject: [PATCH 6/8] fix --- litellm/integrations/arize/_utils.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index aeb6347ff434..b9f229105892 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -603,20 +603,15 @@ def _extract_responses_api_text(output_items) -> Optional[str]: if not isinstance(output_items, list) or not output_items: return None - def _g(obj, key, default=None): - if hasattr(obj, "get"): - return obj.get(key, default) - return getattr(obj, key, default) - texts = [] for item in output_items: - if _g(item, "type") != "message": + if _get(item, "type") != "message": continue - content_list = _g(item, "content") + content_list = _get(item, "content") if not isinstance(content_list, list): continue for c in content_list: - text = _g(c, "text") + text = _get(c, "text") if text: texts.append(text) return "\n\n".join(texts) if texts else None @@ -774,14 +769,14 @@ def _extract_chain_output( # 3. Tool / MCP result shapes — content array, output_text, result for field in ("output_text", "result"): - text = _coerce_text(response_obj_dict.get(field)) + field_value = response_obj_dict.get(field) + text = _coerce_text(field_value) if text: - mime = "application/json" or None - return text, mime + return text, ("application/json" if isinstance(field_value, (list, dict)) else None) content = response_obj_dict.get("content") if content: - return _coerce_text(content), ("application/json" or None) + return _coerce_text(content), ("application/json" if isinstance(content, (list, dict)) else None) # 4. Reranker results results = response_obj_dict.get("results") @@ -945,9 +940,8 @@ def _set_request_attributes( status_code = getattr(original_exception, "status_code", None) if status_code is not None: safe_set_attribute(span, "llm.fallback.error_status_code", status_code) - exception_class = getattr(original_exception, "__class__.__name__", None) - if exception_class: - safe_set_attribute(span, "llm.fallback.error_class", exception_class) + exception_class = type(original_exception).__name__ + safe_set_attribute(span, "llm.fallback.error_class", exception_class) safe_set_attribute( span, "llm.request.type", standard_logging_payload.get("call_type") From 3a9f8ce14aafaf42e9db3c4a31cdcb29242ec032 Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Tue, 28 Apr 2026 14:43:17 -0400 Subject: [PATCH 7/8] lint --- litellm/integrations/arize/_utils.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index b9f229105892..afedb8c58483 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -772,11 +772,17 @@ def _extract_chain_output( field_value = response_obj_dict.get(field) text = _coerce_text(field_value) if text: - return text, ("application/json" if isinstance(field_value, (list, dict)) else None) + return text, ( + "application/json" + if isinstance(field_value, (list, dict)) + else None + ) content = response_obj_dict.get("content") if content: - return _coerce_text(content), ("application/json" if isinstance(content, (list, dict)) else None) + return _coerce_text(content), ( + "application/json" if isinstance(content, (list, dict)) else None + ) # 4. Reranker results results = response_obj_dict.get("results") From d4b6247121403467b5dda6f500ae608635686adf Mon Sep 17 00:00:00 2001 From: mubashir1osmani Date: Tue, 28 Apr 2026 14:47:24 -0400 Subject: [PATCH 8/8] lint --- litellm/integrations/arize/_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/litellm/integrations/arize/_utils.py b/litellm/integrations/arize/_utils.py index afedb8c58483..c017b89d3cb1 100644 --- a/litellm/integrations/arize/_utils.py +++ b/litellm/integrations/arize/_utils.py @@ -794,8 +794,9 @@ def _extract_chain_output( resp = standard_logging_payload.get("response") text = _coerce_text(resp) if text: - mime = "application/json" if isinstance(resp, (list, dict)) else None - return text, mime + return text, ( + "application/json" if isinstance(resp, (list, dict)) else None + ) return None, None