Skip to content

Commit 51eb0b5

Browse files
committed
models - structured output - iterative callback
1 parent b687105 commit 51eb0b5

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/strands/models/anthropic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -387,15 +387,15 @@ def structured_output(
387387
prompt(Messages): The prompt messages to use for the agent.
388388
callback_handler(Optional[Callable]): Optional callback handler for processing events. Defaults to None.
389389
"""
390+
callback_handler = callback_handler or PrintingCallbackHandler()
390391
tool_spec = convert_pydantic_to_tool_spec(output_model)
391392

392393
response = self.converse(messages=prompt, tool_specs=[tool_spec])
393-
# process the stream and get the tool use input
394-
results = process_stream(
395-
response, callback_handler=callback_handler or PrintingCallbackHandler(), messages=prompt
396-
)
397-
398-
stop_reason, messages, _, _, _ = results
394+
for event in process_stream(response, prompt):
395+
if "callback" in event:
396+
callback_handler(**event["callback"])
397+
else:
398+
stop_reason, messages, _, _ = event["stop"]
399399

400400
if stop_reason != "tool_use":
401401
raise ValueError("No valid tool use or tool use input was found in the Anthropic response.")

src/strands/models/bedrock.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,15 +504,15 @@ def structured_output(
504504
prompt(Messages): The prompt messages to use for the agent.
505505
callback_handler(Optional[Callable]): Optional callback handler for processing events. Defaults to None.
506506
"""
507+
callback_handler = callback_handler or PrintingCallbackHandler()
507508
tool_spec = convert_pydantic_to_tool_spec(output_model)
508509

509510
response = self.converse(messages=prompt, tool_specs=[tool_spec])
510-
# process the stream and get the tool use input
511-
results = process_stream(
512-
response, callback_handler=callback_handler or PrintingCallbackHandler(), messages=prompt
513-
)
514-
515-
stop_reason, messages, _, _, _ = results
511+
for event in process_stream(response, prompt):
512+
if "callback" in event:
513+
callback_handler(**event["callback"])
514+
else:
515+
stop_reason, messages, _, _, _ = event["stop"]
516516

517517
if stop_reason != "tool_use":
518518
raise ValueError("No valid tool use or tool use input was found in the Bedrock response.")

tests-integ/test_model_anthropic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def tool_weather() -> str:
3434

3535
@pytest.fixture
3636
def system_prompt():
37-
return "You are an AI assistant that uses & instead of ."
37+
return "You are an AI assistant."
3838

3939

4040
@pytest.fixture
@@ -47,7 +47,7 @@ def test_agent(agent):
4747
result = agent("What is the time and weather in New York?")
4848
text = result.message["content"][0]["text"].lower()
4949

50-
assert all(string in text for string in ["12:00", "sunny", "&"])
50+
assert all(string in text for string in ["12:00", "sunny"])
5151

5252

5353
@pytest.mark.skipif("ANTHROPIC_API_KEY" not in os.environ, reason="ANTHROPIC_API_KEY environment variable missing")

0 commit comments

Comments
 (0)