Fix duplicated system instructions in Python telemetry

This commit is contained in:
copilot-swe-agent[bot]
2026-05-20 21:17:37 +00:00
committed by GitHub
Unverified
parent 7566e3e382
commit 30174f9a5c
2 changed files with 16 additions and 8 deletions
@@ -2155,13 +2155,15 @@ def _capture_messages(
"""Log messages with extra information."""
from ._types import normalize_messages, prepend_instructions_to_messages
prepped = prepend_instructions_to_messages(normalize_messages(messages), system_instructions)
otel_messages: list[dict[str, Any]] = []
normalized_messages = normalize_messages(messages)
prepped = prepend_instructions_to_messages(normalized_messages, system_instructions)
span_messages: list[dict[str, Any]] = []
for index, message in enumerate(prepped):
otel_message = _to_otel_message(message)
if index >= len(prepped) - len(normalized_messages):
span_messages.append(otel_message)
# Reuse the otel message representation for logging instead of calling to_dict()
# to avoid expensive Pydantic serialization overhead
otel_message = _to_otel_message(message)
otel_messages.append(otel_message)
logger.info(
otel_message,
extra={
@@ -2171,9 +2173,9 @@ def _capture_messages(
},
)
if finish_reason:
otel_messages[-1]["finish_reason"] = FINISH_REASON_MAP[finish_reason]
span_messages[-1]["finish_reason"] = FINISH_REASON_MAP[finish_reason]
span.set_attribute(
OtelAttr.OUTPUT_MESSAGES if output else OtelAttr.INPUT_MESSAGES, json.dumps(otel_messages, ensure_ascii=False)
OtelAttr.OUTPUT_MESSAGES if output else OtelAttr.INPUT_MESSAGES, json.dumps(span_messages, ensure_ascii=False)
)
if system_instructions:
if not isinstance(system_instructions, list):
@@ -290,9 +290,9 @@ async def test_chat_client_observability_with_instructions(
assert len(system_instructions) == 1
assert system_instructions[0]["content"] == "You are a helpful assistant."
# Verify input_messages contains system message
# Verify input_messages excludes system instructions
input_messages = json.loads(span.attributes[OtelAttr.INPUT_MESSAGES])
assert any(msg.get("role") == "system" for msg in input_messages)
assert [msg.get("role") for msg in input_messages] == ["user"]
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
@@ -2981,6 +2981,9 @@ async def test_system_instructions_preserves_non_ascii_characters(span_exporter:
system_instructions = json.loads(system_instructions_json)
assert system_instructions[0]["content"] == chinese_text
input_messages = json.loads(span.attributes[OtelAttr.INPUT_MESSAGES])
assert [msg.get("role") for msg in input_messages] == ["user"]
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_tool_arguments_preserves_non_ascii_characters(span_exporter: InMemorySpanExporter):
@@ -3104,6 +3107,9 @@ async def test_agent_instructions_from_default_options(
assert len(system_instructions) == 1
assert system_instructions[0]["content"] == "Default system instructions."
input_messages = json.loads(span.attributes[OtelAttr.INPUT_MESSAGES])
assert [msg.get("role") for msg in input_messages] == ["user"]
@pytest.mark.parametrize("enable_sensitive_data", [True], indirect=True)
async def test_agent_instructions_from_options_override(