Skip to content

Build and run agents you can see, understand and trust.

fix(tracing): report interrupted chat responses (#2450)

老福特128c2182741b
diff --git a/src/agentscope/middleware/_tracing/_extractor.py b/src/agentscope/middleware/_tracing/_extractor.py
index c01661226..a8810fbbf 100644
--- a/src/agentscope/middleware/_tracing/_extractor.py
+++ b/src/agentscope/middleware/_tracing/_extractor.py
@@ -12,7 +12,7 @@ from ._attributes import (
)
from ._converter import _convert_block_to_part
from ._utils import _serialize_to_str
-from ...model import ChatResponse, ChatModelBase
+from ...model import ChatResponse, ChatModelBase, FinishedReason
from ...event import (
ExternalExecutionResultEvent,
UserConfirmResultEvent,
@@ -289,7 +289,7 @@ def _get_llm_output_messages(
]
parts = []
- finish_reason = "stop" # Default finish reason
+ finish_reason = _get_chat_response_finish_reason(chat_response)
for block in chat_response.content:
part = _convert_block_to_part(block)
@@ -319,6 +319,19 @@ def _get_llm_output_messages(
]
+def _get_chat_response_finish_reason(
+ chat_response: ChatResponse | None,
+) -> str:
+ """Get the finish reason to record for a chat response."""
+ if not isinstance(chat_response, ChatResponse):
+ return "unknown"
+
+ if chat_response.finished_reason == FinishedReason.INTERRUPTED:
+ return "interrupted"
+
+ return "stop"
+
+
def _get_llm_response_attributes(
chat_response: ChatResponse | None,
) -> Dict[str, Any]:
@@ -344,8 +357,9 @@ def _get_llm_response_attributes(
"id",
"unknown_id",
),
- # FIXME: finish reason should be capture in chat response
- SpanAttributes.GEN_AI_RESPONSE_FINISH_REASONS: '["stop"]',
+ SpanAttributes.GEN_AI_RESPONSE_FINISH_REASONS: _serialize_to_str(
+ [_get_chat_response_finish_reason(chat_response)],
+ ),
}
if hasattr(chat_response, "usage") and chat_response.usage:
usage = chat_response.usage
diff --git a/tests/tracing_test.py b/tests/tracing_test.py
index a38b72894..fdb7a59f0 100644
--- a/tests/tracing_test.py
+++ b/tests/tracing_test.py
@@ -3,6 +3,7 @@
import asyncio
import json
from typing import Any
+from unittest import TestCase
from unittest.async_case import IsolatedAsyncioTestCase
from opentelemetry import trace as otel_trace
@@ -29,7 +30,11 @@ from agentscope.message import (
ToolResultState,
UserMsg,
)
-from agentscope.model import ChatResponse, ChatUsage
+from agentscope.middleware._tracing._attributes import SpanAttributes
+from agentscope.middleware._tracing._extractor import (
+ _get_llm_response_attributes,
+)
+from agentscope.model import ChatResponse, ChatUsage, FinishedReason
from agentscope.permission import (
PermissionContext,
PermissionDecision,
@@ -137,6 +142,34 @@ class ExternalWeatherTool(ToolBase):
return f"{city}: sunny, 25°C."
+class TracingExtractorTest(TestCase):
+ """Tests for tracing attribute extraction helpers."""
+
+ def test_llm_response_tracing_uses_chat_response_finish_reason(
+ self,
+ ) -> None:
+ """LLM response tracing should preserve ChatResponse finish reason."""
+ response = ChatResponse(
+ content=[TextBlock(text="partial answer")],
+ is_last=True,
+ finished_reason=FinishedReason.INTERRUPTED,
+ )
+
+ attributes = _get_llm_response_attributes(response)
+ output_messages = json.loads(
+ attributes[SpanAttributes.GEN_AI_OUTPUT_MESSAGES],
+ )
+
+ self.assertEqual(
+ attributes[SpanAttributes.GEN_AI_RESPONSE_FINISH_REASONS],
+ '["interrupted"]',
+ )
+ self.assertEqual(
+ output_messages[0]["finish_reason"],
+ "interrupted",
+ )
+
+
def _make_tool_call_response(tool_id: str, city: str) -> ChatResponse:
return ChatResponse(
content=[