Skip to content

Commit bb727b1

Browse files
authored
Fix: Tool message ordering for Gemini models (#2331)
1 parent e833518 commit bb727b1

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

src/agents/extensions/models/litellm_model.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)