Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(middleware): preserve top_p=0 in tracing request attributes (#3520)
Kiko03013ef1b1667598
diff --git a/src/agentscope/middleware/_tracing/_extractor.py b/src/agentscope/middleware/_tracing/_extractor.pyindex 96ad84c61..07b6fc51c 100644--- a/src/agentscope/middleware/_tracing/_extractor.py+++ b/src/agentscope/middleware/_tracing/_extractor.py@@ -202,8 +202,13 @@ def _get_llm_request_attributes(SpanAttributes.GEN_AI_REQUEST_MODEL: instance.model,# recommended attributesSpanAttributes.GEN_AI_REQUEST_TEMPERATURE: kwargs.get("temperature"),- SpanAttributes.GEN_AI_REQUEST_TOP_P: kwargs.get("p")- or kwargs.get("top_p"),+ # ``or`` would drop a falsy-but-valid value such as ``p=0``+ # (greedy decoding), so fall back on None instead.+ SpanAttributes.GEN_AI_REQUEST_TOP_P: (+ kwargs.get("p")+ if kwargs.get("p") is not None+ else kwargs.get("top_p")+ ),SpanAttributes.GEN_AI_REQUEST_TOP_K: kwargs.get("top_k"),SpanAttributes.GEN_AI_REQUEST_MAX_TOKENS: kwargs.get("max_tokens"),SpanAttributes.GEN_AI_REQUEST_PRESENCE_PENALTY: kwargs.get(diff --git a/tests/tracing_test.py b/tests/tracing_test.pyindex 5d51a3ca4..d39df30b8 100644--- a/tests/tracing_test.py+++ b/tests/tracing_test.py@@ -38,6 +38,7 @@ from agentscope.message import (from agentscope.middleware._tracing._attributes import SpanAttributesfrom agentscope.middleware._tracing._extractor import (_get_provider_name,+ _get_llm_request_attributes,_get_llm_response_attributes,)from agentscope.model import (@@ -190,6 +191,36 @@ class TracingExtractorTest(TestCase):self.assertEqual(_get_provider_name(model), "volcengine")+ def test_llm_request_top_p_zero_is_not_dropped(self) -> None:+ """``p=0`` (greedy decoding) is a valid value and must reach the+ ``top_p`` attribute instead of being dropped by an ``or``+ fallback."""+ model = VolcengineChatModel(+ credential=VolcengineCredential(api_key="test"),+ model="doubao-seed-2-1-pro-260628",+ )++ attributes = _get_llm_request_attributes(model, {"p": 0})++ self.assertEqual(+ attributes[SpanAttributes.GEN_AI_REQUEST_TOP_P],+ 0,+ )++ def test_llm_request_top_p_falls_back_to_top_p_key(self) -> None:+ """When ``p`` is absent, ``top_p`` is used."""+ model = VolcengineChatModel(+ credential=VolcengineCredential(api_key="test"),+ model="doubao-seed-2-1-pro-260628",+ )++ attributes = _get_llm_request_attributes(model, {"top_p": 0.9})++ self.assertEqual(+ attributes[SpanAttributes.GEN_AI_REQUEST_TOP_P],+ 0.9,+ )+def test_llm_response_tracing_uses_chat_response_finish_reason(self,) -> None: