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:
1
changelog/3449.fixed.md
Normal file
1
changelog/3449.fixed.md
Normal file
@@ -0,0 +1 @@
|
||||
- Fixed telemetry record correct system_instruction in span for LLM services
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user