From 3568d426871a71daa5f5995e50a6cf03223a4844 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 19:37:12 +0800 Subject: [PATCH 1/6] fix(langfuse_otel): map refusal text and multi-part message output in observation _set_observation_output read content[0].text for Responses API message items: refusal parts were dropped entirely, multi-part output_text messages lost everything past the first part, and an empty content list raised IndexError during span logging. Align the default v1 path with the OTel v2 path's _responses_parts_text: join all output_text parts into content, join refusal parts into a refusal key, and emit role-only output for an empty content list. Fixes #44824 --- .../integrations/langfuse/langfuse_otel.py | 36 +++++++-- .../test_langfuse_otel_observation_output.py | 74 +++++++++++++++++++ 2 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py diff --git a/litellm/integrations/langfuse/langfuse_otel.py b/litellm/integrations/langfuse/langfuse_otel.py index a96fac32c2a..a399c88cced 100644 --- a/litellm/integrations/langfuse/langfuse_otel.py +++ b/litellm/integrations/langfuse/langfuse_otel.py @@ -125,6 +125,35 @@ class LangfuseOtelLogger(OpenTelemetry): value = str(value) safe_set_attribute(span, enum_attr.value, value) + @staticmethod + def _message_observation_output(item) -> dict: + """Flatten a Responses API message item into an observation output dict. + + Joins every ``output_text`` part (multi-part messages previously lost + everything past the first), maps ``refusal`` parts to a ``refusal`` key + (previously dropped entirely), and tolerates an empty content list + (previously raised IndexError). Mirrors the OTel v2 path's + ``_responses_parts_text`` in ``litellm/integrations/otel/model/payloads.py``. + """ + text_parts: Final[list[str]] = [] + refusal_parts: Final[list[str]] = [] + for part in getattr(item, "content", None) or []: + part_type = getattr(part, "type", None) + if part_type == "output_text": + text = getattr(part, "text", None) + if isinstance(text, str): + text_parts.append(text) + elif part_type == "refusal": + refusal = getattr(part, "refusal", None) + if isinstance(refusal, str): + refusal_parts.append(refusal) + message_output: dict = {"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.""" @@ -190,12 +219,7 @@ class LangfuseOtelLogger(OpenTelemetry): } ) elif item_type == "message": - output_items_data.append( - { - "role": getattr(item, "role", "assistant"), - "content": getattr(getattr(item, "content", [{}])[0], "text", ""), - } - ) + output_items_data.append(LangfuseOtelLogger._message_observation_output(item)) elif item_type == "function_call": arguments_str = getattr(item, "arguments", "{}") arguments_obj = ( diff --git a/tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py b/tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py new file mode 100644 index 00000000000..1e5c46bc9af --- /dev/null +++ b/tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py @@ -0,0 +1,74 @@ +"""Regression tests for the langfuse_otel Responses API observation output (#44824). + +The v1 path indexed ``content[0].text`` for ``message`` items, so refusal text +was dropped, multi-part messages lost everything past the first part, and an +empty content list raised IndexError. Aligned with the OTel v2 path's +``_responses_parts_text`` behavior. +""" + +import json + +from litellm.integrations.langfuse.langfuse_otel import LangfuseOtelLogger +from litellm.types.llms.openai import ResponsesAPIResponse + + +class RecordingSpan: + def __init__(self): + self.attributes = {} + + def set_attribute(self, key, value): + self.attributes[key] = value + + +def observation_output(content) -> dict | None: + 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}], + ) + span = RecordingSpan() + LangfuseOtelLogger._set_observation_output(span=span, response_obj=response) + raw = span.attributes.get("litellm.params.observation_output") or span.attributes.get("langfuse.observation.output") + return json.loads(raw) if raw else None + + +def test_refusal_text_is_mapped() -> None: + result = observation_output([{"type": "refusal", "refusal": "I can't help with that."}]) + assert result is not None + assert len(result) == 1 + assert result[0]["role"] == "assistant" + assert result[0]["refusal"] == "I can't help with that." + assert "content" not in result[0] + + +def test_multi_part_output_text_is_joined() -> None: + result = observation_output( + [ + {"type": "output_text", "text": "Part one. ", "annotations": []}, + {"type": "output_text", "text": "Part two.", "annotations": []}, + ] + ) + assert result is not None + assert result[0]["content"] == "Part one. Part two." + + +def test_empty_content_list_does_not_raise() -> None: + result = observation_output([]) + assert result is not None + assert result[0]["role"] == "assistant" + assert "content" not in result[0] + + +def test_mixed_text_and_refusal_parts() -> None: + result = observation_output( + [ + {"type": "output_text", "text": "partial answer. ", "annotations": []}, + {"type": "refusal", "refusal": "then I stopped."}, + ] + ) + assert result is not None + assert result[0]["content"] == "partial answer. " + assert result[0]["refusal"] == "then I stopped." From a6609c956de374716cdf6497ec46881a611fcd20 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 20:20:06 +0800 Subject: [PATCH 2/6] fix(langfuse): keep every message content part in the otel observation --- .../integrations/langfuse/langfuse_otel.py | 41 ++++++++++--------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/litellm/integrations/langfuse/langfuse_otel.py b/litellm/integrations/langfuse/langfuse_otel.py index a399c88cced..4518b9e9be0 100644 --- a/litellm/integrations/langfuse/langfuse_otel.py +++ b/litellm/integrations/langfuse/langfuse_otel.py @@ -127,27 +127,26 @@ class LangfuseOtelLogger(OpenTelemetry): @staticmethod def _message_observation_output(item) -> dict: - """Flatten a Responses API message item into an observation output dict. + """Flatten a Responses API ``message`` item into an observation output dict. - Joins every ``output_text`` part (multi-part messages previously lost - everything past the first), maps ``refusal`` parts to a ``refusal`` key - (previously dropped entirely), and tolerates an empty content list - (previously raised IndexError). Mirrors the OTel v2 path's - ``_responses_parts_text`` in ``litellm/integrations/otel/model/payloads.py``. + 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. Mirrors the OTel v2 path's ``_responses_parts_text`` + in ``litellm/integrations/otel/model/payloads.py``. """ - text_parts: Final[list[str]] = [] - refusal_parts: Final[list[str]] = [] - for part in getattr(item, "content", None) or []: - part_type = getattr(part, "type", None) - if part_type == "output_text": - text = getattr(part, "text", None) - if isinstance(text, str): - text_parts.append(text) - elif part_type == "refusal": - refusal = getattr(part, "refusal", None) - if isinstance(refusal, str): - refusal_parts.append(refusal) - message_output: dict = {"role": getattr(item, "role", "assistant")} + 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: @@ -219,7 +218,9 @@ class LangfuseOtelLogger(OpenTelemetry): } ) elif item_type == "message": - output_items_data.append(LangfuseOtelLogger._message_observation_output(item)) + output_items_data.append( + LangfuseOtelLogger._message_observation_output(item) + ) elif item_type == "function_call": arguments_str = getattr(item, "arguments", "{}") arguments_obj = ( From 42bdb0af5d690eeca3e16a553b6038dbeaf5a27f Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 20:20:15 +0800 Subject: [PATCH 3/6] fix(langfuse): keep every message content part in the otel observation --- tests/unit/integrations/test_langfuse_otel.py | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/unit/integrations/test_langfuse_otel.py b/tests/unit/integrations/test_langfuse_otel.py index 0a9ce55fe16..d06ad938ddb 100644 --- a/tests/unit/integrations/test_langfuse_otel.py +++ b/tests/unit/integrations/test_langfuse_otel.py @@ -1006,5 +1006,74 @@ 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] + + if __name__ == "__main__": pytest.main([__file__]) From 03578ed9a243b5c13117f9e07f46e7740f3dce3d Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Tue, 6 Oct 2026 20:21:37 +0800 Subject: [PATCH 4/6] test(langfuse): fold the regression cases into the mapped otel test file --- .../test_langfuse_otel_observation_output.py | 74 ------------------- 1 file changed, 74 deletions(-) delete mode 100644 tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py diff --git a/tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py b/tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py deleted file mode 100644 index 1e5c46bc9af..00000000000 --- a/tests/unit/integrations/langfuse/test_langfuse_otel_observation_output.py +++ /dev/null @@ -1,74 +0,0 @@ -"""Regression tests for the langfuse_otel Responses API observation output (#44824). - -The v1 path indexed ``content[0].text`` for ``message`` items, so refusal text -was dropped, multi-part messages lost everything past the first part, and an -empty content list raised IndexError. Aligned with the OTel v2 path's -``_responses_parts_text`` behavior. -""" - -import json - -from litellm.integrations.langfuse.langfuse_otel import LangfuseOtelLogger -from litellm.types.llms.openai import ResponsesAPIResponse - - -class RecordingSpan: - def __init__(self): - self.attributes = {} - - def set_attribute(self, key, value): - self.attributes[key] = value - - -def observation_output(content) -> dict | None: - 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}], - ) - span = RecordingSpan() - LangfuseOtelLogger._set_observation_output(span=span, response_obj=response) - raw = span.attributes.get("litellm.params.observation_output") or span.attributes.get("langfuse.observation.output") - return json.loads(raw) if raw else None - - -def test_refusal_text_is_mapped() -> None: - result = observation_output([{"type": "refusal", "refusal": "I can't help with that."}]) - assert result is not None - assert len(result) == 1 - assert result[0]["role"] == "assistant" - assert result[0]["refusal"] == "I can't help with that." - assert "content" not in result[0] - - -def test_multi_part_output_text_is_joined() -> None: - result = observation_output( - [ - {"type": "output_text", "text": "Part one. ", "annotations": []}, - {"type": "output_text", "text": "Part two.", "annotations": []}, - ] - ) - assert result is not None - assert result[0]["content"] == "Part one. Part two." - - -def test_empty_content_list_does_not_raise() -> None: - result = observation_output([]) - assert result is not None - assert result[0]["role"] == "assistant" - assert "content" not in result[0] - - -def test_mixed_text_and_refusal_parts() -> None: - result = observation_output( - [ - {"type": "output_text", "text": "partial answer. ", "annotations": []}, - {"type": "refusal", "refusal": "then I stopped."}, - ] - ) - assert result is not None - assert result[0]["content"] == "partial answer. " - assert result[0]["refusal"] == "then I stopped." From a909b2ec20c0049fad2c2c40744d622d946b5bd0 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Thu, 15 Oct 2026 01:22:04 +0800 Subject: [PATCH 5/6] fix: address review and validation findings for PR 44826 --- litellm/integrations/langfuse/langfuse_otel.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/litellm/integrations/langfuse/langfuse_otel.py b/litellm/integrations/langfuse/langfuse_otel.py index 4518b9e9be0..e3b1bab05a3 100644 --- a/litellm/integrations/langfuse/langfuse_otel.py +++ b/litellm/integrations/langfuse/langfuse_otel.py @@ -218,9 +218,7 @@ class LangfuseOtelLogger(OpenTelemetry): } ) elif item_type == "message": - output_items_data.append( - LangfuseOtelLogger._message_observation_output(item) - ) + output_items_data.append(LangfuseOtelLogger._message_observation_output(item)) elif item_type == "function_call": arguments_str = getattr(item, "arguments", "{}") arguments_obj = ( From 43724dc02017522b2f80c81dfb80f1a6424e0581 Mon Sep 17 00:00:00 2001 From: D41910 <158550716+D41910@users.noreply.github.com> Date: Sun, 25 Oct 2026 20:50:06 +0800 Subject: [PATCH 6/6] fix: align Langfuse message output collection type --- litellm/integrations/langfuse/langfuse_otel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/litellm/integrations/langfuse/langfuse_otel.py b/litellm/integrations/langfuse/langfuse_otel.py index 6fbe6e46c64..0ab3b37d87e 100644 --- a/litellm/integrations/langfuse/langfuse_otel.py +++ b/litellm/integrations/langfuse/langfuse_otel.py @@ -206,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