Improve system message extraction in traced_llm

Enhanced the logic for extracting the system message in the traced_llm decorator to support LLMContext via adapter and handle exceptions gracefully. This improves compatibility with different context types and ensures better tracing information.
This commit is contained in:
Kinshuk Bairagi
2026-01-14 21:46:58 +05:30
parent 0dc95692ba
commit 9cc2644719
2 changed files with 19 additions and 1 deletions

1
changelog/3449.fixed.md Normal file
View File

@@ -0,0 +1 @@
- Fixed telemetry record correct system_instruction in span for LLM services

View File

@@ -500,7 +500,24 @@ def traced_llm(func: Optional[Callable] = None, *, name: Optional[str] = None) -
# Handle system message for different services
system_message = None
if hasattr(context, "system"):
# Handle system message for different services
if isinstance(context, LLMContext):
# Universal LLMContext - use adapter to convert and get system message
if hasattr(self, "get_llm_adapter"):
adapter = self.get_llm_adapter()
try:
# Get LLM invocation params which includes system_instruction
params = adapter.get_llm_invocation_params(context)
if (
isinstance(params, dict)
and "system_instruction" in params
):
system_message = params["system_instruction"]
except Exception as e:
logging.debug(
f"Could not extract system instruction from adapter: {e}"
)
elif hasattr(context, "system"):
system_message = context.system
elif hasattr(context, "system_message"):
system_message = context.system_message