77import json
88import logging
99import mimetypes
10- from typing import Any , Callable , Iterable , Optional , Type , TypedDict , TypeVar , cast
10+ from typing import Any , Generator , Iterable , Optional , Type , TypedDict , TypeVar , Union , cast
1111
1212import anthropic
1313from pydantic import BaseModel
1414from typing_extensions import Required , Unpack , override
1515
1616from ..event_loop .streaming import process_stream
17- from ..handlers .callback_handler import PrintingCallbackHandler
1817from ..tools import convert_pydantic_to_tool_spec
1918from ..types .content import ContentBlock , Messages
2019from ..types .exceptions import ContextWindowOverflowException , ModelThrottledException
@@ -378,24 +377,24 @@ def stream(self, request: dict[str, Any]) -> Iterable[dict[str, Any]]:
378377
379378 @override
380379 def structured_output (
381- self , output_model : Type [T ], prompt : Messages , callback_handler : Optional [ Callable ] = None
382- ) -> T :
380+ self , output_model : Type [T ], prompt : Messages
381+ ) -> Generator [ dict [ str , Union [ T , Any ]], None , None ] :
383382 """Get structured output from the model.
384383
385384 Args:
386385 output_model(Type[BaseModel]): The output model to use for the agent.
387386 prompt(Messages): The prompt messages to use for the agent.
388- callback_handler(Optional[Callable]): Optional callback handler for processing events. Defaults to None.
387+
388+ Yields:
389+ Model events with the last being the structured output.
389390 """
390- callback_handler = callback_handler or PrintingCallbackHandler ()
391391 tool_spec = convert_pydantic_to_tool_spec (output_model )
392392
393393 response = self .converse (messages = prompt , tool_specs = [tool_spec ])
394394 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" ]
395+ yield event
396+
397+ stop_reason , messages , _ , _ = event ["stop" ]
399398
400399 if stop_reason != "tool_use" :
401400 raise ValueError ("No valid tool use or tool use input was found in the Anthropic response." )
@@ -413,4 +412,4 @@ def structured_output(
413412 if output_response is None :
414413 raise ValueError ("No valid tool use or tool use input was found in the Anthropic response." )
415414
416- return output_model (** output_response )
415+ yield { "output" : output_model (** output_response )}
0 commit comments