Skip to content

Commit 1726e76

Browse files
authored
callbacks - no longer passed around (#113)
1 parent e370e41 commit 1726e76

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

docs/user-guide/concepts/agents/agent-loop.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ def event_loop_cycle(
5151
system_prompt: Optional[str],
5252
messages: Messages,
5353
tool_config: Optional[ToolConfig],
54-
callback_handler: Any,
5554
tool_handler: Optional[ToolHandler],
5655
tool_execution_handler: Optional[ParallelToolExecutorInterface] = None,
5756
**kwargs: Any,

docs/user-guide/concepts/model-providers/custom_model_provider.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -290,41 +290,41 @@ Now that you have mapped the Strands Agents input to your models request, use th
290290

291291
### 5. Structured Output Support
292292

293-
To support structured output in your custom model provider, you need to implement a `structured_output()` method that invokes your model, and has it return a json output. Below is an example of what this might look like for a Bedrock model, where we invoke the model with a tool spec, and check if the response contains a `toolUse` response.
293+
To support structured output in your custom model provider, you need to implement a `structured_output()` method that invokes your model, and has it yield a json output. Below is an example of what this might look like for a Bedrock model, where we invoke the model with a tool spec, and check if the response contains a `toolUse` response.
294294

295295
```python
296296

297297
T = TypeVar('T', bound=BaseModel)
298298

299299
@override
300300
def structured_output(
301-
self, output_model: Type[T], prompt: Messages, callback_handler: Optional[Callable] = None
302-
) -> T:
301+
self, output_model: Type[T], prompt: Messages
302+
) -> Generator[dict[str, Union[T, Any]], None, None]:
303303
"""Get structured output using tool calling."""
304-
304+
305305
# Convert Pydantic model to tool specification
306306
tool_spec = convert_pydantic_to_tool_spec(output_model)
307-
307+
308308
# Use existing converse method with tool specification
309309
response = self.converse(messages=prompt, tool_specs=[tool_spec])
310-
310+
311311
# Process streaming response
312312
for event in process_stream(response, prompt):
313-
if callback_handler and "callback" in event:
314-
callback_handler(**event["callback"])
315-
else:
316-
stop_reason, messages, _, _ = event["stop"]
317-
313+
yield event # Passed to callback handler configured in Agent instance
314+
315+
stop_reason, messages, _, _ = event["stop"]
316+
318317
# Validate tool use response
319318
if stop_reason != "tool_use":
320319
raise ValueError("No valid tool use found in the model response.")
321-
320+
322321
# Extract tool use output
323322
content = messages["content"]
324323
for block in content:
325324
if block.get("toolUse") and block["toolUse"]["name"] == tool_spec["name"]:
326-
return output_model(**block["toolUse"]["input"])
327-
325+
yield {"output": output_model(**block["toolUse"]["input"])}
326+
return
327+
328328
raise ValueError("No valid tool use input found in the response.")
329329
```
330330

0 commit comments

Comments
 (0)