From 9cc264471918e4622a98b8be4b3103cd4e6947f1 Mon Sep 17 00:00:00 2001 From: Kinshuk Bairagi Date: Wed, 14 Jan 2026 21:46:58 +0530 Subject: [PATCH] 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. --- changelog/3449.fixed.md | 1 + .../utils/tracing/service_decorators.py | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 changelog/3449.fixed.md diff --git a/changelog/3449.fixed.md b/changelog/3449.fixed.md new file mode 100644 index 000000000..a8663e004 --- /dev/null +++ b/changelog/3449.fixed.md @@ -0,0 +1 @@ +- Fixed telemetry record correct system_instruction in span for LLM services diff --git a/src/pipecat/utils/tracing/service_decorators.py b/src/pipecat/utils/tracing/service_decorators.py index 304ecb5e8..6f772aa88 100644 --- a/src/pipecat/utils/tracing/service_decorators.py +++ b/src/pipecat/utils/tracing/service_decorators.py @@ -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