Skip to content

Commit 9397f58

Browse files
chengweitsaiDennis Tsai (RD-AS)zastrowm
authored
fix: add system_prompt to structured_output_span before adding input_messages (#709)
* fix: add system_prompt to structured_output_span before adding input_messages * test: Add system message ordering validation to agent structured output test * Switch to ensuring exact ordering of messages --------- Co-authored-by: Dennis Tsai (RD-AS) <[email protected]> Co-authored-by: Mackenzie Zastrow <[email protected]>
1 parent 93d3ac8 commit 9397f58

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/strands/agent/agent.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,16 @@ async def structured_output_async(
470470
"gen_ai.operation.name": "execute_structured_output",
471471
}
472472
)
473-
for message in temp_messages:
474-
structured_output_span.add_event(
475-
f"gen_ai.{message['role']}.message",
476-
attributes={"role": message["role"], "content": serialize(message["content"])},
477-
)
478473
if self.system_prompt:
479474
structured_output_span.add_event(
480475
"gen_ai.system.message",
481476
attributes={"role": "system", "content": serialize([{"text": self.system_prompt}])},
482477
)
478+
for message in temp_messages:
479+
structured_output_span.add_event(
480+
f"gen_ai.{message['role']}.message",
481+
attributes={"role": message["role"], "content": serialize(message["content"])},
482+
)
483483
events = self.model.structured_output(output_model, temp_messages, system_prompt=self.system_prompt)
484484
async for event in events:
485485
if "callback" in event:

tests/strands/agent/test_agent.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from strands.handlers.callback_handler import PrintingCallbackHandler, null_callback_handler
1919
from strands.models.bedrock import DEFAULT_BEDROCK_MODEL_ID, BedrockModel
2020
from strands.session.repository_session_manager import RepositorySessionManager
21+
from strands.telemetry.tracer import serialize
2122
from strands.types.content import Messages
2223
from strands.types.exceptions import ContextWindowOverflowException, EventLoopException
2324
from strands.types.session import Session, SessionAgent, SessionMessage, SessionType
@@ -1028,15 +1029,23 @@ def test_agent_structured_output(agent, system_prompt, user, agenerator):
10281029
}
10291030
)
10301031

1031-
mock_span.add_event.assert_any_call(
1032-
"gen_ai.user.message",
1033-
attributes={"role": "user", "content": '[{"text": "Jane Doe is 30 years old and her email is [email protected]"}]'},
1034-
)
1032+
# ensure correct otel event messages are emitted
1033+
act_event_names = mock_span.add_event.call_args_list
1034+
exp_event_names = [
1035+
unittest.mock.call(
1036+
"gen_ai.system.message", attributes={"role": "system", "content": serialize([{"text": system_prompt}])}
1037+
),
1038+
unittest.mock.call(
1039+
"gen_ai.user.message",
1040+
attributes={
1041+
"role": "user",
1042+
"content": '[{"text": "Jane Doe is 30 years old and her email is [email protected]"}]',
1043+
},
1044+
),
1045+
unittest.mock.call("gen_ai.choice", attributes={"message": json.dumps(user.model_dump())}),
1046+
]
10351047

1036-
mock_span.add_event.assert_called_with(
1037-
"gen_ai.choice",
1038-
attributes={"message": json.dumps(user.model_dump())},
1039-
)
1048+
assert act_event_names == exp_event_names
10401049

10411050

10421051
def test_agent_structured_output_multi_modal_input(agent, system_prompt, user, agenerator):

0 commit comments

Comments
 (0)