Python: fix Langfuse observability to capture ChatAgent system instructions (#2316)

Fix bug where ChatAgent system instructions were not captured in Langfuse
traces due to incorrect attribute access.

The observability code was attempting to retrieve instructions using
getattr(self, "instructions", None), but ChatAgent stores instructions
in self.chat_options.instructions. This caused system_instructions to
always be None in Langfuse traces.

Changed both _trace_agent_run and _trace_agent_run_stream functions
to correctly retrieve instructions from chat_options.instructions.

Fixes affect:
- Line 1123: _trace_agent_run (non-streaming)
- Line 1192: _trace_agent_run_stream (streaming)
This commit is contained in:
claude89757
2025-11-19 15:08:27 +08:00
committed by GitHub
Unverified
parent 293abf5b56
commit 037349ff90
@@ -1120,7 +1120,7 @@ def _trace_agent_run(
span=span,
provider_name=provider_name,
messages=messages,
system_instructions=getattr(self, "instructions", None),
system_instructions=getattr(getattr(self, "chat_options", None), "instructions", None),
)
try:
response = await run_func(self, messages=messages, thread=thread, **kwargs)
@@ -1189,7 +1189,7 @@ def _trace_agent_run_stream(
span=span,
provider_name=provider_name,
messages=messages,
system_instructions=getattr(self, "instructions", None),
system_instructions=getattr(getattr(self, "chat_options", None), "instructions", None),
)
try:
async for update in run_streaming_func(self, messages=messages, thread=thread, **kwargs):