Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/strands/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,16 +470,16 @@ async def structured_output_async(
"gen_ai.operation.name": "execute_structured_output",
}
)
for message in temp_messages:
structured_output_span.add_event(
f"gen_ai.{message['role']}.message",
attributes={"role": message["role"], "content": serialize(message["content"])},
)
if self.system_prompt:
structured_output_span.add_event(
"gen_ai.system.message",
attributes={"role": "system", "content": serialize([{"text": self.system_prompt}])},
)
for message in temp_messages:
structured_output_span.add_event(
f"gen_ai.{message['role']}.message",
attributes={"role": message["role"], "content": serialize(message["content"])},
)
events = self.model.structured_output(output_model, temp_messages, system_prompt=self.system_prompt)
async for event in events:
if "callback" in event:
Expand Down
25 changes: 17 additions & 8 deletions tests/strands/agent/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from strands.handlers.callback_handler import PrintingCallbackHandler, null_callback_handler
from strands.models.bedrock import DEFAULT_BEDROCK_MODEL_ID, BedrockModel
from strands.session.repository_session_manager import RepositorySessionManager
from strands.telemetry.tracer import serialize
from strands.types.content import Messages
from strands.types.exceptions import ContextWindowOverflowException, EventLoopException
from strands.types.session import Session, SessionAgent, SessionMessage, SessionType
Expand Down Expand Up @@ -1028,15 +1029,23 @@ def test_agent_structured_output(agent, system_prompt, user, agenerator):
}
)

mock_span.add_event.assert_any_call(
"gen_ai.user.message",
attributes={"role": "user", "content": '[{"text": "Jane Doe is 30 years old and her email is [email protected]"}]'},
)
# ensure correct otel event messages are emitted
act_event_names = mock_span.add_event.call_args_list
exp_event_names = [
unittest.mock.call(
"gen_ai.system.message", attributes={"role": "system", "content": serialize([{"text": system_prompt}])}
),
unittest.mock.call(
"gen_ai.user.message",
attributes={
"role": "user",
"content": '[{"text": "Jane Doe is 30 years old and her email is [email protected]"}]',
},
),
unittest.mock.call("gen_ai.choice", attributes={"message": json.dumps(user.model_dump())}),
]

mock_span.add_event.assert_called_with(
"gen_ai.choice",
attributes={"message": json.dumps(user.model_dump())},
)
assert act_event_names == exp_event_names


def test_agent_structured_output_multi_modal_input(agent, system_prompt, user, agenerator):
Expand Down
Loading