File tree Expand file tree Collapse file tree
src/agents/extensions/models Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -364,8 +364,9 @@ async def _fetch_response(
364364 model = self .model ,
365365 )
366366
367- # Fix for interleaved thinking bug: reorder messages to ensure tool_use comes before tool_result # noqa: E501
368- if "anthropic" in self .model .lower () or "claude" in self .model .lower ():
367+ # Fix message ordering: reorder to ensure tool_use comes before tool_result.
368+ # Required for Anthropic and Vertex AI Gemini APIs which reject tool responses without preceding tool calls. # noqa: E501
369+ if any (model .lower () in self .model .lower () for model in ["anthropic" , "claude" , "gemini" ]):
369370 converted_messages = self ._fix_tool_message_ordering (converted_messages )
370371
371372 # Convert Google's extra_content to litellm's provider_specific_fields format
@@ -588,8 +589,8 @@ def _fix_tool_message_ordering(
588589 """
589590 Fix the ordering of tool messages to ensure tool_use messages come before tool_result messages.
590591
591- This addresses the interleaved thinking bug where conversation histories may contain
592- tool results before their corresponding tool calls, causing Anthropic API to reject the request .
592+ Required for Anthropic and Vertex AI Gemini APIs which require tool calls to immediately
593+ precede their corresponding tool responses in conversation history .
593594 """ # noqa: E501
594595 if not messages :
595596 return messages
You can’t perform that action at this time.
0 commit comments