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
fix(otel): index the opener and the latest prompt turns, not the oldest
yuchenga067557dae5c
diff --git a/litellm/integrations/otel/mappers/openinference.py b/litellm/integrations/otel/mappers/openinference.pyindex 1e2dbf6974d..1fa19b8d4a5 100644--- a/litellm/integrations/otel/mappers/openinference.py+++ b/litellm/integrations/otel/mappers/openinference.py@@ -92,8 +92,13 @@ class OpenInferenceMapper:return {**collect(self._LLM_CALL_ATTRS, data),**collect(self._BLOB_ATTRS, data),- **self._messages("llm.input_messages", "input.value", data.messages_in, indexed_in),- **self._messages("llm.output_messages", "output.value", outputs, indexed_out),+ **self._messages(+ "llm.input_messages",+ "input.value",+ data.messages_in,+ self._prompt_positions(len(data.messages_in), indexed_in),+ ),+ **self._messages("llm.output_messages", "output.value", outputs, range(indexed_out)),**self._tools(data),}@@ -109,18 +114,26 @@ class OpenInferenceMapper:return _MAX_INDEXED_MESSAGES - indexed_out, indexed_out@staticmethod- def _messages(prefix: str, value_key: str, messages: Sequence[object], indexed: int) -> AttributeMap:- """``{prefix}.{idx}.message.*`` keys for the leading ``indexed`` messages + the ``value_key`` blob of all."""+ def _prompt_positions(total: int, indexed: int) -> tuple[int, ...]:+ """Which prompt messages get per-index attributes: message 0 and the most recent turns.++ A value length limit clips the ``input.value`` blob, so the system prompt and the+ live turn each keep a short key of their own. The middle of a long prompt does not.+ """+ if total <= indexed:+ return tuple(range(total))+ return (0, *range(total - indexed + 1, total))++ @staticmethod+ def _messages(prefix: str, value_key: str, messages: Sequence[object], positions: Sequence[int]) -> AttributeMap:+ """``{prefix}.{idx}.message.*`` keys for the messages at ``positions`` + the ``value_key`` blob of all."""parsed: Final = [(m.get("role") if isinstance(m, dict) else None, message_content(m)) for m in messages]attrs: Final = drop_none({key: value- for idx, (role, content) in enumerate(parsed[:indexed])+ for idx, (role, content) in ((idx, parsed[idx]) for idx in positions)for key, value in (- (- f"{prefix}.{idx}.message.role",- role if isinstance(role, str) else None,- ),+ (f"{prefix}.{idx}.message.role", role if isinstance(role, str) else None),(f"{prefix}.{idx}.message.content", content),)}diff --git a/tests/test_litellm/integrations/otel/test_otel_v2_emitter.py b/tests/test_litellm/integrations/otel/test_otel_v2_emitter.pyindex f571ab7004b..6491ab0f79f 100644--- a/tests/test_litellm/integrations/otel/test_otel_v2_emitter.py+++ b/tests/test_litellm/integrations/otel/test_otel_v2_emitter.py@@ -505,7 +505,8 @@ def test_long_conversation_does_not_evict_core_attributes(turns):assert a["llm.input_messages.0.message.content"] == "turn 0"assert a["llm.output_messages.0.message.content"] == "reply 0"- assert f"llm.input_messages.{turns - 1}.message.role" not in a+ assert a[f"llm.input_messages.{turns - 1}.message.content"] == f"turn {turns - 1}"+ assert f"llm.input_messages.{turns // 2}.message.role" not in aassert len(json.loads(a["input.value"])) == turnsassert len(json.loads(a["output.value"])) == 1assert len(json.loads(a[GenAI.INPUT_MESSAGES])) == turns@@ -520,6 +521,31 @@ def test_short_conversation_keeps_every_message_indexed():assert a[f"llm.output_messages.{idx}.message.content"] == f"reply {idx}"+def test_indexed_prompt_keeps_opener_and_latest_turns_under_a_value_length_limit(monkeypatch):+ """The per-index keys are the only untruncated copy once the SDK clips string values.++ Operators bound attribute sizes with ``OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT``,+ which cuts the ``input.value`` blob short. The system prompt and the live turn+ then have to survive as their own short keys, whatever the conversation length.+ """+ monkeypatch.setenv("OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", "256")+ payload = _conversation_payload(60)+ payload["messages"][0] = {"role": "system", "content": "be terse"}+ payload["messages"][-1] = {"role": "user", "content": "LATEST-TURN"}+ a = _conversation_span(["genai", "openinference"], payload).attributes++ assert len(a["input.value"]) == 256+ assert a["llm.input_messages.0.message.role"] == "system"+ assert a["llm.input_messages.0.message.content"] == "be terse"+ assert a["llm.input_messages.59.message.role"] == "user"+ assert a["llm.input_messages.59.message.content"] == "LATEST-TURN"+ assert a["llm.output_messages.0.message.content"] == "reply 0"+ assert [int(key.split(".")[2]) for key in a if key.endswith("message.content") and key.startswith("llm.input_")] == [+ 0,+ *range(54, 60),+ ]++def test_message_cap_is_shared_across_input_and_output():"""One span-wide allowance covers both directions, and the response always keeps a share.@@ -588,4 +614,5 @@ def test_fully_populated_span_with_every_vocabulary_stays_within_the_attribute_lassert a[f"{LiteLLM.COST_PREFIX}total"] == 0.002assert a[LiteLLM.TOOLS_DECLARED] == 127assert a["llm.input_messages.0.message.content"] == "turn 0"+ assert a["llm.input_messages.199.message.content"] == "turn 199"assert a["llm.output_messages.0.message.content"] == "reply 0"