2121from askui .models .shared .token_counter import SimpleTokenCounter
2222from askui .models .shared .tools import ToolCollection
2323from askui .prompts .truncation import SUMMARIZE_INSTRUCTION_PROMPT
24+ from askui .reporting import Reporter
2425
2526if TYPE_CHECKING :
2627 from askui .callbacks .conversation_callback import ConversationCallback
2728 from askui .models .shared .conversation import Conversation
28- from askui .reporting import Reporter
2929
3030logger = logging .getLogger (__name__ )
3131
@@ -81,6 +81,7 @@ def _summarize_message_history(
8181 system : SystemPrompt | None = None ,
8282 tools : ToolCollection | None = None ,
8383 provider_options : dict [str , Any ] | None = None ,
84+ reporter : Reporter | None = None ,
8485) -> MessageParam :
8586 """Ask the VLM to summarize the conversation history.
8687
@@ -99,6 +100,7 @@ def _summarize_message_history(
99100 Required for cache hits on the prefix.
100101 provider_options: Provider-specific options (e.g. ``betas``)
101102 used by the regular conversation calls.
103+ reporter: Reporter to log errors during summarization to
102104
103105 Returns:
104106 The raw VLM response message.
@@ -121,13 +123,21 @@ def _summarize_message_history(
121123 )
122124 )
123125
124- return vlm_provider .create_message (
125- messages = messages_to_summarize ,
126- max_tokens = 2048 ,
127- system = system ,
128- tools = tools ,
129- provider_options = provider_options ,
130- )
126+ try :
127+ return vlm_provider .create_message (
128+ messages = messages_to_summarize ,
129+ max_tokens = 2048 ,
130+ system = system ,
131+ tools = tools ,
132+ provider_options = provider_options ,
133+ )
134+ except Exception as e :
135+ # catch e.g. BadRequestError
136+ error_msg = f"Truncation Failed with error: { e } "
137+ logger .exception (error_msg )
138+ if reporter :
139+ reporter .add_message ("TruncationStrategy" , error_msg )
140+ raise
131141
132142
133143def _extract_summary_text (response : MessageParam ) -> str :
@@ -405,6 +415,7 @@ def truncate(self) -> None:
405415 system = system ,
406416 tools = tools ,
407417 provider_options = provider_options ,
418+ reporter = self .reporter ,
408419 )
409420 if self .reporter :
410421 self .reporter .add_message (
@@ -731,6 +742,7 @@ def truncate(self) -> None:
731742 system = system ,
732743 tools = tools ,
733744 provider_options = provider_options ,
745+ reporter = self .reporter ,
734746 )
735747 if self .reporter :
736748 self .reporter .add_message (
0 commit comments