This commit is contained in:
Winoren 2026-10-27 02:23:42 +00:00 • committed by GitHub
commit f30b0b1520
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 95 additions and 9 deletions

View file

@ -1,7 +1,7 @@
import base64
import json
import os
from collections.abc import Mapping, Sequence
from collections.abc import Mapping
from datetime import datetime
from typing import TYPE_CHECKING, Any, Final, Optional
@ -128,6 +128,33 @@ class LangfuseOtelLogger(OpenTelemetry):
value = str(value)
safe_set_attribute(span, enum_attr.value, value)
@staticmethod
def _message_observation_output(item: object) -> Mapping[str, object]:
"""Flatten a Responses API ``message`` item into an observation output dict.
A message carries every content part, not just the first, so all
``output_text`` parts are joined and ``refusal`` parts are kept under
their own key. Indexing ``content[0]`` instead dropped everything past
the first part, lost refusal text entirely, and raised IndexError on an
empty content list.
"""
text_parts: Final = tuple(
part.text
for part in (getattr(item, "content", None) or ())
if getattr(part, "type", None) == "output_text" and isinstance(getattr(part, "text", None), str)
)
refusal_parts: Final = tuple(
part.refusal
for part in (getattr(item, "content", None) or ())
if getattr(part, "type", None) == "refusal" and isinstance(getattr(part, "refusal", None), str)
)
message_output: Final = {"role": getattr(item, "role", "assistant")}
if text_parts:
message_output["content"] = "".join(text_parts)
if refusal_parts:
message_output["refusal"] = "".join(refusal_parts)
return message_output
@staticmethod
def _set_observation_output(span: Span, response_obj):
"""Helper to set observation output attributes."""
@ -179,7 +206,7 @@ class LangfuseOtelLogger(OpenTelemetry):
output: Final = response_obj.get("output", [])
if output:
output_items_data: Final[list[dict]] = []
output_items_data: Final[list[Mapping[str, object]]] = []
for item in output:
if hasattr(item, "type"):
item_type = item.type
@ -193,13 +220,7 @@ class LangfuseOtelLogger(OpenTelemetry):
}
)
elif item_type == "message":
item_content: Sequence[object] = getattr(item, "content", [{}])
output_items_data.append(
{
"role": getattr(item, "role", "assistant"),
"content": getattr(item_content[0], "text", ""),
}
)
output_items_data.append(LangfuseOtelLogger._message_observation_output(item))
elif item_type == "function_call":
item_arguments: object = getattr(item, "arguments", "{}")
arguments_obj = (

View file

@ -1054,6 +1054,71 @@ class TestLangfuseOtelResponsesAPI:
assert output_data[0]["arguments"] == {}
@staticmethod
def _observation_output_for(content):
"""Run _set_observation_output over a Responses message with ``content`` parts."""
from litellm.types.integrations.langfuse_otel import LangfuseSpanAttributes
response = ResponsesAPIResponse(
id="resp_123",
created_at=1700000000,
model="gpt-5.6",
object="response",
status="completed",
output=[
{
"type": "message",
"id": "msg_1",
"status": "completed",
"role": "assistant",
"content": content,
}
],
)
with patch("litellm.integrations.arize._utils.safe_set_attribute") as mock_set:
LangfuseOtelLogger._set_observation_output(span=MagicMock(), response_obj=response)
output_calls = [
call for call in mock_set.call_args_list if call.args[1] == LangfuseSpanAttributes.OBSERVATION_OUTPUT.value
]
assert len(output_calls) == 1, "observation.output should be set exactly once"
return json.loads(output_calls[0].args[2])
def test_message_observation_output_keeps_every_output_text_part(self):
"""A message item can carry several output_text parts. Reading only the
first one silently dropped the rest of the answer."""
result = self._observation_output_for(
[
{"type": "output_text", "text": "Part one. ", "annotations": []},
{"type": "output_text", "text": "Part two.", "annotations": []},
]
)
assert result[0]["content"] == "Part one. Part two."
def test_message_observation_output_keeps_refusal_text(self):
"""A refusal-only message previously produced content="" because the code
looked for a ``text`` attribute on the refusal part, which does not exist."""
result = self._observation_output_for([{"type": "refusal", "refusal": "I can't help with that."}])
assert result[0]["refusal"] == "I can't help with that."
assert "content" not in result[0], "a refusal message must not also claim empty content"
def test_message_observation_output_keeps_text_and_refusal_separate(self):
result = self._observation_output_for(
[
{"type": "output_text", "text": "partial answer. ", "annotations": []},
{"type": "refusal", "refusal": "then I stopped."},
]
)
assert result[0]["content"] == "partial answer. "
assert result[0]["refusal"] == "then I stopped."
def test_message_observation_output_tolerates_empty_content(self):
"""Indexing content[0] raised IndexError on an empty content list, which
turned a recoverable message into a failed logging call."""
result = self._observation_output_for([])
assert result[0]["role"] == "assistant"
assert "content" not in result[0]
LANGFUSE_ENV_ONLY: Final = {
key: value
for key, value in os.environ.items()